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.

46 lines
755 B

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