Files
python/TangDou/LanQiaoBei/LanQiao14STEMA202208/4_dfs_void_1.cpp

46 lines
755 B
C++
Raw Normal View History

2025-08-30 18:35:01 +08:00
#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;
}