比赛 20091103 评测结果 AAAAAAAAAAA
题目名称 乳草的入侵 最终得分 100
用户昵称 ybh 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2009-11-03 11:30:15
显示代码纯文本
program milkweek;
const
  weiyi:array[1..8,1..2] of integer=
    ((1,0),(0,1),(-1,0),(0,-1),(1,1),(-1,-1),(-1,1),(1,-1));
var
  a:array[0..101,0..101] of char;
  f:array[0..101,0..101] of integer;
  x,y,mx,my,i,j,k,s1,s,step,i1,j1:integer;
  bool:boolean;
begin
  assign(input,'milkweed.in');
  reset(input);
  assign(output,'milkweed.out');
  rewrite(output);
  readln(x,y,mx,my);
  for i:=1 to y do
  begin
    for j:=1 to x do
    begin
      read(a[i,j]);
      if a[i,j]='.'
        then inc(s1)
    end;
    readln
  end;
  if (x=1) and (y=1) then
  begin
    if a[1,1]='.'
      then writeln(0)
      else writeln(-1);
    close(input);
    close(output);
    halt
  end;
  fillchar(f,sizeof(f),0);
  f[y+1-my,mx]:=1;
  s:=1;
  step:=1;
  repeat
    bool:=true;
    inc(step);
    for i:=1 to y do
      for j:=1 to x do
        if f[i,j]=step-1 then
        begin
          for k:=1 to 8 do
          begin
            i1:=i+weiyi[k,1];
            j1:=j+weiyi[k,2];
            if (i1>=1) and (i1<=y) and (j1>=1) and (j1<=x) then
            begin
              if (a[i1,j1]='.') and (f[i1,j1]=0) then
              begin
                bool:=false;
                f[i1,j1]:=step;
                inc(s)
              end
            end
          end
        end
  until (s=s1) or bool;
  if bool
    then writeln(-1)
    else writeln(step-1);
  close(input);
  close(output)
end.