POJ 3669 Meteor Shower BFS

【题解】POJ 3669 Meteor Shower(BFS) POJ 3669 Meteor Shower https://vjudge.net/problem/POJ-3669 题意 有一场流星雨即将到来,Bessie要找到一个安全的地方。现在已知有M颗流星,并且知道它们抵达的位置和时间,Bessi走一个单位需要一个单位时间,问到达安全位置的最短时间,如果无法到达安全位置就输出-1。 细则: 1、Bessie从起点出发,可以向上下左右四个方向行走。 ... 阅读详情

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 (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

题目大意:
一共n个流星雨即将攻击地球,攻击到某个点时将会使该点及其上下左右四个点都被摧毁,当一个点被摧毁后,我们的女主Bessie就不能待在这个地方了。Bessie要在地球上找到一个地方让她能够度过灾难。把整个地图看作一张二维表,设该女孩在原点。其中输入第一行为流星个数n。接下来n行,每行3个值:x,y,t。代表着(x,y)这一点将在t时被流星攻击。输出Bessie逃命的最短时间,Bessie在每一个单位时间内可以向上下左右中的一个方向移动一个单位距离。如果逃离失败,那就输出-1.。

解题思路:
该题的难点在于地图是随着时间动态变化的。
使用二维数组map[][]来保存地球上的各个位置信息,map的值代表该点被摧毁的最近时间(一个点可能被多次摧毁,多次被直接命中或者被波及,我们需要保存最近的时间),初始值为INF,输入全部的流星信息后,地图上值仍为INF的点就是最后的安全地带。
女主需要在一个点被摧毁之前到达该点:

if (now.t >= map[nx][ny]) continue;

题目中有最小字眼,考虑使用广度优先搜索。

注意:

  • oj平台上的测试样例有超出范围的,我把数组开得更大了一些才AC。
  • 需要提前剪枝
  • 我使用cin输入时选择C++ AC成功,选择G++超时,后来改成scanf输入成功AC

AC代码

#include<iostream>
#include<queue>
#include<stdio.h>
#include<algorithm>
using namespace std;
#define INF 0x3fffffff
struct node {
	int x, y;
	int t;
};
queue<node> q;
int map[320][320];//数组开得更大一些
bool mark[320][320];
int mynext[4][2] = { 1,0,-1,0,0,1,0,-1 };
int BFS(int x, int y) {
	while (!q.empty()) q.pop();
	node tmp;
	tmp.x = x; tmp.y = y; tmp.t = 0;
	q.push(tmp);
	while (!q.empty()) {
		tmp = q.front();
		q.pop();
		for (int i = 0; i < 4; i++) {
			int nx = tmp.x + mynext[i][0];
			int ny = tmp.y + mynext[i][1];
			if (nx < 0 || ny < 0 || nx>310 || ny>310) continue;
			if (tmp.t + 1 >= map[nx][ny]) continue;//在该点被摧毁之前到达
			if (mark[nx][ny] == true) continue;
			node now;
			now.x = nx; now.y = ny; now.t = tmp.t + 1;
			q.push(now);
			mark[nx][ny] = true;
			if (map[nx][ny] == INF) { return now.t; }//到达安全地带
		}
	}
	return -1;
}
int main() {
	for (int i = 0; i <= 310; i++) {
		for (int j = 0; j <= 310; j++) {
			map[i][j] = INF;
			mark[i][j] = false;
		}
	}
	int n;
	scanf("%d", &n);
	//cin >> n;//G++超时
	while (n--) {
		int x, y, tmp;
		scanf("%d%d%d", &x, &y, &tmp);
		//cin >> x >> y;
		//cin >> tmp;
		map[x][y] = min(map[x][y], tmp);//自身也应该取最小值!!!
		//更新是使用新的值  也就是用中心被摧毁的时间去尝试更新周围4个点的值
		//因为如果原map[x][y]小于tmp,但map[x][y]可能是被其他流星波及的值,此时无法波及周围的点
		for (int i = 0; i < 4; i++) {
			int nx = x + mynext[i][0];
			int ny = y + mynext[i][1];
			if (nx < 0 || ny < 0 || nx>310 || ny>310) continue;
			map[nx][ny] = min(map[nx][ny], tmp);
		}
	}
	mark[0][0] = true;
	cout << BFS(0, 0) << endl;
	//system("pause");
	return 0;
}
发那科机器人抓手与物料搬运阀的配置与优化实践 本文详细介绍了发那科机器人抓手与物料搬运阀的配置与优化实践,包括基础配置指南、阀岛与输入模块的详细设置、测试程序编写与验证技巧,以及常见问题排查与性能优化建议。通过实际案例和经验分享,帮助技术人员快速掌握关键配置步骤,提升物料搬运系统的稳定性和效率。 阅读详情

相关推荐

在 Windows 系统下使用 VMware 安装 Ubuntu 24.04 LTS 系统

Ubuntu24.04桌面端官方下载:https://cn.ubuntu.com/download/desktop。

u014695938的博客 1万+

P2895 [USACO08FEB]Meteor Shower S

在流星雨的田野里穿梭

qq_43034907的博客 700

第1.1节 构网型 vs. 跟网型:根本区别与技术原理

构网型与跟网型变流器的本质区别在于控制哲学与等效模型。跟网型变流器采用"电压跟随,电流注入"策略,等效为受控电流源,依赖电网电压作为参考,在弱电网下存在稳定性问题。构网型变流器采用"自主同步,电压构建"策略,等效为受控电压源,能主动构建电压和频率,具备自主运行、黑启动和电网支撑能力。两者的核心差异体现在同步机制、电网依赖性、故障行为和系统支撑等方面。构网型变流器更适合高比例新能源、弱电网和微电网场景,是未来电力系统的重要发展方向。

技术工作者 1395

A - Meteor Shower的解答

A - Meteor Shower 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 fi...

Sunshine_H的帅气博客 466

AArch64 ARM64 寄存器介绍

除了上述通用寄存器,还有FPSR(浮点状态寄存器)和 FPCR(浮点控制寄存器)等。X29寄存器和X30寄存器在函数调用和堆栈帧的管理中扮演着关键角色。AArch64架构提供了31个通用寄存器,每个寄存器都可以用作64位X寄存器(X0~X30)或32位W寄存器(W0~W30)。使用X寄存器将导致64位的计算,使用W寄存器将导致32位的计算。EL0、EL1、EL2和EL3是AArch64架构中定义的四个特权级别(Exception Levels),用于支持不同的执行环境和特权级别。

lml435035844的博客 1万+

全球第一开源云ERP Odoo操作手册 模块安装和界面汉化指南

Odoo 的功能包含在不同的模块中, 刚创建的数据库除 Odoo 的核心模块外, 其它模块并没有被安装到系统中,你可以根据需要安装它们。本节将介绍安装模块和界面汉化的相关知识。

weixin_44793763的博客 2073

每日一题4.14 P2895 [USACO08FEB]Meteor Shower

P2895 [USACO08FEB]Meteor Shower S 题目描述 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 s...

u014567353的博客 476

深入解析AndroidManifest.xml与device_admin.xml:打造完美DeviceOwner应用配置

本文深入解析了Android企业级开发中DeviceOwner应用的核心配置文件AndroidManifest.xml与device_admin.xml。详细阐述了如何精准声明DeviceAdminReceiver、定义管理策略,并提供了通过ADB命令进行设置与验证的实战指南,帮助开发者构建权限完整、配置正确的设备所有者应用。

ice55的博客 680

BFS---陨石坠落

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 locatio

zw201909250425的博客 443

避坑指南:STM32移植CanFestival时那些没人告诉你的细节(含TJA1050硬件适配)

本文深入解析STM32移植CanFestival协议栈时的关键细节与TJA1050硬件适配技巧,涵盖AVR残留代码清理、定时器中断优化及CAN通信稳定性提升方案。特别针对TJA1050在特定波特率下的信号畸变问题提供实战解决方案,帮助开发者规避移植过程中的隐形陷阱,实现工业级稳定通信。

work3的博客 761

链式中介效应结果解读:从Bootstrap置信区间到实践意义

本文深入解析链式中介效应结果的解读方法,从Bootstrap置信区间的应用到实践意义的阐述。通过实际案例展示如何分析总效应、直接效应和间接效应,并强调效应量评估的重要性。文章还提供了构建逻辑解释的三步法,以及敏感性分析和报告撰写的实用技巧,帮助研究者准确解读链式中介效应。

weixin_33690367的博客 718

POJ3669-Meteor Shower-BFS

原题链接 Meteor Shower Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18098 Accepted: 4704 DescriptionBessie hears that an extraordinary meteor shower is coming; reports say tha

HowieMen的博客 400

Adams二次开发实战指南:从CMD到Python与插件开发全解析

本文全面解析Adams二次开发的三种核心方法:CMD命令、Python脚本和C++插件开发。通过实战案例展示如何实现参数批量修改、自动化仿真流程和定制功能开发,显著提升多体动力学仿真效率。特别适合需要处理复杂建模和重复性任务的工程师学习参考。

weixin_29235923的博客 127

poj3669 Meteor Shower(BFS)

题目链接:poj3669 Meteor Shower 我只想说这题WA了后去看讨论才发现的坑点,除了要注意原点外,流星范围题目给的是[0,300],到302的位置就绝对安全了。。。 1 #include<cstdio> 2 #include<cmath> 3 #include<queue> 4 #include<cstr...

dhe56357的博客 256

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

Poj3669 meteor shower bfs

题目链接:http://poj.org/problem?id=3669 题目大意:女主牛在起点(0,0),天气预报说今天会下流星雨,女主牛得逃生,流星砸下来它的附近的四个格点的位置都会被毁灭,以后不能再走,女主牛每次可走在地图范围内的四个格点,问女主牛至少得走多少歩才能安全。 分析:这个题目至少,明显是bfs,但又和以前的bfs不一样,唔。。。后来看了别人的结题报告才明白要自己够遭地图,先构建

Na_OH的博客 530

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

【和小安一起刷题】POJ3669 Meteor Shower ----广度优先搜索(BFS)

01、题目描述 小伙伴们大家好,这里是和 【和小安一起刷题 】 系列栏目,今天我们要解决的是一道广搜题目——POJ3669 Meteor Shower。在解决题目前,你需要知道BFS的基本框架和C++< queue >库的简单使用。 题目链接: POJ3669 Meteor Shower. 题目描述:一场流星雨即将袭来,流星会对每个击中的地方以及其周围(上下左右四格)造成破坏。小明开始时位于(0, 0)位置,准备逃到一处不会被袭击到的地方(小明只能在第一象限内移动)。已知每移动1个单位长度需要1

一枝之栖 355

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

POJ3669Meteor Shower【BFS】

Language: Default Meteor Shower Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11570   Accepted: 3163 Description Bessie hears that an extraordinary

走一步再走一步 770

POJ3669 Meteor Shower【BFS】

Meteor Shower Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37006 Accepted: 9463 Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteors will crash into earth and destroy anything they hit. Anxi

海岛Blog 431

[bfs] POJ 3669 Meteor Shower

题目 Meteor Shower Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31700 Accepted: 8158 Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteor...

weixin_44197056的博客 189

POJ 3669-Meteor Shower(BFS)

C - Meteor Shower Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3669 Appoint description:  System Crawler  (2016-04-28) Descr

梧桐下的四叶草 681

BFS——Poj 3669 Meteor Shower

题目 Meteor Shower Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18796 Accepted: 4879 Description Bessie hears that an extraordinary meteor shower is coming;...

点滴 521

[bfs] poj 3669 Meteor Shower

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

童凌的技术博客 685

POJ-3669 Meteor Shower BFS

http://poj.org/problem?id=3669 题意:巨大流星雨即将袭来。每个流星会对击中的地方以及周围(上下左右四格)造成破坏。Bessie开始时位于(0, 0)位置,并希望逃到一处不会被袭击到的地方(在第一象限内)。已知每移动一格需要1个时间单位,被流星破坏后的地方不能再进入。给出M个流星在T时刻击中的地方(X, Y),问Bessie能否逃到安全的地方,若能输出最短时间

ACM_10000h的专栏 1023

poj 3669 Meteor Shower 【BFS】

Meteor Shower Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12517   Accepted: 3386 Description Bessie hears that an extraordinary meteor shower is coming

世界很大 489

BFS搜索:POJ No 3669 Meteor Shower

BFS搜索:POJ No 3669 Meteor Shower #include <iostream> #include <cstring> #include <queue> #include <cstdio> #include <cstdlib> #include <...

豆子 186

[POJ] 3669 Meteor Shower [BFS]

DescriptionBessie 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 w

chutzpah 298
上一篇: AOJ0558 Cheese 广度优先搜索
下一篇: Seven Puzzle AOJ 0121 反向BFS
RPG_Zero
博客等级 码龄8年 77粉丝 181原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值