program slitta; (*versione iterativa*) const MAXN=1002; MAXM=1002; BUO=100000; TRA=0; var matrice:array[0..MAXN,0..MAXM] of longint; N,M,i,j:integer; c:char; cambio:boolean; begin readln (N,M); for i:=0 to N do for j:=0 to M do begin read(c); if c='.' then matrice[i,j]:= BUO else matrice[i,j]:= TRA; end; writeln (matrice[1,0]); end. matrice[0,0]:=1; repeat i:=0; j:=0; cambio:=false; while i< N do begin j:=0; while jTRA then begin if ((i-1>=0) and (j-1>=0)) then if (matrice[i-1,j-1] > matrice[i,j]+1)then begin matrice[i-1,j-1]:= matrice[i,j]+1; cambio:=true; end; if ((i-1>=0) and (j+1 matrice[i,j]+1 )) then begin matrice[i-1,j+1]:=matrice[i,j]+1; cambio:=true; end; if (i-1>=0) then if ((matrice[i-1,j] > matrice[i,j]+1 )) then begin matrice[i-1,j]:= matrice[i,j]+1; cambio:=true;end; if (j>=1) then if ((matrice[i,j-1] > matrice[i,j]+1 )) then begin matrice[i,j-1]:= matrice[i,j]+1; cambio:=true; end; if (j+1 matrice[i,j]+1 )) then begin matrice[i,j+1]:= matrice[i,j]+1; cambio:=true;end; if ((i+1=0)) then if ((matrice[i+1,j-1] > matrice[i,j]+1 )) then begin matrice[i+1,j-1]:= matrice[i,j]+1; cambio:=true; end; if ((i+1 matrice[i,j]+1 )) then begin matrice[i+1,j+1]:= matrice[i,j]+1; cambio:=true;end; if (i+1 matrice[i,j]+1 )) then begin matrice[i+1,j]:= matrice[i,j]+1; cambio:=true; end; if (i-2>=0) then if (matrice[i-2,j] >matrice[i,j]+1 ) then begin matrice[i-2,j]:= matrice[i,j]+1; cambio:=true; end; if (i+2 matrice[i,j]+1 ) then begin matrice[i+2,j]:=matrice[i,j]+1; cambio:=true; end; if (j-2>=0) then if (matrice[i,j-2] > matrice[i,j]+1 ) then begin matrice[i,j-2]:= matrice[i,j]+1; cambio:=true; end; if (j+2matrice[i,j]+1 ) then begin matrice[i,j+2]:= matrice[i,j]+1; cambio:=true; end; end; j:=j+1; end; i:=i+1; end; until cambio=false; writeln (matrice[N-1,M-1]-1); end.