* Chỉ muốn có kết quả (thì không có đệ quy)
type kmang=array[-5..100,-5..100] of longint;
var fi,fo:text;
n,i,j:longint;
a,b:kmang;
procedure ktn;
begin
assign(fi,'robot.inp');
assign(fo,'robot.out');
{$I-} reset(fi); {$I+}
if ioresult<>0 then
begin
rewrite(fi);
reset(fi);
end;
readln(fi,n);
for i:=1 to n do
begin
for j:=1 to n do
read(fi,a[i,j]);
readln(fi);
end;
for i:=0 to n+1 do
for j:=0 to n+1 do b[i,j]:=999999;
close(fi); rewrite(fo);
end;
function min(x,y:longint):longint;
begin
min:=x;
if y<x then min:=y;
end;
procedure taomang;
begin
b[1,1]:=a[1,1];
for i:=1 to n do
for j:=1 to n do
if (i=1) and (j=1) then write('')
else b[i,j]:=a[i,j]+min(b[i,j-1],b[i-1,j]);
writeln(fo,b[n,n]);
end;
BEGIN
ktn; taomang;
close(fo);
END.
* Tìm cả đường đi (có đệ quy để truy vết)
type kmang=array[-5..100,-5..100] of longint;
var fi,fo:text;
n,i,j:longint;
a,b:kmang;
procedure ktn;
begin
assign(fi,'robot.inp');
assign(fo,'robot.out');
{$I-} reset(fi); {$I+}
if ioresult<>0 then
begin
rewrite(fi);
reset(fi);
end;
readln(fi,n);
for i:=1 to n do
begin
for j:=1 to n do
read(fi,a[i,j]);
readln(fi);
end;
for i:=0 to n+1 do
for j:=0 to n+1 do b[i,j]:=999999;
close(fi); rewrite(fo);
end;
function min(x,y:longint):longint;
begin
min:=x;
if y<x then min:=y;
end;
procedure taomang;
begin
b[1,1]:=a[1,1];
for i:=1 to n do
for j:=1 to n do
if (i=1) and (j=1) then write('')
else b[i,j]:=a[i,j]+min(b[i,j-1],b[i-1,j]);
writeln(fo,b[n,n]);
for i:=1 to n do
begin
for j:=1 to n do
write(fo,B[i,j]:5);
writeln(fo);
end;
end;
procedure truyvet(i,j:longint);
begin
if (i=1) and (j=1) then write('')
else
if b[i,j-1]<b[i-1,j] then truyvet(i,j-1)
else truyvet(i-1,j);
write(fo,a[i,j],' ');
end;
BEGIN
ktn; taomang;
truyvet(n,n);
close(fo);
END.