【动态规划】 51nod 1084 矩阵来回取数
通道
思路:dp[i][j][k]:k步时,A到i行,B到j行的最大值
代码:
#include <cstdio> #include <vector> using namespace std; int maxPath(vector<vector<int> > &num) { const int m = num.size(); const int n = num[0].size(); const int l = m + n - 1; vector<vector<int> > g(m + 1, vector<int>(m + 1)); vector<vector<int> > h(m + 1, vector<int>(m + 1)); for (int k = 1; k <= l; k++) { int st = k <= n ? 1 : k - n + 1; int ed = k <= m ? k : m; for (int i = st; i <= ed; i++) for (int j = i; j <= ed; j++) { h[i][j] = num[i - 1][k - i] +max(max(g[i - 1][j - 1], g[i - 1][j]), max(g[i][j - 1], g[i][j])); if (i != j) h[i][j] += num[j - 1][k - j]; } swap(g, h); } return g[m][m]; } int main() { int m, n; scanf("%d%d", &m, &n); vector<vector<int> > num(n, vector<int>(m)); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) scanf("%d", &num[i][j]); printf("%d\n", maxPath(num)); return 0; }