Cod sursa(job #254935)

Utilizator MihaiBunBunget Mihai MihaiBun Data 8 februarie 2009 09:54:46
Problema Kdrum Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 3.14 kb
program jiji;
var f:text;
    i,j,n,m,k,x1,x2,y1,y2,l,min,nr,ls,ld,lung,x,y:longint;
    cx,cy:array[1..100000] of longint;
    a,b:array[0..51,0..51] of longint;
    gasit:boolean;
begin
    assign(f,'kdrum.in');
    reset(f);
    readln(f,n,m,k);
    readln(f,x1,y1,x2,y2);
    for i:=1 to n do
     for j:=1 to m do begin
                         read(f,a[i,j]);
                         if a[i,j]=0 then b[i,j]:=3000
                      end;
    {BORDEZ MATRICEA B CU 3000}
    for i:=0 to n+1 do begin
                       b[i,0]:=3000;
                       b[i,m+1]:=3000
                     end;
     for i:=0 to m+1 do begin
                       b[0,i]:=3000;
                       b[n+1,i]:=3000
                     end;
    {INITIALIZEZ O COADA CU PUNCTUL DE PORNIRE}
    b[x1,y1]:=1;
    nr:=1;
    cx[1]:=x1;
    cy[1]:=y1;
    ls:=1;
    ld:=1;
    gasit:=false;
    {ADAUG IN COADA VECINII PUNCTULUI CURENT
     CAT TIMP N-AM AJUNS LA DESTINATIE}
    while not gasit do
      begin
        gasit:=true;
        nr:=nr+1;
        if b[cx[ls],cy[ls]+1]=0 then begin
                                      ld:=ld+1;
                                      cx[ld]:=cx[ls];
                                      cy[ld]:=cy[ls]+1;
                                      b[cx[ls],cy[ls]+1]:=nr;
                                      gasit:=false;
                                     end;

         if b[cx[ls],cy[ls]-1]=0 then begin
                                      ld:=ld+1;
                                      cx[ld]:=cx[ls];
                                      cy[ld]:=cy[ls]-1;
                                      b[cx[ls],cy[ls]-1]:=nr;
                                      gasit:=false
                                     end;

          if b[cx[ls]+1,cy[ls]]=0 then begin
                                       ld:=ld+1;
                                      cx[ld]:=cx[ls]+1;
                                      cy[ld]:=cy[ls];
                                      b[cx[ls]+1,cy[ls]]:=nr;
                                      gasit:=false
                                     end;

           if b[cx[ls]-1,cy[ls]]=0 then begin
                                       ld:=ld+1;
                                      cx[ld]:=cx[ls]-1;
                                      cy[ld]:=cy[ls];
                                      b[cx[ls]-1,cy[ls]]:=nr;
                                      gasit:=false
                                     end;
          ls:=ls+1;
          if b[x2,y2]<>0 then gasit:=true
    end;
    {PARCURG DRUMUL INAPOI}
    lung:=1;
    x:=x2;
    y:=y2;
    gasit:=false;
    while  not gasit do
    begin

      if (b[x,y]>b[x+1,y])and(b[x+1,y]<>0)
          then x:=x+1
          else if (b[x,y]>b[x-1,y])and(b[x-1,y]<>0)
          then x:=x-1
          else if (b[x,y]>b[x,y+1])and(b[x,y+1]<>0)
          then y:=y+1
          else if (b[x,y]>b[x,y-1])and(b[x,y-1]<>0) then y:=y-1;
      lung:=lung+1;
      if (x=x1)and(y=y1)then gasit:=true
    end;

    close(f);
    assign(f,'kdrum.out');
    rewrite(f);
    writeln(f,lung);
    close(f);

end.