poj2049Finding Nemo(BFS+优先队列)
2012-07-31 18:22
253 查看
http://poj.org/problem?id=2049
与一般的BFS不大一样 是把网格转换成我们常用的点
以左下角的点表示整个方格 用优先队列来保证每次搜的都是最小的 注意Nemo的位置有可能不在墙内
之前把sx看成tx一直调试不出来 ,,纠结了一天 第二天一眼看出它俩不一样。。改完之后 找了找结构体的优先队列怎么用 按知道上回答写的 一直WA 后来看人家的解题报告 发现不是那么用的,改过来交上AC。。
View Code
与一般的BFS不大一样 是把网格转换成我们常用的点
以左下角的点表示整个方格 用优先队列来保证每次搜的都是最小的 注意Nemo的位置有可能不在墙内
之前把sx看成tx一直调试不出来 ,,纠结了一天 第二天一眼看出它俩不一样。。改完之后 找了找结构体的优先队列怎么用 按知道上回答写的 一直WA 后来看人家的解题报告 发现不是那么用的,改过来交上AC。。
View Code
#include<stdio.h> #include<string.h> #include<iostream> #include<queue> using namespace std; typedef struct node { int num,x,y; friend bool operator<( struct node a, struct node b ) { return a.num>b.num; } }st; int p[2][301][301],pt[2][300][300],pf[300][300]; st ne,na; int main() { int n,m,i,j,x,y,t,d,g; float x1,y1; while(cin>>n>>m) { if(n==-1&&m==-1) break; memset(p,0,sizeof(p)); memset(pf,0,sizeof(pf)); memset(pt,0,sizeof(pt)); int f = 1; int minx=300,miny = 300,maxx = -1,maxy = -1; for(i = 1; i <= n ; i++) { cin>>x>>y>>t>>d; if(x<minx) minx = x; if(y<miny) miny = y; if(t==0) { if(maxx<x+d) maxx = x+d; if(maxy<y) maxy = y; for(g = x ; g < x+d ; g++) p[0][g][y] = 1; } else { if(maxy<y+d) maxy = y+d; if(maxx<x) maxx = x; for(g = y ;g < y+d ; g++) p[1][x][g] = 1; } } for(i = 1; i <= m ; i++) { cin>>x>>y>>t; if(maxx<x) maxx = x; if(maxy<y) maxy = y; if(minx>x) minx = x; if(miny>y) miny = y; pt[t][x][y] = 1; } scanf("%f%f", &x1,&y1); int sx = (int)x1; int sy = (int)y1; int flag = 0; if(sx>maxx||sx<minx||sy>maxy||sy<miny) f = 0; else { priority_queue <st> q; ne.num = 0; ne.x = sx; ne.y = sy; q.push(ne); pf[sx][sy]=1; while(!q.empty()) { na = q.top(); int tx = na.x; int ty = na.y; q.pop(); if(tx>=maxx||tx<minx||ty>=maxy||ty<miny) { flag = 1; break; } if((pt[1][tx][ty]||!p[1][tx][ty])&&tx-1>=0&&!pf[tx-1][ty]) { pf[tx-1][ty] = 1; if(pt[1][tx][ty]) ne.num = na.num+1; else ne.num = na.num; ne.x = tx-1; ne.y = ty; q.push(ne); } if((pt[1][tx+1][ty]||!p[1][tx+1][ty])&&!pf[tx+1][ty]) { pf[tx+1][ty] = 1; if(pt[1][tx+1][ty]) ne.num = na.num+1; else ne.num = na.num ; ne.x = tx+1; ne.y = ty; q.push(ne); } if((pt[0][tx][ty+1]||!p[0][tx][ty+1])&&!pf[tx][ty+1]) { pf[tx][ty+1] = 1; if(pt[0][tx][ty+1]) ne.num = na.num+1; else ne.num = na.num; ne.x = tx; ne.y = ty+1; q.push(ne); } if((pt[0][tx][ty]||!p[0][tx][ty])&&ty-1>=0&&!pf[tx][ty-1]) { pf[tx][ty-1] = 1; if(pt[0][tx][ty]) ne.num = na.num+1; else ne.num = na.num ; ne.x = tx; ne.y = ty-1; q.push(ne); } } } if(!f||!flag) { if(!f) cout<<"0"<<endl; else cout<<"-1"<<endl; } else cout<<na.num<<endl; } return 0; }
相关文章推荐
- POJ2049——Finding Nemo
- poj2049--Finding Nemo
- poj2049 Finding Nemo
- poj2049(bfs) Finding Nemo
- poj2049Finding Nemo
- POJ 2049 Finding Nemo (bfs+优先队列)
- POJ:2049Finding Nemo(bfs+优先队列)
- poj 2049 Finding Nemo(bfs+优先队列)
- poj2049 Finding Nemo BFS
- poj2049 Finding Nemo(建图+BFS+优先队列+特判)
- 【bfs+优先队列】POJ2049-Finding Nemo
- poj2049 Finding Nemo(优先队列BFS)
- poj2049 - Finding Nemo
- hdu 1026 Ignatius and the Princess I(bfs+优先队列)
- poj 2049 Finding Nemo(bfs)
- hdu 1242 Rescue_bfs+优先队列
- POJ 2049 Finding Nemo
- POJ 2049 Finding Nemo (网格中的BFS)
- uva 11573 - Ocean Currents(bfs+优先队列)
- hdu--1242--bfs+优先队列