博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ-3669
阅读量:5226 次
发布时间:2019-06-14

本文共 2876 字,大约阅读时间需要 9 分钟。

Meteor Shower
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 21055   Accepted: 5499

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 (XiYi) (0 ≤ X≤ 300; 0 ≤ Y≤ 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: XiYi, 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

40 0 22 1 21 1 20 3 5

Sample Output

5

 

题意:

输入m组数,每组分别代表x,y坐标和陨石落在改点时的时间t。从零点出发,问是否有生还的可能。

 

设mp数组初始值为-1,然后存入时间t。

用bfs层次遍历直到找出mp==-1的点,返回现在的t。

 

AC代码:

1 //#include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
15 #include
16 using namespace std;17 18 const int MAX=310;19 20 struct node{21 int x;int y;int t;22 }s,temp;23 24 int m;25 int mp[MAX][MAX];26 int dir[5][2]={ { 0,0},{ 1,0},{ 0,1},{-1,0},{ 0,-1}};27 queue
q;28 29 int bfs(){30 if(mp[0][0]==-1) return 0;31 if(mp[0][0]==0) return -1;32 s.x=0;33 s.y=0;34 s.t=0;35 q.push(s);36 while(!q.empty()){37 temp=q.front();38 q.pop();39 for(int i=0;i<5;i++){40 s.x=temp.x+dir[i][0];41 s.y=temp.y+dir[i][1];42 s.t=temp.t+1;43 if(s.x<0||s.x>=MAX||s.y<0||s.y>=MAX) continue;44 if(mp[s.x][s.y]==-1) return s.t;45 if(mp[s.x][s.y]<=s.t) continue;46 mp[s.x][s.y]=s.t;47 q.push(s);48 }49 }50 return -1;51 }52 53 int main(){54 ios::sync_with_stdio(false);55 cin>>m;56 memset(mp,-1,sizeof(mp));57 while(m--){58 int x,y,t;59 cin>>x>>y>>t;60 for(int i=0;i<5;i++){61 int nx=x+dir[i][0];62 int ny=y+dir[i][1];63 if(nx<0||nx>=MAX||ny<0||ny>=MAX)64 continue;65 if(mp[nx][ny]==-1)66 mp[nx][ny]=t;67 else68 mp[nx][ny]=min(mp[nx][ny],t);69 }70 }71 cout<
<

 

转载于:https://www.cnblogs.com/Kiven5197/p/7255959.html

你可能感兴趣的文章
关于物料的分类
查看>>
easyui-textbox input输入框的一种取值方式
查看>>
JAVA常见算法题(九)
查看>>
iOS 开发小技巧
查看>>
【转】电子方向开发工程师的职场人生路
查看>>
JqERY
查看>>
图解HTTPS
查看>>
Fiddler模拟发送post请求
查看>>
COCOS2D 释放资源的最佳时机
查看>>
eclipse里配置Android ndk环境,用eclipse编译.so文件
查看>>
最北之北,最南之南
查看>>
combobox的用法
查看>>
AttributeError: module 'unittest' has no attribute 'TestCase'
查看>>
[算法]: 排序-冒泡排序
查看>>
JavaScript实现的图片循环播放
查看>>
动态规划(0-1背包)---字符构成最多的字符串
查看>>
让类只能用new来创建实例
查看>>
【ISO】【2011-3-20】【NSInvocation简单使用】【转】
查看>>
现有Silverlight程序使用mvvmlight开发框架教程
查看>>
Event Handler
查看>>