标签:幸运 代码 没有 蓝桥杯 ++ 空格 splay 优化 二维
1 #include <iostream> 2 #include<stdio.h> 3 #include<cmath> 4 using namespace std; 5 6 #define MAX_NUM 52 7 8 int map[MAX_NUM][MAX_NUM]; //好心程度 | 权值 9 int dp[MAX_NUM][MAX_NUM][MAX_NUM][MAX_NUM]; 10 11 int maxPath(int m, int n) 12 { 13 for (int x1 = 1; x1 <= m; x1++) 14 { 15 for (int y1 = 1; y1 <= n; y1++) 16 { 17 for (int x2 = 1; x2 <= m; x2++) 18 { 19 for (int y2 = 1; y2 <= n; y2++) 20 { 21 /* 22 如果第一个人没有走到最后一行或最后一列,并且两个人没有重复 23 因为走到最后一行或最后一列,容易造成第二个人无路可走的情况 24 */ 25 if ((x1 < m || y1 < n) && x1 == x2 && y1 == y2) 26 { 27 continue; 28 } 29 dp[x1][y1][x2][y2] = max( max(dp[x1-1][y1][x2-1][y2], dp[x1-1][y1][x2][y2-1]), 30 max(dp[x1][y1-1][x2-1][y2], dp[x1][y1-1][x2][y2-1])) 31 + map[x1][y1] + map[x2][y2]; 32 } 33 } 34 } 35 } 36 return dp[m][n][m][n]; 37 } 38 39 int main() 40 { 41 int m, n; 42 scanf("%d%d", &m, &n); 43 44 for (int i = 1;i <= m; i++) 45 for (int j = 1;j <= n; j++) 46 scanf("%d", &map[i][j]); 47 48 int ans = maxPath(m, n); 49 printf("%d\n", ans); 50 51 return 0; 52 }
1 #include <iostream> 2 #include<cmath> 3 using namespace std; 4 5 #define MAX_NUM 52 6 7 int map[MAX_NUM][MAX_NUM]; //好心程度 | 权值 8 int dp[MAX_NUM+MAX_NUM][MAX_NUM][MAX_NUM]; 9 10 int maxPath(int m, int n) 11 { 12 for (int k = 1;k <= m+n-3; k++) 13 { 14 for (int x1 = 0; x1 <= k; x1++) 15 { 16 for (int x2 = 0; x2 <= k; x2++) 17 { 18 if (x1 == x2) //x1 == x2 相当于(x1 == x2 && y1 = y2) 19 { 20 continue; 21 } 22 dp[k][x1][x2] = max(max(dp[k-1][x1][x2], dp[k-1][x1-1][x2-1]), 23 max(dp[k-1][x1-1][x2], dp[k-1][x1][x2-1])) 24 + map[x1][k-x1] + map[x2][k-x2]; 25 } 26 } 27 } 28 return dp[m+n-3][m-1][m-2]; 29 } 30 31 int main() 32 { 33 int m, n; 34 scanf("%d%d", &m, &n); 35 36 for (int i = 0; i < m; i++) 37 for (int j = 0; j < n; j++) 38 scanf("%d", &map[i][j]); 39 40 int ans = maxPath(m, n); 41 printf("%d\n", ans); 42 43 return 0; 44 }
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 using namespace std; 5 6 #define MAX_NUM 52 7 8 int map[MAX_NUM][MAX_NUM]; //好心程度 | 权值 9 int dp[MAX_NUM][MAX_NUM]; 10 11 int maxPath(int m, int n) 12 { 13 14 for (int k = 1; k <= m+n-3; k++) 15 { 16 for (int x1 = m-1; x1 >= 0; x1--) 17 { 18 for (int x2 = m-1; x2 > x1; x2--) 19 { 20 if ( k >= x1 && k >= x2) //x + y = k,当k >= x时,说明还在矩阵范围之内 21 { 22 dp[x1][x2] = max(max(dp[x1][x2], dp[x1-1][x2-1]), 23 max(dp[x1-1][x2], dp[x1][x2-1])) 24 + map[x1][k-x1] + map[x2][k-x2]; 25 } 26 } 27 } 28 } 29 return dp[m-2][m-1]; 30 } 31 32 int main() 33 { 34 int m, n; 35 scanf("%d %d", &m, &n); 36 37 for (int i = 0;i < m; i++) 38 for (int j = 0; j < n; j++) 39 scanf("%d", &map[i][j]); 40 41 int ans = maxPath(m, n); 42 printf("%d\n", ans); 43 44 return 0; 45 }
标签:幸运 代码 没有 蓝桥杯 ++ 空格 splay 优化 二维
原文地址:http://www.cnblogs.com/cao-lei/p/7236061.html