From 91e8a1f75145fba2f5251de08bf1778d4105f693 Mon Sep 17 00:00:00 2001 From: Ren Baoshuo Date: Sun, 15 Nov 2020 15:12:33 +0800 Subject: [PATCH] =?UTF-8?q?P7074=20=E6=96=B9=E6=A0=BC=E5=8F=96=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit R41944428 --- problem/P7074/P7074.cpp | 35 +++++++++++++++++++++++++++++++++++ problem/P7074/solution.md | 11 +++++++++++ 2 files changed, 46 insertions(+) create mode 100644 problem/P7074/P7074.cpp create mode 100644 problem/P7074/solution.md diff --git a/problem/P7074/P7074.cpp b/problem/P7074/P7074.cpp new file mode 100644 index 00000000..fbb4784f --- /dev/null +++ b/problem/P7074/P7074.cpp @@ -0,0 +1,35 @@ +#include + +using namespace std; + +int n, m; +long long w[1005][1005], f[1005][1005][2]; + +long long dfs(int x, int y, int from) { + if (x < 1 || x > n || y < 1 || y > m) { + return -0x3f3f3f3f; + } + if (f[x][y][from] != -0x3f3f3f3f) { + return f[x][y][from]; + } + if (from == 0) { + f[x][y][from] = max({dfs(x + 1, y, 0), dfs(x, y - 1, 0), dfs(x, y - 1, 1)}) + w[x][y]; + } + else { + f[x][y][from] = max({dfs(x - 1, y, 1), dfs(x, y - 1, 0), dfs(x, y - 1, 1)}) + w[x][y]; + } + return f[x][y][from]; +} + +int main() { + cin >> n >> m; + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= m; j++) { + cin >> w[i][j]; + f[i][j][0] = f[i][j][1] = -0x3f3f3f3f; + } + } + f[1][1][0] = f[1][1][1] = w[1][1]; + cout << dfs(n, m, 1) << endl; + return 0; +} diff --git a/problem/P7074/solution.md b/problem/P7074/solution.md new file mode 100644 index 00000000..a3fe48af --- /dev/null +++ b/problem/P7074/solution.md @@ -0,0 +1,11 @@ +## 思路 + ++ 算法:记忆化搜索 + +设 $F_{i,j,0}$ 表示从一个格子上方走到该格子的路径最大和,$F_{i,j,1}$ 表示从一个格子下方走到该格子的路径最大和。 + +若搜到以前搜过的状态则直接返回搜过的最大和(也就是 $F$ 中的值),否则继续搜索到达该格时的最大和。 + +## 代码 + +见 `P7074.cpp` 。