Meteor Shower

面对即将到来的流星雨威胁,Bessie需要找到一条安全的路径到达一个永远不受流星撞击的地方。通过在坐标平面上避开被破坏的位置,Bessie必须确定能够到达安全地点所需的最短时间。

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

#include<stdio.h>  
#include<string.h>  
#include<math.h>  
#include<queue>  
#include<algorithm>  
#define inf 0x3f3f3f3f
using namespace std;  
int vis[350][350];
int vis1[330][330];
const int dis[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
typedef struct node{
	int x,y,t;
}Q;
void BFS()  
{   
    Q  n1,temp;
    queue<node>Q;
    n1.x=0;n1.y=0;n1.t=0;
    Q.push(n1);
    int x1,y1,i;
	if(vis[0][0]==0) {
	    printf("-1\n");
	return ; 
    }
	if(vis[0][0]==inf) {
	    printf("0\n");
	return ; 
     }
     vis1[0][0]=1;
    while(!Q.empty()){
    	n1=Q.front();
    	Q.pop();
    	if(vis[n1.x][n1.y]==inf)
    	 {
    	 	 printf("%d\n",n1.t);
    	 	 return ;
		 }
    	for(i=0;i<4;i++)
    	{
		    temp=n1;
    		x1=temp.x+dis[i][0];
    		y1=temp.y+dis[i][1];
			temp.t=temp.t+1;
			temp.x=x1;
    		temp.y=y1;
    		if(!vis1[x1][y1]&&x1>=0&&x1<=301&&y1>=0&&y1<=301&&temp.t<vis[x1][y1])
    		{
    			
    			if(vis[n1.x][n1.y]==inf)
					 {
					 	 printf("%d\n",n1.t);
					 	 return ;
					 }
    			vis1[x1][y1]=1;
    		
    			Q.push(temp);
			}
		}
	}
	printf("-1\n");
}  
int main()  
{  
    int m;
    while(~scanf("%d",&m))
    {	
	    int i,j;
    	int a,b,c;
    	int a1,b1;
    	memset(vis1,0,sizeof(vis1));
    	for(i=0;i<=310;i++)
    	    for(j=0;j<=310;j++)
    	        vis[i][j]=inf;
    	for(i=0;i<m;i++){
            scanf("%d%d%d",&a,&b,&c);
            if(vis[a][b]>c)
                vis[a][b]=c;
            for(j=0;j<4;j++)
            {
             	a1=a+dis[j][0];
             	b1=b+dis[j][1];
                if(a1>=0&&b1>=0&&vis[a1][b1]>c)
                     vis[a1][b1]=c;
			 }     
		}
    	BFS();
	}
    return 0;
}  
   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值