POJ 3669 Meteor Shower (带时间轴的bfs)
来源:互联网 发布:户型优化设计 编辑:程序博客网 时间:2024/06/17 23:51
题意很简单:
某个地方发生了流星雨,然后有M颗陨石掉落下来。给你每颗陨石掉落下来的坐标和时间。然后问你能否找到一个安全的位置。如果能输出找到这个位置的最小时间。否则输出-1。
思路:
先用一个Destory数组,记录每个位置最早被摧毁的时间,如果一直没被摧毁记录为-1,然后从起点开始寻路,向周围四个方向遍历。找到第一个Destory为-1的位置,输出最小的这个时间,如果找不到就输出-1。
代码如下:
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<vector>#include<queue>#include<set>#include<map>#include<algorithm>using namespace std;typedef long long ll;typedef pair<int,int> P;const int MAX = 50010;int Destory[310][310];bool used[310][310];const int xx[] = {1,0,-1,0,0};const int yy[] = {0,-1,0,1,0};int M;class Point{public: int X,Y,T; Point(int cx=0,int cy=0,int ct=0){ X = cx; Y = cy; T = ct; } void setdate(){ scanf("%d%d%d",&X,&Y,&T); } void Display(){ printf("%d %d %d\n",X,Y,T); } friend bool operator < (Point A,Point B);};class Point q[MAX],tem[MAX];bool operator < (Point A,Point B){ return A.T < B.T;}bool Check(Point A){ if(A.X < 0 || A.Y < 0) return false; return true;}int MaxTime;void init(){ memset(Destory,-1,sizeof(Destory)); memset(used,false,sizeof(used)); MaxTime = -1;}void GetMeteor(){ init(); scanf("%d",&M); for(int i=1;i<=M;++i){ q[i].setdate(); MaxTime = max(MaxTime,q[i].T); for(int j=0;j<5;++j){ int tx = q[i].X + xx[j]; int ty = q[i].Y + yy[j]; int tt = q[i].T; if(Check(Point(tx,ty,tt))){ if(Destory[tx][ty] == -1) Destory[tx][ty] = tt; else Destory[tx][ty] = min(Destory[tx][ty],tt); } } }}int bfs(){ queue<Point> que; que.push(Point()); while(!que.empty()){ Point Basic = que.front();que.pop(); for(int i=0;i<4;++i){ int tx = Basic.X + xx[i]; int ty = Basic.Y + yy[i]; int tt = Basic.T + 1; if(!Check(Point(tx,ty,tt))) continue; if(Destory[tx][ty] == -1) return tt; if(Destory[tx][ty] > tt){ if(!used[tx][ty]){ used[tx][ty] = true; que.push(Point(tx,ty,tt)); } } } } return -1;}int main(void){ GetMeteor(); printf("%d\n",bfs()); return 0;}
阅读全文
0 0
- POJ 3669 Meteor Shower (带时间轴的bfs)
- POJ 3669 Meteor Shower (BFS)
- POJ 3669 Meteor Shower (BFS + 预处理)
- POJ 3669 Meteor Shower (bfs)
- POJ 3669 Meteor Shower(bfs)
- poj 3669 Meteor Shower(BFS)
- POJ 3669 Meteor Shower(BFS)
- POJ 3669-Meteor Shower(BFS)
- POJ - 3669 - Meteor Shower(bfs)
- POJ 3669 Meteor Shower(BFS)
- POJ 3669 Meteor Shower(BFS)
- POJ 3669 Meteor Shower(BFS)
- POJ 3669 Meteor Shower(bfs)
- poj 3669-Meteor Shower(简单bfs)
- POJ 3669 Meteor Shower(BFS)
- poj 3669 Meteor Shower (bfs)
- POJ 3669-Meteor Shower(BFS)
- POJ 3669 Meteor Shower(BFS)
- 欢迎使用CSDN-markdown编辑器
- PAT1003. Emergency (25)
- 关于同步、异步、阻塞、非阻塞的几点理解
- 设计模式之工厂模式
- SpringBoot入门-快速整合Mybatis
- POJ 3669 Meteor Shower (带时间轴的bfs)
- PYTHON 与 小说(二)
- 推荐系统——协同过滤与隐语义模型详解
- win 10 hosts文件不生效
- SpringBoot项目实战:四种读取properties文件的方式
- session中load()和get()的区别
- 组合空间数据加减
- java 计算年月日
- mysql的定时调度器