var n,m,l,i,j:longint;
a:array[0..1000,0..1000] of int64;
function max(x,y:int64):int64;
begin
if x< y then
exit(y)
else
exit(x);
end;
begin
read(n,m,l);
for i:=1 to n do
begin
for j:=1 to m do
read(a[i,j]);
readln;
end;
a[1,1]:=l+a[1,1];
begin
for i:=1 to n do
for j:=1 to m do
begin
a[i,j]:=max(a[i-1,j],a[i,j-1])+a[i,j];
if a[i,j]< 0 then
a[i,j]:=-maxlongint;
end;
end;
if a[n,m]>=0 then
write(a[n,m])
else
write(-1);
end.
大佬求解
var n,m,l,i,j:longint;
a:array[0..1000,0..1000] of int64;
function max(x,y:int64):int64;
begin
if x< y then
exit(y)
else
exit(x);
end;
begin
read(n,m,l);
for i:=1 to n do
begin
for j:=1 to m do
read(a[i,j]);
readln;
end;
a[1,1]:=l+a[1,1];
begin
for i:=1 to n do
for j:=1 to m do
begin
a[i,j]:=max(a[i-1,j],a[i,j-1])+a[i,j];
if a[i,j]< 0 then
a[i,j]:=-maxlongint;
end;
end;
if a[n,m]>=0 then
write(a[n,m])
else
write(-1);
end.
大佬求解
很欣慰慢慢对dp有一点感觉(错觉)了
可以用mp表地图,dp初始为负再递推结果
注意:当某个点左侧和上方的“寿元”都小于0时,这个点是无法到达的。
附C++代码: