【棋盘类DP】传纸条

http://codevs.cn/problem/1169/

#include<iostream>
using namespace std;

int a[100][100];
int dp[55][55][55][55];

int main()
{
	int n,m;
	cin>>n>>m;
	for (int i=1;i<=n;i++)
	for (int j=1;j<=m;j++)
	cin>>a[i][j];
	for (int x1=1;x1<=n;x1++)
	for (int y1=1;y1<=m;y1++)
	for (int x2=1;x2<=n;x2++)
	for (int y2=1;y2<=m;y2++)
	{
		dp[x1][y1][x2][y2]=max(dp[x1][y1][x2][y2],dp[x1-1][y1][x2-1][y2]);
		dp[x1][y1][x2][y2]=max(dp[x1][y1][x2][y2],dp[x1-1][y1][x2][y2-1]);
		dp[x1][y1][x2][y2]=max(dp[x1][y1][x2][y2],dp[x1][y1-1][x2-1][y2]);
		dp[x1][y1][x2][y2]=max(dp[x1][y1][x2][y2],dp[x1][y1-1][x2][y2-1]);
		dp[x1][y1][x2][y2]+=a[x1][y1];
		if (x1==x2&&y1==y2&&((x1==n&&y1==m)||(x1==1&&y1==1)))continue;
		else if (x1==x2&&y1==y2)
		dp[x1][y1][x2][y2]=0;
		else dp[x1][y1][x2][y2]+=a[x2][y2];
	}
	cout<<dp[n][m][n][m];
}


你可能感兴趣的:(dp,动态规划,棋盘形DP)