1187: [HNOI2007]神奇游乐园
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 246 Solved: 118
[ Submit][ Status][ Discuss]
Description
经历了一段艰辛的旅程后,主人公小P乘坐飞艇返回。在返回的途中,小P发现在漫无边际的沙漠中,有一块狭长的绿地特别显眼。往下仔细一看,才发现这是一个游乐场,专为旅途中疲惫的人设计。娱乐场可以看成是一块大小为n×m的区域,且这个n×m的区域被分成n×m个小格子,每个小格子中就有一个娱乐项目。然而,小P并不喜欢其中的所有娱乐项目,于是,他给每个项目一个满意度。满意度为正时表示小P喜欢这个项目,值越大表示越喜欢。为负时表示他不喜欢,这个负数的绝对值越大表示他越不喜欢。为0时表示他对这个项目没有喜恶。小P决定将飞艇停在某个小格中,然后每步他可以移动到相邻的上下左右四个格子的某个格子中。小P希望找一条路径,从飞艇所在格出发,最后又回到这个格子。小P有一个习惯,从不喜欢浪费时间。因此,他希望经过每个格子都是有意义的:他到一个地方后,就一定要感受以下那里的惊险和刺激,不管自己是不是喜欢那里的娱乐项目。而且,除了飞艇所在格,其他的格子他不愿意经过两次。小P希望自己至少要经过四个格子。在满足这些条件的情况下,小P希望自己玩过的娱乐项目的满意度之和最高。你能帮他找到这个最高的满意度之和吗?
Input
输入文件中的第一行为两个正整数n和m,表示游乐场的大小为n×m。因为这个娱乐场很狭窄,所以n和m满足:2<=n<=100,2<=m<=6。接下来的n行,每行有m个整数,第i行第j列表示游乐场的第i行第j列的小格子中的娱乐项目的满意度,这个满意度的范围是[-1000,1000]。同一行的两个整数之间用空格隔开。
Output
输出文件中仅一行为一个整数,表示最高的满意度之和。
Sample Input
4 4
100 300 -400 400
-100 1000 1000 1000
-100 -100 -100 -100
-100 -100 -100 1000
100 300 -400 400
-100 1000 1000 1000
-100 -100 -100 -100
-100 -100 -100 1000
Sample Output
4000
HINT
大家测下这个数据5 5
1 1 -100 3 3
1 1 -100 3 3
1 1 -100 3 3
1 1 -100 3 3
1 1 -100 3 3
结果是30?
裸插头dp
可以任意起点,跪烂
program bzoj1187;
var
v,stu,w,x,y,o,ans,n,m,i,j,k:longint;
a:array [0..101,0..7] of longint;
thr:array [0..8] of longint;
tot:array [0..1] of longint;
dl,f:array [0..1,0..2201] of longint;
yes:array [0..2201] of boolean;
procedure update (stu,point:longint);
begin
if not yes[stu] then
begin
yes[stu]:=true;
f[o,stu]:=point;
inc(tot[o]);
dl[o,tot[o]]:=stu;
end;
if f[o,stu]<point then f[o,stu]:=point;
end;
begin
ans:=-maxlongint;
read(n,m);
for i:=1 to n do
for j:=1 to m do
read(a[i,j]);
thr[0]:=1;
for i:=1 to 8 do
thr[i]:=thr[i-1]*3;
o:=0;
tot[o]:=1;
dl[o,1]:=0;
f[o,0]:=0;
for i:=1 to n do
for j:=1 to m do
begin
o:=1-o;
tot[o]:=0;
fillchar(yes,sizeof(yes),false);
for k:=1 to tot[1-o] do
begin
if j=1 then stu:=dl[1-o,k]*3
else stu:=dl[1-o,k];
x:=stu div thr[j-1] mod 3;
y:=stu div thr[j] mod 3;
if (x=1)and(y=2) then
begin
if (x*thr[j-1]+y*thr[j]=stu)and(f[1-o,dl[1-o,k]]+a[i,j]>ans) then
ans:=f[1-o,dl[1-o,k]]+a[i,j];
continue;
end;
if (x=2)and(y=1) then
begin
update(stu-x*thr[j-1]-y*thr[j],f[1-o,dl[1-o,k]]+a[i,j]);
continue;
end;
if (x=0)and(y=0) then
begin
update(stu,f[1-o,dl[1-o,k]]);
if j<>m then update(stu+thr[j-1]+2*thr[j],f[1-o,dl[1-o,k]]+a[i,j]);
continue;
end;
if x=0 then
begin
update(stu-x*thr[j-1]-y*thr[j]+y*thr[j-1],f[1-o,dl[1-o,k]]+a[i,j]);
if j<>m then update(stu-x*thr[j-1]-y*thr[j]+y*thr[j],f[1-o,dl[1-o,k]]+a[i,j]);
continue;
end;
if y=0 then
begin
update(stu-x*thr[j-1]-y*thr[j]+x*thr[j-1],f[1-o,dl[1-o,k]]+a[i,j]);
if j<>m then update(stu-x*thr[j-1]-y*thr[j]+x*thr[j],f[1-o,dl[1-o,k]]+a[i,j]);
continue;
end;
if x=1 then
begin
stu:=stu-thr[j-1]-thr[j];
v:=1;
for w:=j+1 to m+1 do
begin
if stu div thr[w] mod 3 = 1 then inc(v);
if stu div thr[w] mod 3 = 2 then dec(v);
if v=0 then
begin
stu:=stu-thr[w];
break;
end;
end;
update(stu,f[1-o,dl[1-o,k]]+a[i,j]);
continue;
end;
if x=2 then
begin
stu:=stu-2*thr[j-1]-2*thr[j];
v:=1;
for w:=j-2 downto 0 do
begin
if stu div thr[w] mod 3 = 2 then inc(v);
if stu div thr[w] mod 3 = 1 then dec(v);
if v=0 then
begin
stu:=stu+thr[w];
break;
end;
end;
update(stu,f[1-o,dl[1-o,k]]+a[i,j]);
continue;
end;
end;
end;
writeln(ans);
end.