您的位置:首页 > 其它

Vijos P1493 传纸条 双线程DP

2014-05-15 10:41 232 查看
#include <stdio.h>
#include <string.h>
#include <iostream>
#include<functional>
#include <queue>
#include <string>
#include <algorithm>
using namespace std;
const int maxn = 55;
const int inf = 1<<30;
int n,m;
int map[maxn][maxn],dp[maxn][maxn][maxn][maxn];
int Max( int a,int b,int c,int d )
{
if( a > b && a > c && a > d ) return a;
if( b > a && b > c && b > d ) return b;
if( c > b && c > a && c > d ) return c;
return d;
}
int main()
{
scanf("%d%d",&n,&m);
for( int i = 1; i <= n; i ++ )
for( int j = 1; j <= m; j ++ )
scanf("%d",&map[i][j]);
for( int i = 1; i <= n; i ++ ){
for( int j = 1; j <= m; j ++ ){
for( int x = 1; x < i; x ++ ){
int y = i + j - x;
dp[i][j][x][y] =   Max( dp[i-1][j][x-1][y],dp[i-1][j][x][y-1],dp[i][j-1][x-1][y],dp[i][j-1][x][y-1] ) + map[i][j] + map[x][y];
}
}
}
printf("%d\n",dp
[m-1][n-1][m]);
return 0;
}


#include <stdio.h>
#include <string.h>
#include <iostream>
#include<functional>
#include <queue>
#include <string>
#include <algorithm>
using namespace std;
const int maxn = 55;
const int inf = 1<<30;
int n,m;
int map[maxn][maxn],dp[maxn][maxn][maxn][maxn];
bool vis[maxn][maxn][maxn][maxn];
int Max( int a,int b,int c,int d )
{
if( a < b ) a = b;
if( a < c ) a = c;
if( a < d ) a = d;
return a;
}
int dfs( int a,int b,int c,int d )
{
if( a < 1 || a > n || b < 1 || b > m || c < 1 || c > n || d < 1 || d > m ) //判断是否越界
{
return 0;
}
if( vis[a][b][c][d] == 1 )
return dp[a][b][c][d];
vis[a][b][c][d] = 1;
dp[a][b][c][d] = Max( dfs( a-1,b,c-1,d ),dfs( a-1,b,c,d-1 ),dfs( a,b-1,c-1,d ),dfs( a,b-1,c,d-1 ) ) + map[a][b] + map[c][d];
return dp[a][b][c][d];
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("data.txt","r",stdin);
#endif
scanf("%d%d",&n,&m);
for( int i = 1; i <= n; i ++ ){
for( int j = 1; j <= m; j ++ ){
scanf("%d",&map[i][j]);
vis[i][j][i][j] = 1;
}
}
printf("%d\n",dfs(n,m-1,n-1,m));
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: