#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n, m, a[15][15], dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
ll sx, sy, ex, ey;
ll ans;
bool vis[15][15][10];
void dfs(ll x, ll y, ll step, ll hp){
if(hp <= 0) return;
if(x < 1 || x > n || y < 1 || y > m) return;
if(a[x][y] == 0) return;
if(step >= ans) return;
if(vis[x][y][hp]) return;
if(x == ex && y == ey){
if(hp >= 1) ans = min(ans, step);
return;
}
vis[x][y][hp] = true;
ll new_hp = (a[x][y] == 4 ? 5 : hp - 1);
if(new_hp <= 0) return;
for(ll i = 0; i < 4; i++){
ll xx = x + dir[i][0];
ll yy = y + dir[i][1];
dfs(xx, yy, step + 1, new_hp);
}
vis[x][y][hp] = false;
}
int main(){
cin >> n >> m;
ans = LLONG_MAX;
for(ll i = 1; i <= n; i++){
for(ll j = 1; j <= m; j++){
cin >> a[i][j];
if(a[i][j] == 2) sx = i, sy = j;
if(a[i][j] == 3) ex = i, ey = j;
}
}
memset(vis, false, sizeof(vis));
dfs(sx, sy, 0, 6);
cout << (ans == LLONG_MAX ? -1 : ans) << endl;
return 0;
}
这么~可爱~的代码怎么会错???
重点!!!
因为我任性!所有必须用dfs!
调对者关注!!!