Meteor Shower(POJ 3669)
2016-01-22 10:59
447 查看
Meteor Shower
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
WA:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12816 | Accepted: 3451 |
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
5 题意思路:m个陨石将砸向第一象限和正坐标抽,每块陨石砸的时间分别为Ti,从0,0出发,如何走才能达到安全位置; 用一个T[][]数组记录每个点的最早爆炸时间,vis标记该点是否被砸,如果该点不可能被砸,那么为安全位置,否则向四个方向扩展,改点可走的条件为改点没有走过以及该点爆炸的时间大于当前时间,对于走过的点标记T[][]为0,那么改点在以后就不可能再走。 反正我的思路好麻烦,每次都要写个函数对所有陨石进行遍历判断是否为安全位置。 AC:
#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <set> #include <vector> #define INF 0x3f3f3f #define eps 1e-8 #define MAXN (50000+1) #define MAXM (100000) #define Ri(a) scanf("%d", &a) #define Rl(a) scanf("%lld", &a) #define Rf(a) scanf("%lf", &a) #define Rs(a) scanf("%s", a) #define Pi(a) printf("%d\n", (a)) #define Pf(a) printf("%.2lf\n", (a)) #define Pl(a) printf("%lld\n", (a)) #define Ps(a) printf("%s\n", (a)) #define W(a) while(a--) #define CLR(a, b) memset(a, (b), sizeof(a)) #define MOD 1000000007 #define LL long long #define lson o<<1, l, mid #define rson o<<1|1, mid+1, r #define ll o<<1 #define rr o<<1|1 using namespace std; struct Node{ int x, y, step; }; bool judge(int x, int y){ return x >= 0 && y >= 0; } bool vis[301][301]; int Move[4][2] = {0,1, 0,-1, 1,0, -1,0}; int T[301][301]; int BFS(int x, int y) { if(!vis[x][y]) return 0; queue<Node> Q; Node now, next; now.x = x; now.y = y; now.step = 0; Q.push(now); while(!Q.empty()) { now = Q.front(); Q.pop(); if(!vis[now.x][now.y]) return now.step; for(int k = 0; k < 4; k++) { next.x = now.x + Move[k][0]; next.y = now.y + Move[k][1]; next.step = now.step + 1; if(!judge(next.x, next.y)) continue; if(vis[next.x][next.y]) { if(T[next.x][next.y] != INF && next.step < T[next.x][next.y]) { T[next.x][next.y] = 0; Q.push(next); } } else return next.step; } } return -1; } int main() { int n; while(Ri(n) != EOF) { CLR(vis, false); CLR(T, INF); for(int i = 0; i < n; i++) { int x, y, t; Ri(x); Ri(y); Ri(t); T[x][y] = min(T[x][y], t); vis[x][y] = true; for(int k = 0; k < 4; k++) { int xx = x + Move[k][0]; int yy = y + Move[k][1]; if(!judge(xx, yy)) continue; vis[xx][yy] = true; T[xx][yy] = min(T[xx][yy], t); } } Pi(BFS(0, 0)); } return 0; }
WA:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; int vis[305][305],b[305][305]; struct node {int x,y,t;}m[50005]; bool cmp(node a,node b) {return a.t<=b.t;} int n,Mint; int dx[]={0,0,0,1,-1},dy[]={0,1,-1,0,0}; bool safe(node p) { int l=-1,r=n,mid; while(r-l>1) { mid=(l+r)/2; if(m[mid].t>=p.t) r=mid; else l=mid; } for(int i=r;i<n;i++) { for(int j=0;j<5;j++) { if(p.x==(m[i].x+dx[j])&&p.y==(m[i].y+dy[j])) return 0; } } return 1; } int bfs() { int i,j; Mint=-1; queue<node> s; node temp,next; temp.x=temp.y=temp.t=0; s.push(temp); while(!s.empty()) { temp=s.front(); s.pop(); if(safe(temp)) { Mint=temp.t; break; } temp.t++; int l=-1,r=n,mid; while(r-l>1) { mid=(l+r)/2; if(m[mid].t>=temp.t) r=mid; else l=mid; } while(m[r].t==temp.t) { for(int i=0;i<=4;i++) b[m[r].x+dx[i]][m[r].y+dy[i]]=1; r++; } for(i=1;i<=4;i++) { next.x=temp.x+dx[i]; next.y=temp.y+dy[i]; next.t=temp.t; if(next.x>=0&&next.y>=0&&vis[next.x][next.y]==0&&b[next.x][next.y]==0) { vis[next.x][next.y]=1; s.push(next); } //cout<<"ads"; } } return Mint; } int main() { int i,j; freopen("in.txt","r",stdin); while(scanf("%d",&n)!=EOF) { memset(vis,0,sizeof(vis)); memset(b,0,sizeof(vis)); for(i=0;i<n;i++) scanf("%d%d%d",&m[i].x,&m[i].y,&m[i].t); sort(m,m+n,cmp); //for(i=0;i<n;i++) // cout<<m[i].x<<" "<<m[i y<<" "<<m[i].t<<endl; i=0; bool flag=0; while(m[i].t==0) { if((m[i].x==0&&m[i].y==0)||(m[i].x==0&&m[i].y==1)||(m[i].x==1&&m[i].y==0)) { cout<<-1<<endl; flag=1; break; } i++; } if(flag) continue; bfs(); printf("%d\n",Mint); } }
相关文章推荐
- 【Android开发工作笔记】一些零零星星的tips
- php+html5实现无刷新图片上传教程
- C++ static专区
- linux下eclipse cdt主函数main参数值传递设置
- 文章标题
- Caffe源码解析2:SycedMem
- JavaSE-单例模式初学
- 用户输入 i. 检测常用手势(一)
- OneAlert 入门(二)——事件分析
- Maven实战(一)安装和配置
- RN之Flexbox
- MySQL主从架构配置
- 链表的表示及实现
- 配置 nginx 支持 f4v视频格式播放
- Nginx负载均衡(主备)+Keepalived
- 蓝桥杯 最大乘积
- CentOS 下安装 Redis
- 历经流年,风轻云淡
- cf 616A Comparing Two Long Integers
- Activiti中的流程历史保存流程发起人的方法