XDU 1196

来源:互联网 发布:环信 java 服务端 编辑:程序博客网 时间:2024/06/01 15:32

简单迷宫题,貌似哪里写过,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];}elsefor(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;elsecout << "0" << endl;cin >> w >> h;}} 


原创粉丝点击