您的位置:首页 > 其它

POJ 3669 Meteor Shower (带时间轴的bfs)

2017-11-30 18:44 369 查看
题意很简单:

某个地方发生了流星雨,然后有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;+
cca0
+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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: