拯救ice-cream
背景 Background | |||
天好热……Tina顶着那炎炎的烈日,向Ice-cream home走去…… 可是……停电了…… 冰淇淋们躺在Ice-cream home的冰柜里,慢慢地……慢慢地……融化………… 你说,她能赶在冰淇淋融化完之前赶到Ice-cream home去吗? | |||
描述 Description | |||
给你一张坐标图,s为Tina的初始位置,m为Ice-cream home的位置,‘.’为路面,Tina在上面,每单位时间可以移动一格;‘#’为草地,Tina在上面,每两单位时间可以移动一格(建议不要模仿—毕竟Tina还小);‘o’是障碍物,Tina不能在它上面行动。也就是说,Tina只能在路面或草地上行走,必须绕过障碍物,并到达冰淇淋店。但是…………不保证到达时,冰淇淋还未融化,所以……就请聪明的你……选择最佳的方案啦…………如果,Tina到的时候,冰淇淋已经融化完了,那她可是会哭的。 | |||
输入格式 Input Format | |||
依次输入冰淇淋的融化时间t(0<t<1000),坐标图的长x,宽y(5<=x,y<=25){太长打起来好累……},和整张坐标图。 | |||
输出格式 Output Format | |||
判断按照最优方案是否可以赶在冰淇淋融化之前到达冰淇淋店(注:当T=最优方案所用时间,则判断为未赶到),如赶到,输出所用时间;如未赶到,输出Tina的哭声——“55555”(不包括引号)。 | |||
样例输入 Sample Input [复制数据] | |||
10 8 ......s... .......... #ooooooo.o #......... #......... #......... #.....m... #......... | |||
样例输出 Sample Output [复制数据] | |||
10 | |||
时间限制 Time Limitation | |||
各个测试点1s |
====================================
基础search
----------------------------
题意的理解:
融化之前....
===================
const
dx:array[1..4]of longint=(0,0,-1,1);
dy:array[1..4]of longint=(1,-1,0,0);
type
node=record
x,y:longint;
time:longint;
end;
var
t,x,y,sx,sy,mx,my:longint;
map:array[1..25,1..25]of char;
time:array[1..25,1..25]of longint;
f:array[0..26,0..26]of boolean;
h:array[1..200000]of node;
ans:longint;
procedure init;
begin
assign(input,'ty1117.in');
assign(output,'ty1117.out');
reset(input); rewrite(output);
end;
procedure terminate;
begin
close(input); close(output);
halt;
end;
procedure bfs;
var
l,r:longint;
i:longint;
begin
l:=0; r:=1;
h[1].x:=sx; h[1].y:=sy;
fillchar(time,sizeof(time),$7);
time[sx,sy]:=0;
h[1].time:=0;
ans:=maxlongint;
repeat
inc(l);
if l=200000 then l:=1;
for i:=1 to 4 do
begin
x:=h[l].x+dx[i];
y:=h[l].y+dy[i];
if f[x,y] and (map[x,y] in ['.','#','m']) then
begin
if (map[x,y]='.') then
begin
if time[x,y]>h[l].time+1 then
begin
time[x,y]:=h[l].time+1;
inc(r);
if r=200000 then r:=1;
h[r].x:=x;
h[r].y:=y;
h[r].time:=h[l].time+1;
end;
end
else
if (map[x,y]='#') then
begin
if time[x,y]>h[l].time+2 then
begin
time[x,y]:=h[l].time+2;
inc(r);
if r=200000 then r:=1;
h[r].x:=x;
h[r].y:=y;
h[r].time:=h[l].time+2;
end;
end
else
if (map[x,y]='m') then
begin
if time[x,y]>h[l].time+1 then
begin
time[x,y]:=h[l].time+1;
inc(r);
if r=200000 then r:=1;
h[r].x:=x;
h[r].y:=y;
h[r].time:=h[l].time+1;
end;
end;
if (h[r].x=mx) and (h[r].y=my) and (ans>h[r].time) then
ans:=h[r].time;
end;
end;
until l>=r;
if ans<t then writeln(ans)
else writeln('55555');
end;
procedure main;
var
i,j:longint;
begin
readln(t);
readln(x);
readln(y);
fillchar(f,sizeof(f),false);
for i:=1 to y do
begin
for j:=1 to x do
begin
read(map[i,j]);
f[i,j]:=true;
if map[i,j]='s' then
begin
sx:=i; sy:=j;
end;
if map[i,j]='m' then
begin
mx:=i; my:=j;
end;
end;
readln;
end;
ans:=maxlongint;
bfs;
end;
begin
init;
main;
terminate;
end.