poj 2049 Finding Nemo
来源:互联网 发布:结婚日子测算软件 编辑:程序博客网 时间:2024/05/20 21:47
bfs的水题,主要是理解题意。
题意:有一个迷宫,告诉你由墙和门组成,墙不能走,门可以走,人的初始位置不会在墙和门上,求nemo走出迷宫所需步数。
思路:从起始点开始bfs,最后判断一下如果初始位置是否在1-199。
#include <cstdio>#include <iostream>#include <cmath>#include <queue>#include <algorithm>#include <cstring>using namespace std;const int inf = 0x3f3f3f3f;const int MAXN = 210;typedef pair <int, int> Pii;int G[MAXN][MAXN][2];int dist[MAXN][MAXN];bool vis[MAXN][MAXN];int ans;bool judge(int x, int y){ if(G[x][y][1]==0||G[x][y][0]==0||G[x+1][y][1]==0||G[x][y+1][0]==0) return true; return false;}void bfs(int x, int y){ int i, vx, vy; queue <Pii> q; q.push(Pii(x, y)); vis[x][y]=1; while(!q.empty()) { Pii p = q.front(); q.pop(); vx=p.first; vy=p.second; vis[vx][vy]=1; if(judge(p.first,p.second)) { ans=min(dist[vx][vy], ans); return; } for(i=0; i<4; ++i) { if(i==0) { if(G[vx][vy][1]==2&&!vis[vx-1][vy]) { dist[vx-1][vy]=dist[vx][vy]+1; q.push(Pii(vx-1,vy)); } } if(i==1) { if(G[vx][vy][0]==2&&!vis[vx][vy-1]) { dist[vx][vy-1]=dist[vx][vy]+1; q.push(Pii(vx,vy-1)); } } if(i==2) { if(G[vx+1][vy][1]==2&&!vis[vx+1][vy]) { dist[vx+1][vy]=dist[vx][vy]+1; q.push(Pii(vx+1,vy)); } } if(i==3) { if(G[vx][vy+1][0]==2&&!vis[vx][vy+1]) { dist[vx][vy+1]=dist[vx][vy]+1; q.push(Pii(vx,vy+1)); } } } }}int main (){ int n, m; int x, y, dir, l; while(scanf("%d %d", &n,&m), n!=-1&&m!=-1) { memset(G, 0, sizeof(G)); for(int i=1; i<=n; ++i) { scanf("%d %d %d %d", &x, &y, &dir, &l); if(dir==1) { for(int j=0; j<l; ++j) { G[x][y+j][dir]=1; } } else { for(int j=0; j<l; ++j) { G[x+j][y][dir]=1; } } } for(int i=1; i<=m; ++i) { scanf("%d %d %d", &x, &y, &dir); if(dir==1) { G[x][y][dir]=2; } else { G[x][y][dir]=2; } } double nx, ny; scanf("%lf %lf", &nx, &ny); x=(int)nx, y=(int)ny; memset(vis, 0, sizeof(vis)); memset(dist, 0, sizeof(dist)); ans=inf; if(x>=200||y>=200||x<1||y<1) x=201,y=201; bfs(x, y); if(ans==inf) printf("-1\n"); else printf("%d\n", ans); } return 0;}
0 0
- POJ 2049 Finding Nemo
- POJ 2049 Finding Nemo
- POJ 2049 Finding Nemo
- POJ 2049 Finding Nemo
- POJ 2049 Finding Nemo
- POJ 2049 Finding Nemo
- POJ 2049 Finding Nemo
- POJ 2049 Finding Nemo
- 【POJ 2049】Finding Nemo
- poj 2049 Finding Nemo
- Finding Nemo(POJ--2049
- POJ 2049 Finding Nemo
- poj 2049 Finding Nemo
- POJ-2049-Finding Nemo
- POJ-2049 Finding Nemo
- poj 2049 Finding Nemo
- poj 2049 Finding Nemo
- poj 2049 Finding Nemo
- 阿里在线测评
- 面试题整理_Activity的奇葩生命周期
- Kali Rolling版本下的open-vm-tools安装
- 2017.3.4 pat甲级D题ZigZagging on a Tree
- nginx认证服务搭建
- poj 2049 Finding Nemo
- Floyd最短路径算法
- PHP页面静态化
- Matlab各种疑难
- Java开发中错误集合
- Just for fun 计算机英文词汇说明
- Java中的内部接口
- 通讯协议特别篇
- 锚点