0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-27 20:36:28 +00:00

P7074 方格取数

R41944428
This commit is contained in:
Baoshuo Ren 2020-11-15 15:12:33 +08:00 committed by Baoshuo Ren
parent 10a13692b5
commit 91e8a1f751
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68
2 changed files with 46 additions and 0 deletions

35
problem/P7074/P7074.cpp Normal file
View File

@ -0,0 +1,35 @@
#include <bits/stdc++.h>
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;
}

11
problem/P7074/solution.md Normal file
View File

@ -0,0 +1,11 @@
## 思路
+ 算法:记忆化搜索
设 $F_{i,j,0}$ 表示从一个格子上方走到该格子的路径最大和,$F_{i,j,1}$ 表示从一个格子下方走到该格子的路径最大和。
若搜到以前搜过的状态则直接返回搜过的最大和(也就是 $F$ 中的值),否则继续搜索到达该格时的最大和。
## 代码
`P7074.cpp`