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.

35 lines
665 B

#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 110;
int a[N][N];
int n, m;
// dfs解法
/*
2 3
1 3 5
2 4 6
答案:
13
*/
int dfs(int x, int y) {
if (x == n && y == m) return a[n][m];
if (x + 1 <= n && y + 1 <= m) return a[x][y] + min(dfs(x + 1, y), dfs(x, y + 1));
if (x + 1 <= n && y + 1 > m) return a[x][y] + dfs(x + 1, y);
if (x + 1 > n && y + 1 <= m) return a[x][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;
}