比赛 2008haoi模拟训练3 评测结果 AAAATTTTTT
题目名称 位图 最终得分 40
用户昵称 cuixiaofei 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2008-04-24 11:26:12
显示代码纯文本
//na                            :cuixiaofei;
//da                            :08_04_24;
program bit;
  const
    max                         =200;
    fx                          :array[1..4,1..2] of longint=((-1,0),(0,1),(1,0),(0,-1));
  var
    m,n,ge                      :longint;
    a                           :array[0..max,0..max] of char;
    ji                          :array[1..max*max,1..2] of longint;
    chu                         :array[0..max,0..max] of longint;
    f1,f2                       :text;
  procedure init;
    var
      i,j                       :longint;
  begin
    assign(f1,'bit.in');
    reset(f1);
    assign(f2,'bit.out');
    rewrite(f2);
    fillchar(ji,sizeof(ji),0);
    readln(f1,n,m);
    for i:=0 to n+1 do
      begin
        a[i,0]:='!';
        a[i,m+1]:='!';
      end;
    for i:=0 to m+1 do
      begin
        a[0,i]:='!';
        a[n+1,i]:='!';
      end;
    ge:=0;
    for i:=1 to n do
     begin
       for j:=1 to m do
         begin
           read(f1,a[i,j]);
           chu[i,j]:=maxlongint;
           if a[i,j]='1' then
             begin
               inc(ge);
               ji[ge,1]:=i;
               ji[ge,2]:=j;
             end;
         end;
       readln(f1);
     end;
  end;
  procedure kancha(k:longint);
    var
      i,j,st,ed                 :longint;
      pc,jilu                   :array[1..max,1..max] of longint;
      jin                       :array[1..max*max,1..2] of longint;
  begin
    fillchar(jilu,sizeof(jilu),0);
    jin[1,1]:=ji[k,1];
    jin[1,2]:=ji[k,2];
    st:=1;
    ed:=1;
    pc[jin[1,1],jin[1,2]]:=0;
    jilu[jin[1,1],jin[1,2]]:=1;
    while st<=ed do
    begin
      for i:=1 to 4 do
        if (a[jin[st,1]+fx[i,1],jin[st,2]+fx[i,2]]<>'!') and (jilu[jin[st,1]+fx[i,1],jin[st,2]+fx[i,2]]=0) then
          begin
            inc(ed);
            jin[ed,1]:=jin[st,1]+fx[i,1];
            jin[ed,2]:=jin[st,2]+fx[i,2];
            if a[jin[ed,1],jin[ed,2]]<>'1' then
            pc[jin[ed,1],jin[ed,2]]:=abs(jin[ed,1]-ji[k,1])+abs(jin[ed,2]-ji[k,2]);
            jilu[jin[ed,1],jin[ed,2]]:=1;
          end;
        inc(st);
    end;
    for i:=1 to n do
      for j:=1 to m do
        if chu[i,j]>pc[i,j] then
          chu[i,j]:=pc[i,j];
  end;
  procedure main;
    var
      i                         :longint;
  begin
    for i:=1 to ge do
      begin
        kancha(i);
      end;
  end;
  procedure print;
    var
      i,j                       :longint;
  begin
    for i:=1 to n do
      begin
        for j:=1 to m-1 do
          write(f2,chu[i,j],' ');
        writeln(f2,chu[i,m]);
      end;
    close(f1);
    close(f2);
  end;
  begin
    init;
    main;
    print;
  end.