比赛 20091103 评测结果 AAAAAAAAAAA
题目名称 乳草的入侵 最终得分 100
用户昵称 maxiem 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2009-11-03 10:47:32
显示代码纯文本
program milkweed;
const dir:array [1..8,1..2] of shortint=((-1,0),(1,0),(0,-1),(0,1),(-1,-1),(-1,1),(1,-1),(1,1));
var
  table:array [1..100,1..100] of integer;
  q:array [1..10000] of record
    w,x,y:integer;
  end;
  x0,y0,m,n,x,y,i,j,head,tail,grass:integer;
  flag:boolean;
  str:string;
begin
  fillchar (table,sizeof(table),0);
  fillchar (q,sizeof(q),0);
  assign (input,'milkweed.in');
  reset (input);
  readln (m,n,x,y);
  grass:=0;
  for i:=1 to n do begin
    readln (str);
    for j:=1 to m do if str[j]='.' then inc(grass) else table[i,j]:=1;
  end;
  head:=1;tail:=1;
  q[1].x:=n+1-y;
  q[1].y:=x;
  q[1].w:=0;
  table[q[1].x,q[1].y]:=2;
  flag:=false;
  dec(grass);
  repeat
    for i:=1 to 8 do begin
      x0:=q[tail].x+dir[i,1];
      y0:=q[tail].y+dir[i,2];
      if (x0>=1) and (x0<=n) and (y0>=1) and (y0<=m) and (table[x0,y0]=0) then begin
        dec(grass);
        table[x0,y0]:=2;
        inc (head);
        q[head].x:=x0;
        q[head].y:=y0;
        q[head].w:=q[tail].w+1;
      end;
      if grass=0 then begin
        flag:=true;
        break;
      end;
    end;
    inc(tail);
  until (tail>head) or (flag);
  close (input);
  assign (output,'milkweed.out');
  rewrite (output);
  if flag then writeln (q[head].w) else writeln (-1);
  close (output);
end.