You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
598 B
36 lines
598 B
#include <bits/stdc++.h>
|
|
|
|
using namespace std;
|
|
const int INF = 0x3f3f3f3f;
|
|
|
|
const int N = 110;
|
|
int a[N][N];
|
|
int n, m;
|
|
int f[N][N];
|
|
|
|
// dfs解法
|
|
/*
|
|
2 3
|
|
1 3 5
|
|
2 4 6
|
|
|
|
答案:
|
|
13
|
|
*/
|
|
|
|
int dfs(int x, int y) {
|
|
if (f[x][y]) return f[x][y];
|
|
if (x > n || y > m) return INF;
|
|
if (x == n && y == m) return a[n][m];
|
|
return f[x][y] = a[x][y] + min(dfs(x + 1, y), dfs(x, y + 1));
|
|
}
|
|
|
|
int main() {
|
|
cin >> n >> m;
|
|
for (int i = 1; i <= n; i++)
|
|
for (int j = 1; j <= m; j++)
|
|
cin >> a[i][j];
|
|
|
|
printf("%d\n", dfs(1, 1));
|
|
return 0;
|
|
} |