广度优先搜索——USACO08FEB(洛谷 P2895)

本文要讲述的是一道既简单又复杂的一道题,值得我们好好去思考并求解。

题目出自洛谷P2895 同时也是USACO2008年的一道题。

其中主要问题是如何将问题转化为对应的数组或图?如何判断满足的条件情况?

以及设置的坑有哪些?

先看一下题目吧,在题目后面,我会给出具体的思路和避免的坑!


题目描述

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.

中文翻译

贝茜听说了一个骇人听闻的消息:一场流星雨即将袭击整个农场,由于流星体积过大,它们无法在撞击到地面前燃烧殆尽,届时将会对它撞到的一切东西造成毁灭性的打击。很自然地,贝茜开始担心自己的安全问题。以FJ牧场中最聪明的奶牛的名誉起誓,她一定要在被流星砸到前,到达一个安全的地方(也就是说,一块不会被任何流星砸到的土地)。如果将牧场放入一个直角坐标系中,贝茜现在的位置是原点,并且,贝茜不能踏上一块被流星砸过的土地。 根据预报,一共有M颗流星(1 <= M <= 50,000)会坠落在农场上,其中第i颗流星会在时刻T_i (0 <= T_i <= 1,000)砸在坐标为(X_i, Y_i) (0 <= X_i <= 300;0 <= Y_i <= 300)的格子里。流星的力量会将它所在的格子,以及周围4个相邻的格子都化为焦土,当然贝茜也无法再在这些格子上行走。

贝茜在时刻0开始行动,它只能在第一象限中,平行于坐标轴行动,每1个时刻中,她能移动到相邻的(一般是4个)格子中的任意一个,当然目标格子要没有被烧焦才行。如果一个格子在时刻t被流星撞击或烧焦,那么贝茜只能在t之前的时刻在这个格子里出现。

请你计算一下,贝茜最少需要多少时间才能到达一个安全的格子。

输入输出样例

输入 

4
0 0 2
2 1 2
1 1 2
0 3 5

输出 

5

 正文开始

读完题目是不是感觉 有点思路但又不完全有 QAQ 确实比普通的搜索题复杂了一点,但不是完全一点没有写下去的想法的!

前言: 

 这道题……怎么说呢……完全是一道很普通的广搜外加不普通的坑点……

1.坑点介绍 

1.坐标不能低于0,但可以超300!

2.流星定时砸下;

3.流星砸下时间已最早的那个为准!

4.如果出不去还要输出-1!

你,有没有被坑到呢?

2.关于这道题

首先,是一道明显的bfs题,要求最短时间,所以用队列记录;

陨石地图用一个二维数组记录,内容为砸下时间,以时间早为标准;再用一个二维数组记录每个点最短时间;

终止条件:如果搜到一个点永远不会被陨石砸到,输出该点时间,或者直到搜索结束也没有出去,输出-1(翻译竟然没有翻出来我无语了……

 解题代码:

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;
int n,sx,sy,st,ma[305][305],book[305][305],ans[305][305];
int dx[5] = {0,0,0,1,-1};
int dy[5] = {0,1,-1,0,0}; //处理移动和陨石砸落
queue<int> q[2]; //建一个队列数组,分别存放x 和 y 的坐标
int ch(int a){
	if(a==-1) return 99999;
	else return a;
}//判断该点是不是已经砸落过了,如果没有 则相当于n年后才砸落
void bfs(){
	book[0][0] = 1; //初始化操作
	q[0].push(0); q[1].push(0);
	while(!q[0].empty()){
		int x = q[0].front(),y=q[1].front();
		int s = ans[x][y] + 1;
		if(ma[x][y]==-1){ //安全的最终点是已经确定了的
			printf("%d",s-1);
			break;
		}
		for(int i=1;i<=4;i++){
			int xx=x+dx[i],yy=y+dy[i];
			if(xx>=0&&yy>=0 && s<ch(ma[xx][yy]) && book[xx][yy]==0){
				//s 代表当前时间 和下一步地点砸落时间作比较
				q[0].push(xx); q[1].push(yy);
				book[xx][yy] = 1;
				ans[xx][yy] = s;
			}
		}
		q[0].pop(); q[1].pop();
	}
	if(q[0].empty()) printf("-1");
}
int main(){
	scanf("%d",&n);
	for(int i=0;i<305;i++){
		for(int j=0;j<305;j++){
			 ma[i][j] = -1;
		}
	}//陨石 初始化为-1
	//输入陨石并保存结果
	for(int i=1;i<=n;i++){
		scanf("%d%d%d",&sx,&sy,&st);
		for(int j=0;j<5;j++){ //标记陨石砸落
			if(sx+dx[j]>=0&&sy+dy[j]>=0&& (ma[sx+dx[j]][sy+dy[j]]==-1||ma[sx+dx[j]][sy+dy[j]]>st)){
				ma[sx+dx[j]][sy+dy[j]]=st;
			}
		}
	}
	bfs();
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44572229/article/details/119713982