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; }
相关文章推荐
- Vijos P1493 传纸条(动态规划,双线程DP)
- Vijos P1493 传纸条 (双线程DP)
- vijos 1493 双线程DP
- NYOJ 61 传纸条(一)(双线程dp模板)
- vijos P1493 传纸条 多线程dp
- 蓝桥杯 - 传纸条 (双线程DP)
- Vijos 1493 传纸条(DP)
- nyist 61 传纸条(一)(双线程DP)
- 传纸条(一)(双线程dp)
- 双线程DP (三维法四维法) sdnu 1032.机器人 1194.传纸条
- NYOJ 712 探寻宝藏 &NYOJ 61 传纸条 双线程DP
- nyoj 61 传纸条(一)【 双线程 DP】
- Vijos P1493 传纸条
- TYVJ 1011 传纸条(双线程DP)
- VIJOS P1493 传纸条
- NOIP 2008 传纸条题解[双线程DP]
- nyist 61 传纸条 nyist 712 探 寻 宝 藏(双线程dp问题)
- NOIP 2008 传纸条题解[双线程DP]
- nyoj 61 传纸条(一)双线程DP
- C语言动态规划(6)___传纸条(Vijos P1493)