poj 2964 双进程DP
又一道双进程DP,状态转移方程如下:
f(k,i,j)=max(f(k-1,i,j),f(k-1,i-1,j),f(k-1,i,j-1),f(k-1,i-1,j-1))
然后f(k,i,j)加上(i,k-i)和(j,k-j)上的interesting locations。注意i和j相等的情况。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int map[110][110];
int res[211][110][110];
inline int max(int a,int b,int c,int d)
{
int s,t;
s = a > b ? a : b;
t = c > d ? c : d;
return s > t ? s : t;
}
int work(int n,int m)
{
int k,x1,x2;
res[2][1][1]=map[1][1];
for(k=3;k<=n+m;k++)
{
for(x1=1;x1<=n;x1++)
{
if(k-x1 < 1 || k-x1 > m) continue;
for(x2=1;x2<=n;x2++)
{
if(k-x2 < 1 || k-x2 > m) continue;
if(map[x1][k-x1]==2 || map[x2][k-x2]==2) continue;
res[k][x1][x2]=max(res[k-1][x1][x2],res[k-1][x1-1][x2],res[k-1][x1][x2-1],res[k-1][x1-1][x2-1]);
res[k][x1][x2]+=map[x1][k-x1];
if(x1!=x2) res[k][x1][x2]+=map[x2][k-x2];
}
}
}
return res[n+m][n][n];
}
int main()
{
int i,j,m,n,Case;
char c;
scanf("%d",&Case);
while(Case--)
{
memset(res,0,sizeof(res));
scanf("%d %d",&m,&n);
getchar();
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
c=getchar();
if(c=='.') map[i][j]=0;
if(c=='*') map[i][j]=1;
if(c=='#') map[i][j]=2;
}
getchar();
}
printf("%d\n",work(n,m));
}
return 0;
}