#include <bits/stdc++.h>
using namespace std;
int arr[12][12];
int n, m, min123 =999;
void dfs(int x, int y, int times, int hp){
if(hp == 0) return;
if(x>=n || y>=m || x<0 || y<0) return;
if(arr[x][y] == 0) return;
if(arr[x][y] == 3){
if(times < min123) min123 = times;
return;
}
if(arr[x][y] == 4){
arr[x][y] = 1;
dfs(x+1, y, times+1, 5);
dfs(x-1, y, times+1, 5);
dfs(x, y+1, times+1, 5);
dfs(x, y-1, times+1, 5);
arr[x][y] = 4;
return;
}
dfs(x+1, y, times+1, hp-1);
dfs(x-1, y, times+1, hp-1);
dfs(x, y+1, times+1, hp-1);
dfs(x, y-1, times+1, hp-1);
return;
}
int main(){
cin>> n>> m;
int startn, startm;
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
cin>> arr[i][j];
if(arr[i][j] == 2){
startm = i;
startn = j;
arr[i][j] = 1;
}
}
}
dfs(startm, startn, 0, 6);
if(min123 == 999) min123 = -1;
cout<< min123;
return 0;
}
另外想问一下,bits/stdc++.h这个在noip可以使用吗0. 0?