记录编号 14735 评测结果 AAAAAAAAAAA
题目名称 [USACO Oct09] 乳草的入侵 最终得分 100
用户昵称 GravatarEnAsn 是否通过 通过
代码语言 Pascal 运行时间 0.012 s
提交时间 2009-11-03 20:17:16 内存使用 0.69 MiB
显示代码纯文本
program ex;
const
 g:array[1..8,1..2]of integer=((1,-1),(1,0),(1,1),(-1,-1),(-1,0),(-1,1),(0,-1),(0,1));
type
 sz=array[1..100000,1..3]of integer;
 ss=array[-1..101,-1..101]of boolean;
var
 f:sz;
 map:ss;
 x,y:integer;
 mx,my,num:integer;
procedure init;
 var
  i,j:integer;
  ch:char;
 begin
  assign(input,'milkweed.in');
  assign(output,'milkweed.out');
  reset(input);
  rewrite(output);
  readln(x,y,mx,my);
  f[1,1]:=y-my+1;
  f[1,2]:=mx;
  f[1,3]:=0;
  for i:=1 to y do
   begin
    for j:=1 to x do
     begin
      read(ch);
      if ch='.' then begin map[i,j]:=true;inc(num); end;
     end;
    readln;
   end;
  close(input);
 end;
procedure main;
 var
  head,tail,tot:integer;
  i,j:integer;
 begin
  tot:=1;
  map[f[1,1],f[1,2]]:=false;
  head:=1;tail:=1;
  while head<=tail do
   begin
    for i:=1 to 8 do
     begin
      if map[f[head,1]+g[i,1],f[head,2]+g[i,2]] then
       begin
        inc(tail);inc(tot);
        f[tail,1]:=f[head,1]+g[i,1];
        f[tail,2]:=f[head,2]+g[i,2];
        f[tail,3]:=f[head,3]+1;
        map[f[head,1]+g[i,1],f[head,2]+g[i,2]]:=false;
       end;
     end;
    inc(head);
   end;
  if tot<>num then writeln(-1) else writeln(f[tail,3]);
  close(output);
 end;
begin
 init;
 main;
end.