(而我只开到longint了)
附程序:
program rq35;
type node=record
x,y:integer;
end;
node2=record
step,health:longint;
end;
const fx:array [1..8,1..2] of integer=((1,0),(0,1),(-1,0),(0,-1),(1,-1),(1,1),(-1,1),(-1,-1));
var s:array [1..500] of {ansi}string;
queue:array [1..250000] of node;
head,tail,n,m,rgx,rgy,i1,j1:integer;
best:array [1..500,1..500] of node2;
procedure init;
var i,j:integer;
begin
fillchar(queue,sizeof(queue),0);
readln(i1,j1);
head:=1;
tail:=1;
queue[1].x:=i1;
queue[1].y:=j1;
readln(rgx,rgy);
for i:=1 to n do
readln(s[i]);
for i:=1 to n do
for j:=1 to m do
with best[i,j] do
begin
step:=maxlongint;
health:=maxlongint;
end;
with best[1,1] do
begin
step:=1;
health:=0;
end;
end;
function value(x,y:integer):boolean;
begin
exit((x>0) and (y>0) and (x<=n) and (y<=m));
end;
procedure bfs;
var i,w:shortint;
tx,ty:integer;
begin
while head<=tail do
begin
{if (queue[head].x=rgx) and (queue[head].y=rgy) then
begin
if (queue[head].health<ans1) or ((queue[head].health=ans1) and (queue[head].step<ans2)) then
begin
ans1:=queue[head].health;
ans2:=queue[head].step;
end;
end
else}
for i:=1 to 8 do
begin
tx:=queue[head].x+fx[i,1];
ty:=queue[head].y+fx[i,2];
if value(tx,ty) and (s[tx][ty]<>’0’) and (s[tx][ty]<>’3’) then
begin
tail:=tail+1;
queue[tail].x:=tx;
queue[tail].y:=ty;
{queue[tail].step:=queue[head].step+1;
if s[tx][ty]=s[queue[head].x][queue[head].y] then queue[tail].health:=queue[head].health
else queue[tail].health:=queue[head].health+1;}
if (s[tx,ty]=s[queue[head].x,queue[head].y]) then w:=0
else w:=1;
if (best[tx,ty].health>best[queue[head].x,queue[head].y].health+w) or
(best[tx,ty].step>best[queue[head].x,queue[head].y].step+1) and (best[tx,ty].health=best[queue[head].x,queue[head].y].health+w) then
begin
best[tx,ty].step:=best[queue[head].x,queue[head].y].step+1;
best[tx,ty].health:=best[queue[head].x,queue[head].y].health+w;
end;
{s[tx,ty]:=’3’;}
end;
end;
{if head=1 then s[i1][j1]:=’3’;}
s[queue[head].x,queue[head].y]:=’3’;
head:=head+1;
end;
writeln(best[rgx,rgy].step,’ ’,best[rgx,rgy].health);
end;
begin
readln(n,m);
init;
bfs;
end.