比赛 20101224 评测结果 WAEEWAAAEA
题目名称 激光电话 最终得分 50
用户昵称 donny 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2010-12-24 21:04:45
显示代码纯文本
program lphone;
type
  s=record
    x,y:longint;
  end;
var
  i,j,k,l,m,n,xx,yy,c,d:longint;
  a:array[1..100,1..100]of longint;
  b:array[1..100,1..100]of char;
  f:array[1..10000]of s;
  w:array[0..101,0..101]of boolean;
procedure go(aa,bb:longint);
var
  ii,jj,kk:longint;
begin
  ii:=f[aa].x;
  jj:=f[aa].y;
  kk:=a[ii,jj];
  case bb of
  1:
    while (jj>1)and(b[ii,jj-1]<>'*') do
    begin
      jj:=jj-1;
      if (kk+1)<a[ii,jj] then
      a[ii,jj]:=kk+1;
      w[ii,jj]:=true;
      inc(j);
      f[j].x:=ii;
      f[j].y:=jj;
      if (b[ii,jj]='C')and(ii<>xx)and(jj<>yy) then
      begin
        writeln(a[ii,jj]);
        close(input);
        close(output);
        halt;
      end;
    end;
  2:
    while (jj<n)and(b[ii,jj+1]<>'*') do
    begin
      jj:=jj+1;
      if (kk+1)<a[ii,jj] then
      a[ii,jj]:=kk+1;
      w[ii,jj]:=true;
      inc(j);
      f[j].x:=ii;
      f[j].y:=jj;
      if (b[ii,jj]='C')and(ii<>xx)and(jj<>yy) then
      begin
        writeln(a[ii,jj]);
        close(input);
        close(output);
        halt;
      end;
    end;
  3:
    while (ii>1)and(b[ii-1,jj]<>'*') do
    begin
      ii:=ii-1;
      if (kk+1)<a[ii,jj] then
      a[ii,jj]:=kk+1;
      w[ii,jj]:=true;
      inc(j);
      f[j].x:=ii;
      f[j].y:=jj;
      if (b[ii,jj]='C')and(ii<>xx)and(jj<>yy) then
      begin
        writeln(a[ii,jj]);
        close(input);
        close(output);
        halt;
      end;
    end;
  4:
    while (ii<m)and(b[ii+1,jj]<>'*') do
    begin
      ii:=ii+1;
      if (kk+1)<a[ii,jj] then
        a[ii,jj]:=kk+1;
      w[ii,jj]:=true;
      inc(j);
      f[j].x:=ii;
      f[j].y:=jj;
      if (b[ii,jj]='C')and(ii<>xx)and(jj<>yy) then
      begin
        writeln(a[ii,jj]);
        close(input);
        close(output);
        halt;
      end;
    end;
  end;
end;
begin
  assign(input,'lphone.in');
  reset(input);
  assign(output,'lphone.out');
  rewrite(output);
  readln(n,m);
  for i:=1 to m do
  begin
    for j:=1 to n do
      read(b[i,j]);
    readln;
  end;
  for i:=1 to m do
    for j:=1 to n do
      if b[i,j]='C' then
      begin
        xx:=i;
        yy:=j;
      end;
  for i:=1 to m do
    for j:=1 to n do
      a[i,j]:=maxint;
  i:=1;
  w[xx,yy]:=true;
  f[i].x:=xx;
  f[i].y:=yy;
  c:=xx;
  d:=yy;
  j:=1;
  while (d>1)and(b[c,d-1]<>'*') do
    begin
      d:=d-1;
      a[c,d]:=0;
      w[c,d]:=true;
      inc(j);
      f[j].x:=c;
      f[j].y:=d;
    end;
  c:=xx;
  d:=yy;
  while (d<n)and(b[c,d+1]<>'*') do
    begin
      d:=d+1;
      a[c,d]:=0;
      w[c,d]:=true;
      inc(j);
      f[j].x:=c;
      f[j].y:=d;
    end;
  c:=xx;
  d:=yy;
  while (c>1)and(b[c-1,d]<>'*') do
    begin
      dec(c);
      a[c,d]:=0;
      w[c,d]:=true;
      inc(j);
      f[j].x:=c;
      f[j].y:=d;
    end;
  c:=xx;
  d:=yy;
  while (c<m)and(b[c+1,d]<>'*') do
    begin
      inc(c);
      a[c,d]:=0;
      w[c,d]:=true;
      inc(j);
      f[j].x:=c;
      f[j].y:=d;
    end;
  repeat
    if w[f[i].x,f[i].y-1]=false then
      go(i,1);
    if w[f[i].x,f[i].y+1]=false then
      go(i,2);
    if w[f[i].x-1,f[i].y]=false then
      go(i,3);
    if w[f[i].x+1,f[i].y]=false then
      go(i,4);
    inc(i);
  until j=n*m;
  writeln('0');
  close(input);
  close(output);
end.