#include<iostream>
#include<cstdio>
#include<cstring>
#define MAX 30
using namespace std;
int map[MAX][MAX];
const int dir[4][2]={-1, 0, 1, 0, 0, -1, 0, 1};
int h, w, ans;
void dfs(int x, int y, int steps){
if(steps > ans || steps > 10 || map[x][y] == -1)
return;
for(int i = 0; i < 4; i ++){
int xx = x;
int yy = y;
if(map[xx + dir[i][0]][yy + dir[i][1]] != 1){
while(map[xx + dir[i][0]][yy + dir[i][1]] != 1){
xx += dir[i][0];
yy += dir[i][1];
if(map[xx][yy] == -1)
break;
if(map[xx][yy] == 3){
if(ans > steps + 1) ans = steps + 1;
return;
}
}
if(map[xx + dir[i][0]][yy + dir[i][1]] == 1){
map[xx + dir[i][0]][yy + dir[i][1]] = 0;
dfs(xx, yy, steps + 1);
map[xx + dir[i][0]][yy + dir[i][1]] = 1;
}
}
}
}
int main(){
int st, end;
/* freopen("in.c", "r", stdin); */
while(cin >> w >> h && (h + w)){
memset(map, -1, sizeof(map));
for(int i = 1; i <= h; i ++){
for(int j = 1; j <= w; j ++){
cin >> map[i][j];
if(map[i][j] == 2){
st = i;
end = j;
}
}
}
ans = 1 << 29;
dfs(st, end, 0);
if(ans > 10) ans = -1;
cout << ans << endl;
}
return 0;
}
POJ 3009
最新推荐文章于 2022-04-11 09:26:15 发布