问题描述: | 故事的起源不加赘述,那23个路口。 | |
数据输入: | 第1行两个整数 n,m (茫茫大街的长和宽) | |
结果输出: | 一个整数sum (可以得到的最大憧憬值) | |
样例: | 4 4 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 | 23 |
核心思想: | 动态规划(四个方向) |
var
a:array[0..310,0..310]of longint;
f:array[0..310,0..310,0..23]of int64;
go:array[1..4,1..2]oflongint=((0,-1),(1,0),(0,1),(-1,0));
n,m,i,j,k,p:longint;
ans:int64;
function max(a,b:int64):int64;
begin
ifa>b then exit(a);
exit(b);
end;
begin
assign(input,'p1253.in');reset(input);
assign(output,'p1253.out');rewrite(output);
readln(m,n);
fillchar(f,sizeof(f),200);
fori:=1 to n do
begin
for j:=1 to m do
begin
read(a[i,j]);
if a[i,j]=0 then f[i,j,0]:=0;
end;
readln;
end;
fork:=1 to 23 do
fori:=1 to n do
for j:=1 to m do
if a[i,j]>0 then{<障碍和起点不进行>}
begin
for p:=1 to 4 do
if f[i+go[p,1],j+go[p,2],k-1]>=0 then
f[i,j,k]:=max(f[i,j,k],f[i+go[p,1],j+go[p,2],k-1]);
if f[i,j,k]>=0 then
inc(f[i,j,k],a[i,j]);
end;
ans:=-maxlongint;
fori:=1 to n do{<结束可以在任意点(除起点和障碍),取最大>}
forj:=1 to m do
ans:=max(ans,f[i,j,23]);
writeln(ans);
close(input);close(output);
end.
题目来源:NDK 1253