码迷,mamicode.com
首页 > 其他好文 > 详细

[USACO08FEB]Meteor Shower S

时间:2020-05-01 20:59:36      阅读:71      评论:0      收藏:0      [点我收藏+]

标签:leaves   find   const   位置   contains   ini   不能   point   queue   

题目:Meteor Shower S

网址:https://www.luogu.com.cn/problem/P2895

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.

输入
  • Line 1: A single integer: M

  • Lines 2..M+1: Line i+1 contains three space-separated integers: Xi, Yi, and Ti

输出
  • Line 1: The minimum time it takes Bessie to get to a safe place or -1 if it is impossible.
输入输出样例
输入
4
0 0 2
2 1 2
1 1 2
0 3 5
输出
5

记录每一个格子被砸的时间(如果没有就赋值为-1)。

广搜,如果越界或者当前时间>=该格子砸落的时间就不能扩展。

需要判重。

不过要注意这道题有很多值得深思回味的坑: 1.如果一个格子多次被砸,则记录最早一次被砸的时间。
2.题目指的安全位置可以大于n。

代码如下:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#define pii pair <int, int>
using namespace std;
const int maxn = 300 + 5;
const int dx[4] = {-1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
int m;
int map[maxn][maxn], d[maxn][maxn];
bool valid(int x, int y)
{
	if(x < 0 || y < 0 || d[x][y] > -1) return false;
	return true;
}
int bfs()
{
	queue <pii> Q;
	while(!Q.empty()) Q.pop();
	memset(d, -1, sizeof(d));
	int time;
	d[0][0] = 0;
	Q.push(make_pair(0, 0));

	while(!Q.empty())
	{
		pii now = Q.front(), next;
		Q.pop();
		time = d[now.first][now.second] + 1;
		for(int i = 0; i < 4; ++ i)
		{
			next = make_pair(now.first + dx[i], now.second + dy[i]);
			if(!valid(next.first, next.second)) continue;
			if(map[next.first][next.second] == -1) return time;
			if(map[next.first][next.second] <= time) continue;
			
			d[next.first][next.second] = time;
			Q.push(next);
		}
	}
	return -1;
}
int main()
{
	memset(map, -1, sizeof(map));
	scanf("%d", &m);
	for(int i = 0; i < m; ++ i)
	{
		int x, y, t;
		scanf("%d %d %d", &x, &y, &t);
		if(map[x][y] == -1) map[x][y] = t;
		else map[x][y] = min(t, map[x][y]);
		if(x) {
			if(map[x - 1][y] == -1) map[x - 1][y] = t;
			else map[x - 1][y] = min(map[x - 1][y], t);
		}
		if(x < 300) {
			if(map[x + 1][y] == -1) map[x + 1][y] = t;
			else map[x + 1][y] = min(map[x + 1][y], t);
		}
		if(y) {
			if(map[x][y - 1] == -1) map[x][y - 1] = t;
			else map[x][y - 1] = min(map[x][y - 1], t);
		}
		if(y < 300) {
			if(map[x][y + 1] == -1) map[x][y + 1] = t;
			else map[x][y + 1] = min(map[x][y + 1], t);
		}
	}
	printf("%d\n", bfs());
	return 0;
}

[USACO08FEB]Meteor Shower S

标签:leaves   find   const   位置   contains   ini   不能   point   queue   

原文地址:https://www.cnblogs.com/zach20040914/p/12814283.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!