记录编号 127588 评测结果 AAAAAAAAAA
题目名称 [USACO Jan09] 激光电话 最终得分 100
用户昵称 Gravatar筽邝 是否通过 通过
代码语言 Pascal 运行时间 0.008 s
提交时间 2014-10-15 21:19:05 内存使用 0.88 MiB
显示代码纯文本
program cojs524l;
const
  dx:array[1..4]of longint=(-1,0,1,0);
  dy:array[1..4]of longint=(0,1,0,-1);
type
  qnode=record
    x,y,dir:longint;
  end;

var
  q:array[1..50000]of qnode;
  a:array[0..110,0..110]of boolean;
  v:array[0..110,0..110,1..4]of boolean;
  f:array[0..110,0..110,1..4]of longint;
  tx,ty,sx,sy,ans,n,m:longint;

procedure init;
var
  ch:char;
  i,j,k:longint;
  flag:boolean;
begin
  readln(m,n);
  flag:=true;
  for i:=1 to n do
  begin
    for j:=1 to m do
    begin
      read(ch);
      case ch of
      '.':a[i,j]:=true;
      'C':if flag then
          begin
            sx:=i;
            sy:=j;
            flag:=false;
            a[i,j]:=true;
          end else begin
            tx:=i;
            ty:=j;
            a[i,j]:=true;
          end;
      '*':a[i,j]:=false;
      end;
    end;
    readln;
  end;
  for i:=1 to n do
  for j:=1 to m do
  for k:=1 to 4 do
    f[i,j,k]:=maxlongint shr 1;
end;

procedure bfs;
var
  t,xx,yy,i,front,tail:longint;
begin
  front:=0; tail:=4;
  for i:=1 to 4 do
  begin
    q[i].x:=sx; q[i].y:=sy; q[i].dir:=i;
    v[sx,sy,i]:=true;
    f[sx,sy,i]:=0;
  end;
  while front<>tail do
  begin
    inc(front);
    for i:=1 to 4 do
    begin
      xx:=q[front].x+dx[i]; yy:=q[front].y+dy[i];
      if i=q[front].dir then t:=f[q[front].x,q[front].y,q[front].dir]
      else t:=f[q[front].x,q[front].y,q[front].dir]+1;
      if not a[xx,yy] then continue;
      if t<f[xx,yy,i] then
      begin
        f[xx,yy,i]:=t;
        if not v[xx,yy,i] then
        begin
          inc(tail);
          q[tail].x:=xx; q[tail].y:=yy; q[tail].dir:=i;
          v[xx,yy,i]:=true;
        end;
      end;
    end;
    v[q[front].x,q[front].y,q[front].dir]:=false;
  end;
  for i:=1 to 4 do
  if f[tx,ty,i]<ans then ans:=f[tx,ty,i];
end;

begin
assign(input,'lphone.in');reset(input);
assign(output,'lphone.out');rewrite(output);

  init;
  ans:=maxlongint;
  bfs;
  writeln(ans);

close(input);close(output);
end.