比赛 2008haoi模拟训练3 评测结果 AAAAAAAAAA
题目名称 位图 最终得分 100
用户昵称 梦里醉逍遥 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2008-04-24 08:58:42
显示代码纯文本
program bit;
const
  fi='bit.in';  fo='bit.out';
  dx:array[1..4] of longint=(1,0,-1,0);
  dy:array[1..4] of longint=(0,1,0,-1);
type
  arr=array[1..200,1..200] of longint;
  que=array[1..40000] of longint;
var
  fin,fout:text;  n,m,head,tail:longint;
  graph,d,hash:arr;    qx,qy,step:que;

  procedure init;
  var
    i,j:longint;   ch:char;
  begin
    assign(fin,fi);  reset(fin);
    readln(fin,m,n);
    for i:=1 to m do begin
      for j:=1 to n do begin
        read(fin,ch);
        graph[i,j]:=ord(ch)-ord('0');
        if graph[i,j]=0 then d[i,j]:=maxlongint;
      end;
      readln(fin);
    end;
    close(fin);
  end;

  procedure insert(x,y,s:longint);
  begin
    tail:=tail+1;
    qx[tail]:=x;
    qy[tail]:=y;
    step[tail]:=s;
    hash[x,y]:=1;
  end;

  procedure bfs(x,y:longint);
  var
    i,j,k,p,q:longint;
  begin
    fillchar(hash,sizeof(hash),0);
    head:=1;  tail:=0;
    insert(x,y,0);
    while head<=tail do begin
      i:=qx[head];  j:=qy[head];
      for k:=1 to 4 do begin
        p:=i+dx[k];  q:=j+dy[k];
        if (p>0) and (p<=m) and (q>0) and (q<=n) then
          if (graph[p,q]=0) and (hash[p,q]=0) then
            if step[head]+1<d[p,q] then begin
              d[p,q]:=step[head]+1;
              insert(p,q,step[head]+1);
            end;
      end;
      head:=head+1;
    end;
  end;

  procedure main;
  var
    i,j:longint;
  begin
    for i:=1 to m do
      for j:=1 to n do
        if graph[i,j]=1 then bfs(i,j);
  end;

  procedure print;
  var
    i,j:longint;
  begin
    assign(fout,fo);  rewrite(fout);
    for i:=1 to m do begin
      for j:=1 to n-1 do
        write(fout,d[i,j],' ');
      writeln(fout,d[i,n]);
    end;
    close(fout);
  end;

begin
  init;
  main;
  print;
end.