记录编号 22858 评测结果 AAAAAAAAAA
题目名称 [USACO Jan09] 激光电话 最终得分 100
用户昵称 GravatarDes. 是否通过 通过
代码语言 Pascal 运行时间 0.010 s
提交时间 2010-12-30 17:01:47 内存使用 0.20 MiB
显示代码纯文本
program lphone;
var a:array[1..100,1..100]of char;
    b:array[1..100,1..100]of integer;
    x,y,z:array[0..10000]of integer;
    t,k,m,n,i,j:longint;
    c:char;
begin
assign(input,'lphone.in');
reset(input);
assign(output,'lphone.out');
rewrite(output);
readln(m,n);
for t:=1 to n do
  begin
    for k:=1 to m do
      begin
        read(a[t,k]);
        if a[t,k]='C'  then
          if i=0 then
            begin
              inc(i);
              x[1]:=t;
              y[1]:=k;
              b[t,k]:=1;
            end
          else
            begin
              x[0]:=t;
              y[0]:=k;
            end;
      end;
    readln;
  end;
i:=0;
j:=1;
repeat
  inc(i);
  t:=x[i];
  k:=y[i];
  if (t>1)and(a[t-1,k]<>'*') then
   repeat
      dec(t);

    if (b[t,k]=0) then
    begin
      inc(j);
      x[j]:=t;
      y[j]:=k;
      z[j]:=z[i]+1;
      b[t,k]:=1;
      if (x[j]=x[0])and(y[j]=y[0]) then
        begin
          writeln(z[j]-1);
          close(output);
          halt;
        end;
    end;
   until (t=1)or(a[t-1,k]='*');
  t:=x[i];
  k:=y[i];
  if (k>1)and(a[t,k-1]<>'*') then
    repeat
      dec(k);

  if (b[t,k]=0) then
    begin
      inc(j);
      x[j]:=t;
      y[j]:=k;
      z[j]:=z[i]+1;
      b[t,k]:=1;
      if (x[j]=x[0])and(y[j]=y[0]) then
        begin
          writeln(z[j]-1);
          close(output);
          halt;
        end;
    end;
  until (k=1)or(a[t,k-1]='*');
  t:=x[i];
  k:=y[i];
  if (t<n)and(a[t+1,k]<>'*') then
    repeat
      inc(t);

  if (b[t,k]=0) then
    begin
      inc(j);
      x[j]:=t;
      y[j]:=k;
      z[j]:=z[i]+1;
      b[t,k]:=1;
      if (x[j]=x[0])and(y[j]=y[0]) then
        begin
          writeln(z[j]-1);
          close(output);
          halt;
        end;
    end;
  until (t=n)or(a[t+1,k]='*');
  t:=x[i];
  k:=y[i];
  if (k<m)and(a[t,k+1]<>'*') then
    repeat
      inc(k);

  if (b[t,k]=0) then
    begin
      inc(j);
      x[j]:=t;
      y[j]:=k;
      z[j]:=z[i]+1;
      b[t,k]:=1;
      if (x[j]=x[0])and(y[j]=y[0]) then
        begin
          writeln(z[j]-1);
          close(output);
          halt;
        end;
    end;
  until (k=m)or(a[t,k+1]='*');
until i=j;
end.