Meteor Shower


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


4
0 0 2
2 1 2
1 1 2
0 3 5

Sample Output


5


這道B題,我就操瞭,整瞭那麼久。這道題題意就是一個妹子的傢鄉要被流星雨襲擊,妹子傢住在原點坐標處,地圖隻包括第一象限


,給出流行下落的位置以及時間,流行還會毀壞它附近(上下左右)的位置,問你妹子走最少幾步就能到達安全區。


這道題得進行預處理,也就是說先把流行墜落的位置處以及他的周圍標記上時間,如果你在大於這個時間的時候到達這。那麼肯定是不行的。


我這裡有一個註意點需要記住,memset初始化二維數組隻能是0跟-1,但十六進制數也是可以的,實在不行就用for循環。


另外我這裡有一個點一直過不瞭差點氣死我,就是你廣搜如果搜不到安全區域呢,那麼不就得輸出-1,可我卻沒有進行處理。


好好記住這個教訓。


#include<iostream>
#include<stdio.h>
#include<queue>
#include<string.h>
#include<stdlib.h>
#define INF 99999
using namespace std;
struct node
{
int x;
int y;
int step;
};

queue<node> Q;
int map[512][512];
int book[512][512];
int next[5][2]={{0,0},{1,0},{-1,0},{0,1},{0,-1}};
int M,most=0,flag=0;
void BFS();
int main()
{
int i,j,xx,yy,tt,nx,ny;
cin>>M;
//memset(map,0x7F,sizeof(map));
for(i=0;i<512;i++)
for(j=0;j<512;j++)
map[i][j]=INF;
memset(book,0,sizeof(book));
for(i=0;i<M;i++)
{
scanf("%d %d %d",&xx,&yy,&tt);
//cout<<"tt="<<tt<<endl;
most=max(most,tt);
for(j=0;j<5;j++)
{
nx=xx+next[j][0];
ny=yy+next[j][1];
if(nx<0 || ny<0) continue;
//printf("nx=%d ny=%d map=%d\n",nx,ny,map[nx][ny]);
if(map[nx][ny]>tt)
{
map[nx][ny]=tt;
//cout<<map[nx][ny]<<endl;
}
}
}
book[0][0]=1;
if(map[0][0]==0)
{
printf("-1\n");
}
else
{
BFS();
if(flag==0)
printf("-1\n");
}
return 0;
}

void BFS()
{
struct node start,temp;
int i,j;
start.x=0;
start.y=0;
start.step=0;

Q.push(start);

while(!Q.empty())
{
start=Q.front();

Q.pop();

for(i=1;i<5;i++)
{
temp=start;
temp.x=start.x+next[i][0];
temp.y=start.y+next[i][1];
temp.step++;
if(temp.x<0 || temp.y<0) continue;
//out<<map[temp.x][temp.y]<<endl;
if(map[temp.x][temp.y]>temp.step && book[temp.x][temp.y]==0)
{
//puts("jdd");
book[temp.x][temp.y]=1;
if(map[temp.x][temp.y]>most)
{
printf("%d\n",temp.step);
flag=1;
return ;
}
Q.push(temp);
}
}
}
}

0 個評論

要回覆文章請先登錄註冊