Poj 3669 Meteor Shower【bfs】

24 篇文章 0 订阅

Meteor Shower
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 13447 Accepted: 3582

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


题意:

有n个流星从天上掉下来,可以摧毁包括落地点和上下左右在内的五个区域(被摧毁了就不能到达了),给出每个流星下落的坐标和时间,现在一个人位于0 0 坐标处,每一个单位的时间,他能走向前后左右四个方向,问这个人能否到达安全区域,如果不能,输出-1,否则输出最短时间。


实在想不到太好的方法,然后就bfs+暴力模拟了,结果一直wa,后来看了别人的讨论,才明白,自己被平时的bfs 模式化了,安全区域可以超出给出的界限,然后修改了之后,C++,900+MS ,G++ 700+MS,险过....

个人的思路是,记录每个状态的当前时间和当前位置走的步数,搜索的时候所有的状态都按时间排序,时间相同的按步数小的优先排序,在进行判断时,如果在某个时间点有流星落下,就进行模拟,减掉一些状态,否则继续搜索,一直等到流星落下完全时,那么队列的第一个状态就是需要的解。

看到别人的思路,只是状态搜索的处理方法不一样,省了很多时间。因为我是按时间进行排序剪枝的,每次减掉的状态都很少,也就浪费了很多的处理时间,有机会好好再研究一下,争取尽量优化自己的代码


#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn=405;
int n,dx[]={0,0,-1,1},dy[]={1,-1,0,0};
bool map[maxn][maxn],vis[maxn][maxn];
struct met
{
	int x,y,step,time;//坐标,步数,当前时间 
	bool friend operator < (met a,met b)
	{
		if(a.time==b.time)
		{
			return a.step>b.step;
		}
		return a.time>b.time;
	}
}m[maxn*maxn];
bool cmp(met a,met b)
{
	return a.time<b.time;
}
void ruin(int num)//摧毁的地区 
{
	map[m[num].x][m[num].y]=1;//落地点 
	for(int i=0;i<4;++i)//四个方位 
	{
		int tx=m[num].x+dx[i],ty=m[num].y+dy[i];
		if(tx<0||ty<0)
		{
			continue;
		}
		map[tx][ty]=1;
	}
}
int bfs(int x,int y)
{
	memset(vis,0,sizeof(vis));
	memset(map,0,sizeof(map));
	priority_queue<met> q;
	met st={0,0,0,0};vis[0][0]=1;
	q.push(st);
	int cnt=0;//统计流星下落的个数 
	while(!q.empty())
	{
		st=q.top();q.pop();
		while(cnt<n&&st.time>=m[cnt].time)//控制流星落下 
		{
			ruin(cnt);
			++cnt;
		}
		if(map[st.x][st.y])//当前位置被摧毁 
		{
			continue; 
		}
		else//否则可以不移动 
		{
			if(cnt==n)
			{
				return st.step; 
			}
			met tp=st;++tp.time;//讨论一直在某处不走的情况 
			q.push(tp);
		}
		for(int i=0;i<4;++i)//四个方向尝试 
		{
			int tx=st.x+dx[i],ty=st.y+dy[i];
			if(tx<0||ty<0||map[tx][ty])//出界或者被摧毁 
			{
				continue;
			}
			if(!vis[tx][ty])//未访问过 
			{
				vis[tx][ty]=1;
				met tp={tx,ty,st.step+1,st.time+1}; 
				q.push(tp);
			}
		}
	}
	return -1;
}
int main()
{
//	freopen("shuju.txt","r",stdin); 
	while(~scanf("%d",&n))
	{
		for(int i=0;i<n;++i)
		{
			scanf("%d%d%d",&m[i].x,&m[i].y,&m[i].time);
		}
		sort(m,m+n,cmp);
		printf("%d\n",bfs(0,0));
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值