admin管理员组文章数量:1532251
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 rep
orts say that M meteors (1 ≤ M ≤ 50,000) will strike, with meteori 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.
* Line 1: A single integer: M
* Lines 2..M+1: Line i+1 contains three space-separated integers:Xi, Yi, and Ti
* Line 1: The minimum time it takes Bessie to get to a safe place or -1 if it is impossible.
4 0 0 2 2 1 2 1 1 2 0 3 5
与大白书上的迷宫例题十分相似 可以说套路基本一致
用数组place去存他爆炸的时间,存入最大值,然后更新此地爆炸最小值
用d来存 到达各点所用时间,顺路判断此地是否走过
queue用来进行宽度优先搜索
#include <iostream>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
int M;
const int INF=0x3f3f3f3f;
const int maxn=300+5;
int d[maxn][maxn];
int place[maxn][maxn];
typedef pair<int,int> P;
queue<P> que;
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
int bfs()
{
memset(d,-1,sizeof(d));
que.push(P(0,0));
d[0][0]=0;
int gx=0,gy=0;
while(!que.empty())
{
P p=que.front();
que.pop();
if(place[p.first][p.second]==INF)
{ return d[p.first][p.second] ;
break;
}
for(int i=0;i<4;i++) //对其四周进行行走判断
{
int nx=p.first+dx[i],ny=p.second+dy[i];
//如果下一步走位置,在当前时间加一后还未破坏,并且此处未走过,则存入队列
if(nx>=0&&ny>=0&&(place[nx][ny]>d[p.first][p.second]+1)&&d[nx][ny]==-1)//宛如一个智障,把nx ny写反了,查了好久的错,
{
que.push(P(nx,ny));
d[nx][ny]=d[p.first][p.second]+1;
}
}
}
return -1;//可走点都空了还未安全 那。。die了
}
int main()
{
cin>>M;
int t,x,y;
memset(place,INF,sizeof place );
for(int i=0;i<M;i++)
{
cin>>x>>y>>t;
place[x][y]=min(place[x][y],t);//注意此处也要取min
for(int i=0;i<4;i++) //存储爆炸范围
{
int nx=x+dx[i],ny=y+dy[i];
if(nx>=0&&ny>=0)
place[nx][ny]=min(place[nx][ny],t);
}
}
int res=bfs();
cout << res << endl;
return 0;
}
版权声明:本文标题:E G - Meteor Shower爆爆爆逃离炸区 bfs 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dianzi/1726559838a1075717.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论