CF540C Ice Cave

Sxy_Limit發表於2019-01-27

題目有毒呀,翻譯有問題…
先碼了一個DFS 瞄了一眼題解裡都是dfs

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:longint;
    ch:char;
    a,b:array[0..100,0..100]of boolean;
    x,y:array[0..10000]of longint;
    fx,fy,lx,ly:longint;
procedure yes;
begin
  write('YES');
  halt;
end;
procedure pd(x,y:longint);//判斷是否可以落到終點
var i:longint;
begin
  if not a[x,y] then yes;//如果終點就是碎冰那麼可以直接掉下去
  for i:=1 to 4 do if a[x+z[i,1],y+z[i,2]] then yes;//不是碎冰要看看旁邊有沒有浮冰
end;
procedure dfs(x,y:longint);//深度優先搜尋,就是暴力呀QAQ
var i:longint;
begin
  if(x=lx)and(y=ly)then//到終點了
  begin
  pd(x,y);
  exit;
  end;
  if not a[x,y] then exit;
  a[x,y]:=false;
  for i:=1 to 4 do
  dfs(x+z[i,1],y+z[i,2]);
  a[x,y]:=true;
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;
  dfs(fx,fy);
  write('NO');//到不了QAQ
end.

成功TLE祭
又費力改成bfs

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
  h:=1;
  t:=1;
  x[1]:=fx;
  y[1]:=fy;
  repeat//裸的bfs
    if(x[t]=lx)and(y[t]=ly)then yes;
    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;
end.

除了題目翻譯有問題,其他都還行…吧