错误结果为-1,正确结果为459
为什么啊......
program rq335;
type pos=record
x,y,t:integer;
end;
const fx:array[1..4] of integer = (-1,1,0,0);
fy:array[1..4] of integer = (0,0,-1,1);
var
map:array[-1..301,-1..301] of 0..1;
mt:array[-1..301,-1..301] of longint;
v:array[-1..301,-1..301] of 0..1;
m,i,j,x,y,t:longint;
function f(x,y:longint):boolean;
begin
f:=true;
if (x<0)or(y<0) then exit(false);
if (x>300)or(y>300) then exit(false);
if v[x,y]<>0 then exit(false);
end;
procedure bfs;
var head,tail,i,x,y,t:longint;
p:array[0..10000] of pos;
node:pos;
begin
head:=1;
tail:=1;
p[head].x:=0;
p[head].y:=0;
p[head].t:=0;
v[0,0]:=1;
while head<=tail do
begin
node:=p[head];
x:=node.x;
y:=node.y;
t:=node.t;
inc(head);
for i:=1 to 4 do
if f(x+fx[i],y+fy[i]) then
begin
if map[x+fx[i],y+fy[i]] = 0 then
begin
writeln(t+1);
exit;
end;
if (mt[x+fx[i],y+fy[i]] > t+1 ) and (map[x+fx[i],y+fy[i]]=1 ) then
begin
inc(tail);
p[tail].t:= t+1;
p[tail].x:=x+fx[i];
p[tail].y:=y+fy[i];
v[x+fx[i],y+fy[i]]:=1;
end;
end;
end;
writeln('-1');
end;
begin
readln(m);
fillchar(map,sizeof(map),0);
fillchar(mt,sizeof(mt),0);
fillchar(v,sizeof(v),0);
for i:=1 to m do
begin
readln(x,y,t);
map[x,y]:=1;
if (mt[x,y] = 0) or (mt[x,y] > t) then mt[x,y]:= t;
for j:=1 to 4 do
begin
map[x+fx[j],y+fy[j]]:=1;
if (mt[x+fx[j],y+fy[j]]=0) or (mt[x+fx[j],y+fy[j]]>t) then
mt[x+fx[j],y+fy[j]]:=t;
end;
end;
bfs; readln;
end.