题解
2024-12-08 18:29:28
发布于:广东
*#include <bits/stdc++.h>
using namespace std;
const int N = 30;
char mp[N][N];
int n, m, ans = 2e9;
int sx, sy, ex, ey;
int dir[4][2] = {0,1,1,0,0,-1,-1,0};
bool vis[N][N];
bool in(int x, int y) {
return x >= 1 && x <= n && y >= 1 && y <= m;
}
void DFS(int x, int y, int sum) {
if(x == ex && y == ey) {
if(sum < ans) ans = sum;
return;
}
vis[x][y] = true;
for(int i = 0; i < 4; i++) {
int nx = x + dir[i][0];
int ny = y + dir[i][1];
if(in(nx, ny) && !vis[nx][ny] && mp[nx][ny] != '#') {
if(mp[nx][ny] >= '1' && mp[nx][ny] <= '9')
DFS(nx, ny, sum + (mp[nx][ny] - '0') + 1);
else
DFS(nx, ny, sum + 1);
}
}
vis[x][y] = false;
}
int main() {
cin >> n >> m;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
cin >> mp[i][j];
if(mp[i][j] == 'Z') sx = i, sy = j;
if(mp[i][j] == 'W') ex = i, ey = j;
}
}
DFS(sx, sy, 0);
if(ans == 2e9) cout << "IMPOSSIBLE";
else cout << ans;
return 0;
}*
这里空空如也
有帮助,赞一个