CF540C Ice Cave

这题就是一个裸的BFS,luogu的翻译不好,建议看原文QAQ

const z:array[1..4,1..2]of -1..1=((1,0),(0,1),(-1,0),(0,-1));
var i,j,k:longint;
    m,n,h,t:longint;
    ch:char;
    a,b:array[0..601,0..601]of boolean;
    x,y:array[0..1000000]of longint;
    fx,fy,lx,ly:longint;
procedure yes;//写成过程方便
begin
  write('YES');
  halt;
end;
procedure no;
begin
  write('NO');
  halt;
end;
begin
  readln(m,n);
  for i:=1 to m do
  begin
    for j:=1 to n do
    begin
      read(ch);
      if ch='X' then a[i,j]:=false;//不要看翻译那么玄学,就是不能走就好了
      if ch='.' then a[i,j]:=true;
    end;
    readln;
  end;
  readln(fx,fy,lx,ly);
  a[fx,fy]:=true;
  if (fx=lx)and(fy=ly) then//起点和终点相同的特判
  begin
    k:=0;
    for i:=1 to 4 do
    if a[lx+z[i,1],ly+z[i,2]] then inc(k);
    if k<1 then no else yes;
  end;
  if a[lx,ly] then//如果终点是浮冰,则要到终点旁的一个点绕一下
  begin
    k:=0;
    for i:=1 to 4 do
    if a[lx+z[i,1],ly+z[i,2]] then inc(k);
    if k<2 then no;//所以必须要有2个及以上的浮冰
  end else
  a[lx,ly]:=true;//终点赋成true,BFS起来方便
  //队列的初始化,不多说
  h:=1;
  t:=1;
  x[1]:=fx;
  y[1]:=fy;
  repeat
    if(x[t]=lx)and(y[t]=ly)then yes;//到终点了QAQ
    for i:=1 to 4 do
    if a[x[t]+z[i,1],y[t]+z[i,2]] then
    begin//进入队列
      inc(h);
      a[x[t]+z[i,1],y[t]+z[i,2]]:=false;
      x[h]:=x[t]+z[i,1];
      y[h]:=y[t]+z[i,2];
    end;
    inc(t);//出队
  until t>h;
  no;//走不到QAQ
end.

裸的BFS但是代码不短啊QAQ

发布了72 篇原创文章 · 获赞 22 · 访问量 5541

猜你喜欢

转载自blog.csdn.net/sxy__orz/article/details/88582992
ice
今日推荐