POJ-3669-Meteor Shower
2014-02-20 11:30
459 查看
其实不难的一个BFS题目,写了好久~~最后发现是搜索完后流星还没攻击完的状况忽略了~该死!
代码:
代码:
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<queue> using namespace std; const int maxn=405; const int inf=1<<29; struct Node { int x; int y; int t; bool operator < (const Node &a)const { return t<a.t; } }a[51000]; int n,movex[4]={0,0,-1,1},movey[4]={1,-1,0,0}; queue<Node> q; int dist[maxn][maxn]; bool isok(int x) { if(x<0||x>=maxn) return false; return true; } int main() { while(scanf("%d",&n)!=EOF) { for(int i=0;i<n;i++) scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].t); for(int i=0;i<maxn;i++) for(int j=0;j<maxn;j++) dist[i][j]=inf; sort(a,a+n); Node node; node.x=0; node.y=0; node.t=0; dist[0][0]=0; q.push(node); int index=0; while(!q.empty()) { int x=q.front().x; int y=q.front().y; int t=q.front().t; q.pop(); while(index<n&&a[index].t<=t) { dist[a[index].x][a[index].y]=-1; for(int i=0;i<4;i++) if(isok(a[index].x+movex[i])&&isok(a[index].y+movey[i])) dist[a[index].x+movex[i]][a[index].y+movey[i]]=-1; index++; } if(dist[x][y]==-1) continue; if(index==n) { while(!q.empty()) q.pop(); break; } for(int i=0;i<4;i++) { int itx=x+movex[i]; int ity=y+movey[i]; if(isok(itx)&&isok(ity)&&dist[itx][ity]!=-1&&dist[itx][ity]==inf) { node.x=itx; node.y=ity; node.t=t+1; dist[itx][ity]=t+1; q.push(node); } } } while(index<n) { dist[a[index].x][a[index].y]=-1; for(int i=0;i<4;i++) if(isok(a[index].x+movex[i])&&isok(a[index].y+movey[i])) dist[a[index].x+movex[i]][a[index].y+movey[i]]=-1; index++; } int ans=inf; for(int i=0;i<maxn;i++) for(int j=0;j<maxn;j++) if(dist[i][j]!=-1) ans=min(ans,dist[i][j]); if(ans!=inf) printf("%d\n",ans); else printf("-1\n"); } return 0; }
相关文章推荐
- poj_3669_Meteor Shower(BFS+预处理)
- POJ - 3669 Meteor Shower (广搜+标记)
- Poj 3669 Meteor Shower【bfs】
- POJ 3669 Meteor Shower
- POJ 3669 Meteor Shower
- POJ 3669 Meteor Shower(BFS)
- POJ 3669 Meteor Shower
- Meteor Shower (POJ 3669,广度优先搜索)
- POJ 3669 Meteor Shower 挑战P135
- Meteor Shower(POJ-3669)
- POJ 3669 Meteor Shower
- POJ 3669:Meteor Shower 处处是坑的广搜
- poj 3669 meteor shower
- Poj-3669 Meteor Shower--Bfs经典例题
- Poj 3669 Meteor Shower
- POJ 3669 Meteor Shower(BFS)
- POJ 3669Meteor Shower(广搜)
- POJ 3669 Meteor Shower(BFS)
- BFS——Poj 3669 Meteor Shower
- 【POJ 3669】Meteor Shower