简单迷宫题,貌似哪里写过,So,一遍AC
考点貌似在判断上,或者说,没有。。
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
int vis[31][31];
int walls[60][31];
int w, h;
int dir[4][2] = {
1, 0, -1, 0, 0, 1, 0, -1
};
int main()
{
cin >> w >> h;
while(w){
memset(vis, 0, sizeof(vis));
memset(walls, 0, sizeof(walls));
for(int i = 1; i <= 2*h-1; i++)
if(i%2){
for(int j = 1; j <= w - 1; j++)
cin >> walls[i][j];
}else
for(int j = 1; j <= w; j++)
cin >> walls[i][j];
vis[1][1] = 1;
queue<short> qw;
queue<short> qh;
qw.push(1); qh.push(1);
while(!qw.empty()){
short x = qh.front(), y = qw.front(), step = vis[x][y];
qw.pop(); qh.pop();
if(x == h && y == w)
break;
for(int i = 0; i < 4; i++){
short newx = x + dir[i][0], newy = y + dir[i][1];
if(newx >= 1 && newx <= h && newy >= 1 && newy <= w){
short tempx = x < newx ? x : newx;
short wallx = x == newx ? 2*(tempx-1)+1 : 2*tempx;
short wally = y < newy ? y : newy;
int newstep = step+1;
if(!walls[wallx][wally]){
if(!vis[newx][newy]){
vis[newx][newy] = newstep;
qw.push(newy);
qh.push(newx);
}else if(newstep < vis[newx][newx]){
vis[newx][newy] = newstep;
}
}
}
}
}
if(vis[h][w])
cout << vis[h][w] << endl;
else
cout << "0" << endl;
cin >> w >> h;
}
}