tyvj P1232 - 跳格子II
P1232 - 跳格子IIFrom Admin Normal (OI) 总时限:10s 内存限制:128MB |
||||||||
---|---|---|---|---|---|---|---|---|
|
Delphi语言: 高亮代码由发芽网提供
program tyv1232;
type node=record
x,y:longint;
end;
const max=1000000;
var f:array[1..500,1..500] of longint;
mark:array[1..500,1..500] of boolean;
a:array[1..500,1..500] of char;
q:array[0..max] of node;
d:array[1..4] of integer=(1,-1,0,0);
l:array[1..4] of integer=(0,0,-1,1);
head,tail,i,j,n,m,x1,x2,y1,y2,xx,yy:longint;
k:node;
procedure push(x,y:longint);
begin
inc(head);
q[head].x:=x;q[head].y:=y;
mark[x,y]:=true;
end;
function pop:node;
begin
pop:=q[tail];
inc(tail);
end;
begin
readln(n,m);
for i:=1 to n do begin
for j:=1 to m do
read(a[i,j]);
readln;
end;
readln(x1,y1,x2,y2);
fillchar(mark,sizeof(mark),false);
for i:=1 to n do
for j:=1 to m do f[i,j]:=maxlongint;
f[x1,y1]:=0;
head:=0;tail:=1;
push(x1,y1);
while head>=tail do begin
k:=pop;
for i:=1 to 4 do begin
xx:=k.x+d[i];yy:=k.y+l[i];
if (xx<=n)and(xx>=1)and(yy>=1)and(yy<=m) then begin
if (a[xx,yy]=a[k.x,k.y])and(f[xx,yy]>f[k.x,k.y]+1) then begin
f[xx,yy]:=f[k.x,k.y]+1;
if not mark[xx,yy] then push(xx,yy);
end
else if (f[xx,yy]>f[k.x,k.y]+2)and(a[xx,yy]<>a[k.x,k.y]) then begin
f[xx,yy]:=f[k.x,k.y]+2;
if not mark[xx,yy] then push(xx,yy);
end;
end;
end;
mark[k.x,k.y]:=false;
end;
writeln(f[x2,y2]);
end.