您的位置:首页 > 其它

拯救ice-cream(Vijos p1340)

2012-10-05 22:04 405 查看
  今晚我和几个哥们一起编这道坑爹的搜索题,BFS,刚开始总是溢出,绞尽脑汁想了各种优化办法。

  刚开始直接用bool判重,后来发现这样判重可能会漏掉最优解。拿了水出来的60分后,没编了,王大牛还在奋斗,我们商讨了一下判重方法,用一个记录,记录目前到这点的最短时间,这样就可以避免漏掉最优解的情况,以下是借用王大牛的程序:

  

program P1340;
type
rec=record
x,y:longint;
di:longint;
time:longint;
end;
var
s:array[1..100000,1..4] of longint;
time:array[1..30,1..30] of longint;
a:array[1..30,1..30] of char;
sb1,sb2:char;
b:array[1..30,1..30] of longint;
q,open,closed,t,x,y,x0,y0,x1,y1,i,j,k,l,m:longint;
begin
q:=maxlongint;
assign(input,'haha.in');
reset(input);
readln(t);
read(x,y);
for j:=1 to y do
begin
read(sb1,sb2);
for i:=1 to x do
begin
read(a[i,j]);
if a[i,j]='.'then b[i,j]:=1
else if a[i,j]='#'then b[i,j]:=2
else if a[i,j]='o' then b[i,j]:=maxlongint
else if a[i,j]='s' then begin x0:=i;y0:=j; b[i,j]:=1;end
else if a[i,j]='m' then begin x1:=i;y1:=j; b[i,j]:=0;end;
end;
end;
close(input);
for j:=1 to y do
for i:=1 to x do
time[i,j]:=maxlongint;
closed:=0; open:=1;
s[1,1]:=x0; s[1,2]:=y0; s[1,3]:=0; s[1,4]:=0;
repeat
repeat
inc(closed);
if time[s[closed,1],s[closed,2]]>s[closed,4] then begin
time[s[closed,1],s[closed,2]]:=s[closed,4];
if (s[closed,3]<>1)and(s[closed,1]<x)and(a[s[closed,1]+1,s[closed,2]]<>'o')then begin
inc(open);
s[open,1]:=s[closed,1]+1;
s[open,2]:=s[closed,2];
s[open,3]:=3;
s[open,4]:=s[closed,4]+b[s[closed,1],s[closed,2]];
end;
if a[s[open,1],s[open,2]]='m' then break;
if (s[closed,3]<>3)and(s[closed,1]>1)and(a[s[closed,1]-1,s[closed,2]]<>'o') then begin
inc(open);
s[open,1]:=s[closed,1]-1;
s[open,2]:=s[closed,2];
s[open,3]:=1;
s[open,4]:=s[closed,4]+b[s[closed,1],s[closed,2]];
end;
if a[s[open,1],s[open,2]]='m' then break;
if (s[closed,3]<>0)and(s[closed,2]>1)and(a[s[closed,1],s[closed,2]-1]<>'o') then begin
inc(open);
s[open,1]:=s[closed,1];
s[open,2]:=s[closed,2]-1;
s[open,3]:=2;
s[open,4]:=s[closed,4]+b[s[closed,1],s[closed,2]];
end;
if a[s[open,1],s[open,2]]='m' then break;
if (s[closed,3]<>2)and(s[closed,2]<y)and(a[s[closed,1],s[closed,2]+1]<>'o') then begin
inc(open);
s[open,1]:=s[closed,1];
s[open,2]:=s[closed,2]+1;
s[open,3]:=0;
s[open,4]:=s[closed,4]+b[s[closed,1],s[closed,2]];
end;
if a[s[open,1],s[open,2]]='m' then break;
end;
until (a[s[open,1],s[open,2]]='m')or(closed=open);
if q>s[open,4] then q:=s[open,4];
until (closed=open);
if q>=t then write('55555')
else
write(q);
end.


  大概就是BFS,然后判重,没什么别的技巧,憋了几个小时总算是憋出了100分,惭愧啊惭愧!

  By ZYT

  12.10.05
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: