- 晴天小猪历险记之Hill
- 2012-10-30 19:45:00 @
var
i, j, n, x, y : longint;
a : array[1 .. 1000, 1 .. 1000] of longint;
v : array[1 .. 1000, 1 .. 1000] of longint;
function min(a, b : longint) : longint;
begin
if a = 0 then exit(b);
if a < b then exit(a) else exit(b);
end;
begin
fillchar(a, sizeof(a), 0);
readln(n);
for i := 1 to n do for j := 1 to i do read(a[i][j]);
v[n][1] := a[n][1];
for i := n downto 1 do
begin
if i < n then
begin
x := i + 1;
for j := 1 to i do
begin
y := j + 1;
v[i][j] := min(v[i][j], v[x][y] + a[i][j]);
y := j;
v[i][j] := min(v[i][j], v[x][y] + a[i][j]);
if j = 1 then
begin
y := i + 1;
v[i][j] := min(v[i][j], v[x][y] + a[i][j]);
end;
if j = i then
begin
y := 1;
v[i][j] := min(v[i][j], v[x][y] + a[i][j]);
end;
end; end;
x := i;
for j := 1 to i do
begin
y := j - 1;
if y = 0 then y := i;
v[i][j] := min(v[i][j], v[x][y] + a[i][j]);
end;
for j := i downto 1 do
begin
y := j + 1;
if y > i then y := 1;
v[i][j] := min(v[i][j], v[x][y] + a[i][j]);
end;
end;
writeln(v[1][1]);
end.