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.

39 lines
700 B

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 110;
int a[N][N];
int n, m;
int res = INF;
void dfs(int x, int y, int sum) {
if (x == n && y == m) {
res = min(res, sum);
return;
}
if (x + 1 <= n)
dfs(x + 1, y, sum + a[x + 1][y]);
if (y + 1 <= m)
dfs(x, y + 1, sum + a[x][y + 1]);
}
/*
2 3
1 3 5
2 4 6
13
*/
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> a[i][j];
dfs(1, 1, a[1][1]);
printf("%d\n", res);
return 0;
}