POJ3669 Meteor Shower(BFS)

题面:

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

 

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

 

分析:

这道题很久以前就遇到过,不过当时看题解都把我看懵逼了,就一直很怂这道题,当时连BFS的基本原理都没想清楚……这次因为做了另一道BFS预处理的题,发现其实没想的那么难,又刚好再次遇见了这道题,才回过头来认真做了。

题意大致是主角最开始位于原点(0,0)处,不幸遭遇了一场贴地流星雨,流星按照时间顺序间歇性砸下来,被流星砸到的地方上下左右加起来一个五个格子都宣布当场报废,主角的求生欲望强烈,想知道能不能在被砸死之前跑到一个绝对安全也就是永远不会被流星砸中的地方。

地图最大300X300,先对流星雨进行预处理,记录下每块地方被砸中的最早时间,划重点,最早时间!这个坑我调了好久才调出来,虽然考虑到了被波及的地方可能更早就被砸中了,但是忘了同一块地方也可能作为中心被砸上几次的,没被砸到的地方就初始化为inf,表示永远不会被砸中。预处理好被砸时间之后常规BFS就行,结构体里加个steps变量记录到达该处时间,起点在(0,0),判断条件加一个到达该处的时间是否小于被砸中的时间,如果到达了一个被砸中时间为inf的地方,就算革命成功啦。对了要记得判断原点被砸的时间,如果原点在0时间就被砸中了,那显然就直接gg了。

话说min函数的头文件不是<algorithm>吗,我小修改的时候忘了用了min,把头文件删掉了,交上去怎么还是AC了。

 

AC代码:

#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
const int maxn = 305;
const int inf = 0x3f3f3f3f;
int maze[maxn][maxn];
int m, dir[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
bool book[maxn][maxn];

struct node{
	int x, y;
	int steps;
};

bool check(int x, int y){
	if(x < 0 || y < 0 || x >= maxn || y >= maxn) return false;
	return true;
}


int BFS(){
	if(maze[0][0] == 0) return -1;
	queue<node>q;
	node front;
	front.x = 0; front.y = 0;
	front.steps = 0;
	q.push(front);
	book[0][0] = true;
	while(!q.empty()){
		front = q.front();
		q.pop();
		if(maze[front.x][front.y] == inf) return front.steps;
		for(int i = 0; i < 4; i++){
			node next = front;
			next.x += dir[i][0];
			next.y += dir[i][1];
			if(check(next.x, next.y) && !book[next.x][next.y]){
				if(front.steps + 1 < maze[next.x][next.y]){
					next.steps = front.steps + 1;
					book[next.x][next.y] = true;
					q.push(next);
				}
			}
		}
	}
	return -1;
}

int main(){
	cin>>m;
	int a, b, t;
	memset(maze, inf, sizeof(maze));
	memset(book, 0, sizeof(book));
	for(int i = 0; i < m; i++) {
		scanf("%d%d%d", &a, &b, &t);
		maze[a][b] = min(maze[a][b], t); //此处有坑,需要记录最早被砸的时间 
		for(int j = 0; j < 4; j++){
			int aa = a + dir[j][0];
			int bb = b + dir[j][1];
			if(check(aa, bb))
				maze[aa][bb] = min(maze[aa][bb], t);
		}
	}
	
	cout<<BFS()<<endl;
	return 0;
} 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值