依旧还是BFS吧。 如果当前点是草地,则必须把草地先变成道路,拓展后的点与拓展前的点相同,并且时间+1,这样我们就保证了草地这个点经过了 两次,且时间增加了2 program P1117; const dx:array [1..4] of -1..1=(0,1,0,-1); dy:array [1..4] of -1..1=(1,0,-1,0); type atp=record xx,yy,time:longint; end; var t,x,y,ex,ey,sx,sy,head,tail:longint; a:array [0..1000,0..1000] of char; que:array [0..1000] of atp; procedure init; var i,j:longint; begin readln(t); readln(x); readln(y); for i:=y downto 1 do begin for j:=1 to x do begin read(a[j,i]); case a[j,i] of 's':begin sx:=j; sy:=i; end; 'm':begin ex:=j; ey:=i; end; end;{case} end; readln; end; end; procedure bfs; var i,tx,ty,mx,my,temp:longint; begin head:=0; tail:=1; que[1].xx:=sx; que[1].yy:=sy; que[1].time:=0; a[sx,sy]:='o'; while head<tail do begin inc(head); tx:=que[head].xx; ty:=que[head].yy; for i:=1 to 4 do begin mx:=tx+dx[i]; my:=ty+dy[i]; if (mx=ex) and (my=ey) then begin if que[head].time+1>=t then writeln(55555) else writeln(que[head].time+1); halt; end; if (mx>0) and (mx<=x) and (my>0) and (my<=y) and (a[mx,my]<>'o') then begin if a[mx,my]='.' then begin inc(tail); a[mx,my]:='o'; que[tail].xx:=mx; que[tail].yy:=my; que[tail].time:=que[head].time+1; end; if a[mx,my]='#' then begin inc(tail); a[mx,my]:='.'; que[tail].xx:=tx; que[tail].yy:=ty; que[tail].time:=que[head].time+1; end; end; end; end; end; begin init; bfs; end.