admin管理员组文章数量:1584633
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
* Line 1: The minimum time it takes Bessie to get to a safe place or -1 if it is impossible.
Sample Input4 0 0 2 2 1 2 1 1 2 0 3 5Sample Output
5
#include<iostream>
#include<string>
#include<string.h>
#include<queue>
#include<cstdio>
using namespace std;
#define maxn 302 //301就是不对,不清楚原因
#define INF 0x3f3f3f3f
int mp[maxn][maxn],vis[maxn][maxn];
int dir[4][2]={-1,0,1,0,0,1,0,-1};
typedef struct Node{
int x,y,step;
}Node;
int bfs(int s,int t)
{
memset(vis,0,sizeof(vis));
queue<Node> q;
Node tmp,nxt;
nxt.x=s,nxt.y=t;
nxt.step=0;
vis[s][t]=1;
q.push(nxt);
while(!q.empty()){
tmp=q.front();
q.pop();
if(mp[tmp.x][tmp.y]==INF)
return tmp.step;
for(int i=0;i<4;i++)
{
int a=tmp.x+dir[i][0],b=tmp.y+dir[i][1];
if(a>=0&&a<maxn&&b>=0&&b<maxn)
{
nxt.x=a;
nxt.y=b;
nxt.step=tmp.step+1;
if(!vis[nxt.x][nxt.y]&&nxt.step<mp[nxt.x][nxt.y])
{
vis[a][b]=1;
q.push(nxt);
}
}
}
}
return -1;
}
int main()
{
int m,x,y,tmp;
for(int i=0;i<maxn;i++)
{
for(int j=0;j<maxn;j++)
mp[i][j]=INF;
}
cin>>m;
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&x,&y,&tmp);
mp[x][y]=min(tmp,mp[x][y]);
for(int j=0;j<4;j++)
{
int a=x+dir[j][0],b=y+dir[j][1];
if(a>=0&&a<maxn&&b>=0&&b<maxn)
{
mp[a][b]=min(tmp,mp[a][b]);
}
}
}
cout<<bfs(0,0)<<endl;
return 0;
}
版权声明:本文标题:(bfs)Meteor Shower 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/xitong/1726559737a1075706.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论