bfs--poj3669
2013-09-20 19:16
274 查看
Language:
Default
Meteor Shower
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 6297
Accepted: 1857
Description
Bessie hears that an extraordinary meteor shower is coming; reports say that these meteors will crash into earth and destroy anything they hit. Anxious for her safety, she vows to find her way to a safe location (one that is never destroyed by a meteor)
. She is currently grazing at the origin in the coordinate plane and wants to move to a new, safer location while avoiding being destroyed by meteors along her way.
The reports say that M meteors (1 ≤ M ≤ 50,000) will strike, with meteor i will striking point (Xi, Yi)
(0 ≤ Xi ≤ 300; 0 ≤ Yi ≤ 300) at time Ti (0 ≤ Ti ≤ 1,000). Each meteor destroys the point
that it strikes and also the four rectilinearly adjacent lattice points.
Bessie leaves the origin at time 0 and can travel in the first quadrant and parallel to the axes at the rate of one distance unit per second to any of the (often 4) adjacent rectilinear points that are not yet destroyed by a meteor. She cannot be located
on a point at any time greater than or equal to the time it is destroyed).
Determine the minimum time it takes Bessie to get to a safe place.
Input
* Line 1: A single integer: M
* Lines 2..M+1: Line i+1 contains three space-separated integers: Xi, Yi, and Ti
Output
* Line 1: The minimum time it takes Bessie to get to a safe place or -1 if it is impossible.
Sample Input
Sample Output
[code]5
这个需要稍微想一下,对个点进行预处理,这一点跟周围四个的值为陨石落下的时间,然后bfs,要是时间小于原来的值就可以走。
下面是AC代码:
下面这个超时,但感觉跟上面的差不多
Default
Meteor Shower
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 6297
Accepted: 1857
Description
Bessie hears that an extraordinary meteor shower is coming; reports say that these meteors will crash into earth and destroy anything they hit. Anxious for her safety, she vows to find her way to a safe location (one that is never destroyed by a meteor)
. She is currently grazing at the origin in the coordinate plane and wants to move to a new, safer location while avoiding being destroyed by meteors along her way.
The reports say that M meteors (1 ≤ M ≤ 50,000) will strike, with meteor i will striking point (Xi, Yi)
(0 ≤ Xi ≤ 300; 0 ≤ Yi ≤ 300) at time Ti (0 ≤ Ti ≤ 1,000). Each meteor destroys the point
that it strikes and also the four rectilinearly adjacent lattice points.
Bessie leaves the origin at time 0 and can travel in the first quadrant and parallel to the axes at the rate of one distance unit per second to any of the (often 4) adjacent rectilinear points that are not yet destroyed by a meteor. She cannot be located
on a point at any time greater than or equal to the time it is destroyed).
Determine the minimum time it takes Bessie to get to a safe place.
Input
* Line 1: A single integer: M
* Lines 2..M+1: Line i+1 contains three space-separated integers: Xi, Yi, and Ti
Output
* Line 1: The minimum time it takes Bessie to get to a safe place or -1 if it is impossible.
Sample Input
4 0 0 2 2 1 2 1 1 2 0 3 5
Sample Output
[code]5
这个需要稍微想一下,对个点进行预处理,这一点跟周围四个的值为陨石落下的时间,然后bfs,要是时间小于原来的值就可以走。
下面是AC代码:
#include<iostream> #include<cstdio> #include<cstring> #include<queue> #define bug puts("here"); using namespace std; int vis[330][330]; int fx[]={0,0,0,1,-1}; int fy[]={0,1,-1,0,0}; struct Node{ int x,y,t; }; queue<Node>que; int bfs(){ int k; Node tmp,now,nx; if(vis[0][0]==0) return -1; else if(vis[0][0]==-1) return 0; tmp.x=tmp.y=tmp.t=0; que.push(tmp); while(!que.empty()){ now=que.front(); que.pop(); for(k=1;k<5;k++){ nx.x=now.x+fx[k]; nx.y=now.y+fy[k]; nx.t=now.t+1; if(nx.x<0 || nx.y<0) continue; if(vis[nx.x][nx.y]==-1) return nx.t; if(nx.t>=vis[nx.x][nx.y]) continue; vis[nx.x][nx.y]=0; que.push(nx); } } return -1; } int main(){ int m,k,t,x,y,xx,yy; scanf("%d",&m); memset(vis,-1,sizeof(vis)); while(m--){ scanf("%d%d%d",&x,&y,&t); for(k=0;k<5;k++){ xx=x+fx[k]; yy=y+fy[k]; if(xx<0 || yy<0) continue; if(vis[xx][yy]==-1) vis[xx][yy]=t; else vis[xx][yy]=min(vis[xx][yy],t); } } printf("%d\n",bfs()); return 0; }
下面这个超时,但感觉跟上面的差不多
#include<iostream> #include<queue> #include<cstdio> #include<cstring> using namespace std; const int INF=10000000; int map[405][405]; bool vis[405][405]; int dx[]= {0,0,1,-1,0}; int dy[]= {1,-1,0,0,0}; int max1; struct node { int x,y,time; }; void bfs() { int ans=-1; queue<node> q; node a,b; a.x=a.y=a.time=0; q.push(a); while(!q.empty()) { a=q.front(); q.pop(); if(map[a.x][a.y]==INF) { ans=a.time; break; } for(int i=0; i<5; i++) { b.x=a.x+dx[i],b.y=a.y+dy[i]; if((a.time+1<map[b.x][b.y])&&b.x>=0&&b.y>=0) { b.time=a.time+1; q.push(b); } } } cout<<ans<<endl; } int main() { //freopen("in.txt","r",stdin); int M; int x,y,t; while(scanf("%d",&M)!=EOF) { for(int i=0; i<=310; i++) fill(map[i],map[i]+310,INF); for(int i=0; i<M; i++) { scanf("%d%d%d",&x,&y,&t); map[x][y]=t; for(int j=0; j<4; j++) { int tx=x+dx[j],ty=y+dy[j]; if(tx>=0&&ty>=0&&t<map[tx][ty]) { map[tx][ty]=t; } } } if(map[0][0]<=1) { cout<<-1<<endl; continue; } if(map[0][0]==INF) { cout<<-1<<endl; continue; } bfs(); } return 0; }
相关文章推荐
- poj3669【bfs】
- POJ3669——BFS经典
- poj3669(bfs)
- poj3669(有坑的bfs,mdzz出题人系列)
- 【BFS】POJ3669-Meteor Shower
- poj3669 Meteor Shower BFS
- POJ3669(BFS,障碍关联时间)
- poj3669 BFS
- poj3669 bfs
- poj3669 Meteor Shower bfs
- POJ3669-Meteor Shower-BFS
- POJ3669 Meteor Shower (BFS)
- POJ3669(meteor shower, BFS)TLE
- POJ3669 Meteor Shower BFS
- (BFS)poj3669-Meteor Shower
- poj3669 Meteor Shower(bfs java)
- POJ3669 Meteor Shower(BFS)
- poj3669 之bfs解法
- POJ3669 Meteor Shower 【BFS】
- POJ3669-Meteor Shower(BFS)