洛谷P2895Meteor Shower S

2024-02-04 21:38
文章标签 洛谷 shower p2895meteor

本文主要是介绍洛谷P2895Meteor Shower S,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

输入输出样例

输入 #1

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

输出 #1

5
题意:在一个农场里,有M个流星落下来,第i个流星,坐标为(xi,yi),下落时间为ti, 流星下落时会砸到5个点,自己的点,该点的上下左右点。砸下来之后,贝西不能走这5个点。但是在砸下来之前可以走。
思路:流星下落时间进行排序,早的在前,然后把下落的时间放进一个dangerous数组,注意如果一个格子同时被多个流星影响到,考虑最早的格子。然后bfs

#include <iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;const int N = 50000 + 100;//最大流星个数
struct star
{int x, y, t;//坐标以及落地时间
} a[N], b[N];struct point
{int x, y, step;//逃亡到达的点坐标,所用的时间
};bool cmp(const star a, const star b)
{return a.t < b.t;//时间小的在前
}int M, MaxTime;
int dangerous[500][500];//第k秒之后有危险
int step[500][500] = {-1};//-1表示当前格子未被访问int bfs(int x, int y)
{queue<point>q;q.push((point){x, y, 0});int p = 0;//指向数组当前的位置while(!q.empty()){point cur = q.front();//队首位置q.pop();if(step[cur.x][cur.y] != -1)continue;step[cur.x][cur.y] = cur.step;if(dangerous[cur.x][cur.y] == 1010)//当前位置没危险return cur.step;int ne[4][2] = {{-1, 0}, {1, 0}, {0, - 1}, {0, 1}};for(int i = 0; i < 4; i++){int dx = cur.x + ne[i][0];int dy = cur.y + ne[i][1];//不走出去,在流星掉下来之前走过去,还没走过if(dx >= 0 && dx <= 302 && dy >= 0 && dy <= 302 && dangerous[dx][dy] > cur.step + 1 && step[dx][dy] == -1){q.push((point){dx, dy, cur.step + 1});}}}return -1;
}int main()
{scanf("%d", &M);for(int i = 0; i < M; i++){scanf("%d%d%d", &b[i].x, &b[i].y, &b[i].t);}sort(b, b + M, cmp);//按时间对陨石进行排序int j = 1;a[0] = b[0];//把b[0]给a[0]for(int i = 1; i < M; i++){if(a[j].x != b[i].x || a[j].y != b[i].y || a[j].t != b[i].t){a[j++] = b[i];}}M = j;MaxTime = a[M-1].t;for(int i = 0; i <= 302; i++){for(int j = 0; j <= 302; j++){dangerous[i][j] = 1010;step[i][j] = -1;}}for(int i = 0; i < M; i++){int x = a[i].x, y = a[i].y;if(dangerous[x][y] == 1010)//未被之前的流星影响到dangerous[x][y] = a[i].t;if(x - 1 >= 0 && dangerous[x - 1][y] == 1010)//不在范围内 未被之前的流星影响到dangerous[x - 1][y] = a[i].t;if(x + 1 <= 300 && dangerous[x + 1][y] == 1010)dangerous[x + 1][y] = a[i].t;if(y - 1 >= 0 && dangerous[x][y - 1] == 1010)dangerous[x][y - 1] = a[i].t;if(y + 1 <= 300 && dangerous[x][y + 1] == 1010)dangerous[x][y + 1] = a[i].t;}printf("%d", bfs(0,0));return 0;}

这篇关于洛谷P2895Meteor Shower S的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/678866

相关文章

高精度计算(代码加解析,洛谷p1601,p1303)除法待更新

目录 高精度加法 高精度减法 高精度乘法 高精度加法 我们知道在c++语言中任何数据类型都有一定的表示范围。当两个被加数很大时,正常加法不能得到精确解。在小学,我们做加法都采用竖式方法。那么我们也只需要按照加法进位的方式就能得到最终解。 8 5 6+ 2 5 5-------1 1 1 1 加法进位: c[i] = a[i] + b[i];if(c[i] >=

洛谷 凸多边形划分

T282062 凸多边形的划分 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 先整一个半成品,高精度过两天复习一下补上 #include <iostream>#include <algorithm>#include <set>#include <cstring>#include <string>#include <vector>#include <map>

能量项链,洛谷

解释:  环形dp问题还是考虑将环拉直,可以参考我上一篇文章:环形石子合并 [2 3 5 10 2] 3 5 10 将环拉直,[]内是一个有效的区间,可以模拟吸收珠子的过程,         如[2 3 5] <=>(2,3)(3,5)    2是头,3是中间,5是尾 len >= 3:因为最后[2 10 2]是最小的可以合并的有效区间 len <= n + 1因为[2 3

洛谷P5490扫描线

0是最小的数字,将一个线段看成一个区间,对于一个矩形,从下扫到上,入边为1,而出边为-1,意思是将这个区间上的所有点加1(区间修改).把线段表示为Line[i],其中记录了l,r,h,tag,左右端点,高度,入边还是出边(1或-1) 那么每次区间修改后不为0的区间它的值可能是1,2,3或者是其它数字,这不好统计,可以将它转化一下,0是不是表示没有被覆盖过的地方,我们只要统计0的个数然后用总长减去

挤牛奶洛谷uasco

题目描述 三个农民每天清晨5点起床,然后去牛棚给3头牛挤奶。第一个农民在300秒(从5点开始计时)给他的牛挤奶,一直到1000秒。第二个农民在700秒开始,在 1200秒结束。第三个农民在1500秒开始2100秒结束。期间最长的至少有一个农民在挤奶的连续时间为900秒(从300秒到1200秒),而最长的无人挤奶的连续时间(从挤奶开始一直到挤奶结束)为300秒(从1200秒到1500秒)。

洛谷刷题(7)

P8738 [蓝桥杯 2020 国 C] 天干地支 题目描述 古代中国使用天干地支来记录当前的年份。 天干一共有十个,分别为:甲(jiǎ)、乙(yǐ)、丙(bǐng)、丁(dīng)、戊 (wù)、己(jǐ)、庚(gēng)、辛(xīn)、壬(rén)、癸(guǐ)。 地支一共有十二个,分别为:子(zǐ)、丑(chǒu)、寅(yín)、卯(mǎo)、辰(chén)、巳(sì)、午(wǔ)、

C++ 洛谷 哈希表(对应题库:哈希,hash)习题集及代码

马上就开学了,又一个卷季,不写点东西怎么行呢?辣么,我不准备写那些dalao们都懂得,熟练的,想来想去,最终还是写哈希表吧!提供讲解&题目&代码解析哦! 奉上题目链接: 洛谷题目 - 哈希,hash 1、哈希、哈希表(hash)简介 哈希(Hash)是一种将任意长度的输入映射为固定长度输出的算法。哈希函数的输出值称为哈希值或散列值。哈希函数具有以下特性: 确定性:对于相同的输入

洛谷p2236彩票题解

题目描述 某地发行一套彩票。彩票上写有 1 到 M 这 M 个自然数。彩民可以在这 M 个数中任意选取 N 个不同的数打圈。每个彩民只能买一张彩票,不同的彩民的彩票上的选择不同。 每次抽奖将抽出两个自然数 X 和 Y。如果某人拿到的彩票上,所选 N 个自然数的倒数和,恰好等于 X/Y,则他将获得一个纪念品。 已知抽奖结果 X 和 Y。现在的问题是,必须准备多少纪念品,才能保证支付所有获奖者的

洛谷p2994题解 [USACO10OCT] Dinner Time S

题目描述 Farmer John's N (1 <= N <= 1,000) cows conveniently numbered 1..N are participating in the IOI in Bulgaria. The cows like the Bulgarian sun and are enjoying their holiday. All seems well. This

洛谷刷题(4)

P1089 [NOIP2004 提高组] 津津的储蓄计划 题目描述 津津的零花钱一直都是自己管理。每个月的月初妈妈给津津 300 元钱,津津会预算这个月的花销,并且总能做到实际花销和预算的相同。 为了让津津学习如何储蓄,妈妈提出,津津可以随时把整百的钱存在她那里,到了年末她会加上 20% 还给津津。因此津津制定了一个储蓄计划:每个月的月初,在得到妈妈给的零花钱后,如果她预计到这个月的月末手中