记录编号 14696 评测结果 AAAAAAAAAAA
题目名称 [USACO Oct09] 乳草的入侵 最终得分 100
用户昵称 Gravatarybh 是否通过 通过
代码语言 Pascal 运行时间 0.038 s
提交时间 2009-11-03 14:26:24 内存使用 0.14 MiB
显示代码纯文本
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.