记录编号 22821 评测结果 AAAAAAAAAA
题目名称 [USACO Jan09] 激光电话 最终得分 100
用户昵称 Gravatarnick09 是否通过 通过
代码语言 Pascal 运行时间 0.011 s
提交时间 2010-12-27 20:51:32 内存使用 0.92 MiB
显示代码纯文本
var     wq,hq:array [1..100000] of longint;
        dist:array [0..101,0..101] of longint;
        map:array [0..101,0..101] of char;
        i,j,head,tail,w,h,sw,sh:longint;
procedure enq(i,j:longint);
begin
        wq[tail]:=i;
        hq[tail]:=j;
        inc(tail);
end;
procedure outq(var i,j:longint);
begin
      i:=wq[head];
      j:=hq[head];
      inc(head);

end;

begin
        assign(input,'lphone.in');
        reset(input);
        readln(h,w);
        for i:=1 to w do
        begin
                for j:=1 to h do
                begin
                        read(map[i,j]);
                        dist[i,j]:=maxlongint;
                        if map[i,j]='C' then
                        begin
                                sw:=i;
                                sh:=j;
                        end;
                end;
                readln
        end;
        close(input);
        head:=1;
        tail:=1;
        map[sw,sh]:='*';
        dist[sw,sh]:=-1;
        enq(sw,sh);

        while true do
        begin
                outq(sw,sh);
                if map[sw,sh]='C' then break;

                for i:=sw-1 downto 1 do
                begin
                        if map[i,sh]='*' then break;
                        if (dist[i,sh])=maxlongint then
                        begin
                                dist[i,sh]:=dist[sw,sh]+1;
                                enq(i,sh);
                        end;
                end;
                for i:=sw+1 to w do
                begin
                        if map[i,sh]='*' then break;
                        if (dist[i,sh])=maxlongint then
                        begin
                                dist[i,sh]:=dist[sw,sh]+1;
                                enq(i,sh);
                        end;
                end;
                for i:=sh-1 downto 1 do
                begin
                        if map[sw,i]='*' then break;
                        if (dist[sw,i])=maxlongint then
                        begin
                                dist[sw,i]:=dist[sw,sh]+1;
                                enq(sw,i);
                        end;
                end;
                for i:=sh+1 to h do
                begin
                        if map[sw,i]='*' then break;
                        if (dist[sw,i])=maxlongint then
                        begin
                                dist[sw,i]:=dist[sw,sh]+1;
                                enq(sw,i);
                        end;
                end;
        end;
        assign(output,'lphone.out');
        rewrite(output);
        writeln(dist[sw,sh]);
        {for i:=1 to w do
         begin
         for j:=1 to h do
          write(dist[i,j],' ');
         writeln;
         end;}
        close(output);

end.