Cod sursa(job #1229575)

Utilizator hakerulstan andrei hakerul Data 17 septembrie 2014 19:09:15
Problema Rj Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 5.6 kb
program suma;
 type matrice=array[0..100,0..100] of integer;
 el=record
    l,c:integer;
    end;
        const  dep:array[1..8] of el=((l:1;c:0),(l:0;c:1),(l:-1;c:0),(l:0;c:-1),(l:-1;c:1),(l:-1;c:-1),(l:1;c:1),(l:1;c:1));
     var
          co:array[1..46000] of el;
          k,i,j,n,m,lj,lr,cj,cr,st,sf,x:integer;
          pred:array[1..9000] of integer;
          a,b:matrice;
          f,g:text;


  procedure citire;
   var i,j:integer;
   k:char;
    begin
        readln(f,n,m);
         for i:=1 to  n do
          begin
              for j:=1 to m do
               begin
                   read(f,k);
                    if k='X' then
                     a[i,j]:=-1
                     else
                      if k=' ' then
                     a[i,j]:=0
                     else
                      if  k='J' then
                         begin
                             lj:=i;
                             cj:=j;
                         end
                         else
                          if k='R' then
                         begin
                             lr:=i;
                             cr:=j;
                         end;

               end;
               readln(f);
          end;
    end;
     procedure bordare;
      var i:integer;
       begin
          for i:=0 to n+1 do
      a[i,0]:=-1;
      for i:=0 to m+1 do
       a[0,i]:=-1;
       for i:=0 to n+1 do
        a[i,m+1]:=-1;
        for i:=0 to m+1 do
         a[n+1,i]:=-1;
       end;
       begin
           assign(f,'rj.in');
           reset(f);
           assign(g,'rj.out');
           rewrite(g);
           citire;
           bordare;
                     for i:=0 to n+1 do
                     begin
                      for j:=0 to m+1 do
                       begin
                       b[i,j]:=a[i,j];
                        end;
                        end;
                      st:=0;
                      sf:=1;
                   co[sf].l:=lr;
                   co[sf].c:=cr;
                   pred[sf]:=st;
                   a[lr,cr]:=2;
                  {  stj:=0;
                    sfj:=1;
                    coj[sfj].l:=lj;
                    coj[sfj].c:=cj; }
                     if (lj=lr) and  (cr=cj) then
                      st:=sf
                      else
                     while (st<sf)  do
                           begin
                               inc(st);
                                  for i:=1 to 8 do
                                      begin
                                          if a[co[st].l+dep[i].l,co[st].c+dep[i].c]=0 then
                                             begin
                                                 a[co[st].l+dep[i].l,co[st].c+dep[i].c]:=a[co[st].l,co[st].c]+1;
                                                  inc(sf);
                                                  pred[sf]:=st;
                                                  co[sf].l:=co[st].l+dep[i].l;
                                                  co[sf].c:=co[st].c+dep[i].c;
                                                   if (co[sf].l=lj) and (co[sf].c=cj) and ( a[co[st].l+dep[i].l,co[st].c+dep[i].c] mod 2=0) then
                                                          begin
                                                              x:=sf;
                                                              st:=sf;
                                                          end;
                                             end;

              end;
              end;
                        st:=0;
                      sf:=1;
                   co[sf].l:=lj;
                   co[sf].c:=cj;
                   pred[sf]:=st;
                   b[lj,cj]:=2;
                  {  stj:=0;
                    sfj:=1;
                    coj[sfj].l:=lj;
                    coj[sfj].c:=cj; }
                     if (lj=lr) and  (cr=cj) then
                      st:=sf
                      else
                     while (st<sf)  do
                           begin
                               inc(st);
                                  for i:=1 to 8 do
                                      begin
                                          if b[co[st].l+dep[i].l,co[st].c+dep[i].c]=0 then
                                             begin
                                                 b[co[st].l+dep[i].l,co[st].c+dep[i].c]:=b[co[st].l,co[st].c]+1;
                                                  inc(sf);
                                                  pred[sf]:=st;
                                                  co[sf].l:=co[st].l+dep[i].l;
                                                  co[sf].c:=co[st].c+dep[i].c;
                                                   if (co[sf].l=lj) and (co[sf].c=cj) and ( b[co[st].l+dep[i].l,co[st].c+dep[i].c] mod 2=0) then
                                                          begin
                                                              x:=sf;
                                                              st:=sf;
                                                          end;
                                             end;

              end;
              end;
               for i:=1 to n do
                for j:=1 to m do
                 if (a[i,j]>0) and (a[i,j]=b[i,j]) then
                  begin

                      write(g,a[i,j]-1,' ',i,' ',j);
                      break;
                      break;
                  end;
               close(f);
               close(g);
           end.