POJ 3669 - Meteor Shower

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int dx[4]={+1,0,-1,0};
int dy[4]={0,+1,0,-1};
struct type{
	int x,y,t;
}meteor[50000+5],now,next;
int n,map[305][305],latest;
bool vis[305][305];
int bfs()
{
	memset(vis,0,sizeof(vis)); //初始化标记数组 
	queue<type> q;
	now.x=0;
	now.y=0;
	now.t=0;
	vis[now.x][now.y]=1;
	q.push(now);
	while(!q.empty())
	{
		now=q.front();q.pop();
		if(map[now.x][now.y] == 2139062143) return now.t; //如果当前这个格子的被流星摧毁时间是正无穷,那么已经安全
		for(int i=0;i<4;i++)
		{
			next.x=now.x+dx[i] , next.y=now.y+dy[i] , next.t=now.t+1 ;
			if(next.x >= 0 && next.y >= 0 && map[next.x][next.y] > next.t && !vis[next.x][next.y])
			{
				vis[next.x][next.y]=1;
				q.push(next);
			}
		}
	}
	return -1;
}
int main()
{
	while(scanf("%d",&n) != EOF)
	{
		memset(map,0x7F,sizeof(map)); //先将地图上每个格子初始化为“正无穷” 
		latest=-1;
		for(int i=1;i<=n;i++)
		{
			scanf("%d %d %d",&meteor[i].x,&meteor[i].y,&meteor[i].t);
			
			if(map[meteor[i].x][meteor[i].y] > meteor[i].t) map[meteor[i].x][meteor[i].y] = meteor[i].t; 
			for(int j=0;j<4;j++){
				next.x=meteor[i].x+dx[j];
				next.y=meteor[i].y+dy[j];
				if(map[ (next.x) ][ (next.y) ] > meteor[i].t) map[ (next.x) ][ (next.y) ] = meteor[i].t;
			} //将地图上的每个格子更新,使其值成为该格最早被流星摧毁的时间 
			 
		}
		printf("%d\n",bfs());
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值