POJ 3669 - Meteor Shower(广搜)

发布时间:2014-10-22 13:08:57编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"POJ 3669 - Meteor Shower(广搜)",主要涉及到POJ 3669 - Meteor Shower(广搜)方面的内容,对于POJ 3669 - Meteor Shower(广搜)感兴趣的同学可以参考一下。

Meteor Shower Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6218   Accepted: 1836 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 5 ================================ 题目大意:流星在t时刻砸在(x,y)点以及周围四个点。人从(0,0)开始走,每秒走一步,上下左右都行,但是要在第一象限。问最少多少步才能走到安全区域。 思路:把map数组置为-1,预处理一下,把所有被砸的点记上砸下来的时间,依然为-1的点就是安全点了。如果到达某一点的时间大于该点被破坏的时间就不能走,直接continue。看最少多少步能走到一个-1点。 需要注意的是流星砸下来的区域在0<=x,y<=300,但是安全区域在整个第一象限。 #include <iostream> #include <cstdio> #include <queue> #include <algorithm> #include <cstring> using namespace std; int dir[4][2]={{-1,0},{0,-1},{0,1},{1,0}}; int map[333][333],vis[333][333]; int time,m; struct point { int x,y,t; }p,s[55555]; queue <point> que; bool cmp(point a,point b) { return a.t<b.t; } bool check(int x,int y) { if(x>=0&&y>=0) return 1; return 0; } void solve() { for(int i=0;i<m;i++) { if(map[s[i].x][s[i].y]==-1) map[s[i].x][s[i].y]=s[i].t; if(map[s[i].x-1][s[i].y]==-1&&s[i].x>=1)//小心别出界 map[s[i].x-1][s[i].y]=s[i].t; if(map[s[i].x][s[i].y-1]==-1&&s[i].y>=1)//小心别出界 map[s[i].x][s[i].y-1]=s[i].t; if(map[s[i].x][s[i].y+1]==-1) map[s[i].x][s[i].y+1]=s[i].t; if(map[s[i].x+1][s[i].y]==-1) map[s[i].x+1][s[i].y]=s[i].t; } } int bfs() { p.x=0;p.y=0;p.t=0; que.push(p); vis[0][0]=1; while(!que.empty()) { point temp=que.front(); que.pop(); if (temp.t>=map[temp.x][temp.y]) continue; for(int i=0;i<4;i++) { p.x=temp.x+dir[i][0]; p.y=temp.y+dir[i][1]; p.t=temp.t+1; if(!vis[p.x][p.y]) { time=p.t; if(check(p.x,p.y)&&map[p.x][p.y]==-1)//到达安全区域了 { return time; } else if(check(p.x,p.y)&&p.t<map[p.x][p.y]) { que.push(p); vis[p.x][p.y]=1;//标记已经走过的点 } } } } return -1; } int main() { memset(map,-1,sizeof(map)); memset(vis,0,sizeof(vis)); scanf("%d",&m); for(int i=0;i<m;i++) scanf("%d%d%d",&s[i].x,&s[i].y,&s[i].t); sort(s,s+m,cmp); solve(); /*for(int i=0;i<4;i++) { for(int j=0;j<4;j++) cout<<map[i][j]<<" "; cout<<endl; }*/ int ans=bfs(); if(ans!=-1) printf("%d\n",ans); else printf("-1\n"); return 0; }

下一篇:[置顶] 我的嵌入式学习之路——笔记计划