#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int dir[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
int map[30][30];
int w, h;
int sx, sy, ex, ey;
int step;
int Min;
void dfs(int x, int y) {
if(step > 10) return;
int i;
for(i = 0; i < 4; i++) {
int flag = 0;
int nx = x + dir[i][0];
int ny = y + dir[i][1];
while(nx >= 0 && ny >= 0 && nx < h && ny < w && map[nx][ny] != 1) {
flag = 1;
if(nx == ex && ny == ey && step < Min) Min = step;
nx += dir[i][0];
ny += dir[i][1];
}
if(map[nx][ny] == 1 && flag) {
step++;
map[nx][ny] = 0;
dfs(nx - dir[i][0], ny - dir[i][1]);
step--;
map[nx][ny] = 1;
}
}
}
int main() {
while(cin >> w >> h && w && h) {
memset(map, 0, sizeof(map)); //为什么不初始化就过不了?
int i, j;
for(i = 0; i < h; i++) {
for(j = 0; j < w; j++) {
cin >> map[i][j];
if(map[i][j] == 2) {
sx = i;
sy = j;
}
else if(map[i][j] == 3) {
ex = i;
ey = j;
}
}
}
Min = 99999999;
step = 1;
dfs(sx, sy);
cout << (Min > 10 ? -1 : Min) << endl;
}
return 0;
}poj-3009
最新推荐文章于 2020-03-29 13:29:05 发布
636

被折叠的 条评论
为什么被折叠?



