【样例说明】
一共有4颗流星将坠落在农场,它们落地点的坐标分别是(0, 0),(2, 1),(1, 1)以及(0, 3),时刻分别为2,2,2,5。
t = 0 t = 2 t = 5
5|. . . . . . . 5|. . . . . . . 5|. . . . . . .
4|. . . . . . . 4|. . . . . . . 4|# . . . . . . * = 流星落点
3|. . . . . . . 3|. . . . . . . 3|* # . . . . .
2|. . . . . . . 2|. # # . . . . 2|# # # . . . . # = 行走禁区
1|. . . . . . . 1|# * * # . . . 1|# # # # . . .
0|B . . . . . . 0|* # # . . . . 0|# # # . . . .
-------------- -------------- --------------
0 1 2 3 4 5 6 0 1 2 3 4 5 6 0 1 2 3 4 5 6
如果我们观察在t=5时的牧场,可以发现离贝茜最近的安全的格子是(3,0)——不过由于早在第二颗流星落地时,贝茜直接跑去(3,0)的路线就被封死了。离贝茜第二近的安全格子为(4,0),但它的情况也跟(3,0)一样。再接下来的格子就是在(0,5)-(5,0)这条直线上。在这些格子中,(0,5),(1,4)以及(2,3)都能在5个单位时间内到达。
5|. . . . . . .
4|. . . . . . .
3|3 4 5 . . . . 某个合法的逃生方案中
2|2 . . . . . . 贝茜每个时刻所在地点
1|1 . . . . . .
0|0 . . . . . .
--------------
0 1 2 3 4 5 6
这道题我用的是广搜 代码如下:
const
x:array[1..4]of integer=(1,-1,0,0);
y:array[1..4]of integer=(0,0,1,-1);
var
e:array[-30..410,-30..410]of longint; //这里下限-1其实就可以了,上限也不用开那么大,302就可以,因为流星只能在300之内,最多影响到301
a:array[-30..410,-30..410]of boolean;
n:longint;
s:array[1..1001*1001,1..2]of longint; // 这里数组其实可以不用开那么大
f,ti:array[0..1001*1001]of longint; //
procedure init
var i,j,k,l:longint;
begin
readln(n);
for i:=-1 to 301 do
for j:=-1 to 301 do
e[i,j]:=maxlongint; //先赋一个最大值
for i:=1 to n do
begin
readln(k,j,l); //题目中行和列是反的。。。所以我们也反一下
if l<e[j,k] then e[j,k]:=l; //这个地方这样做是因为有些流星波及到的区域可能会重复,那么,本来一个地方很早就不能走了(被流星波及到)但后来又有一个
流星波及到,并且时间比之前的晚,就会覆盖掉之前的时间
if l<e[j+1,k] then e[j+1,k]:=l;
if l<e[j-1,k] then e[j-1,k]:=l;
if l<e[j,k-1] then e[j,k-1]:=l;
if l<e[j,k+1] then e[j,k+1]:=l;
end;
end;
procedure bfs;
var i,j,k,h,t:longint;
begin
h:=0; t:=1; a[0,0]:=true;
repeat
inc(h);
for i:=1 to 4 do
if (s[h,1]+x[i]>=0)and(s[h,2]+y[i]>=0) then
if (e[s[h,1]+x[i],s[h,2]+y[i]]>ti[h]+1)and(not a[s[h,1]+x[i],s[h,2]+y[i]])
then
begin
inc(t);
f[t]:=h;
ti[t]:=ti[f[t]]+1;
s[t,1]:=s[h,1]+x[i];
s[t,2]:=s[h,2]+y[i];
a[s[t,1],s[t,2]]:=true;
if e[s[t,1],s[t,2]]=maxlongint then begin writeln(ti[t]); close(input); close(output); halt; end;
end;
until h>t;
end;
begin
assign(input,'meteor.in');reset(input);
assign(output,'meteor.out');rewrite(output);
init;
bfs;
writeln(-1);
close(input); close(output);
end.