BFS搜索:POJ No 3669 Meteor Shower
#include <iostream>
#include <cstring>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std; const int maxn = ;
const int INF = ;
int M;
struct Meteor {
int X, Y;
int Time; //Time_i
Meteor(int X = , int Y = , int T = ) :
X(X), Y(Y), Time(T) {}
bool operator < (const Meteor& b) { // 升序排序
return Time < b.Time;
}
} meteor[ + ];
int dir[][] = {{-, }, {, }, {, }, {, -}, {, }};
bool used[maxn][maxn];
int field[maxn][maxn]; //标记好每个位置被流星砸(以及扩散到)的时间
int last;
int ans; void input();
void solve();
bool check(int r, int c, int T);
int BFS(int r, int c, int T); void input()
{
memset(used, false, sizeof(used));
for (int i = ; i < maxn; i++) {
for (int j = ; j < maxn; j++) {
field[i][j] = INF;
}
}
scanf("%d", &M);
for (int i = ; i < M; i++) {
scanf("%d%d%d", &meteor[i].X, &meteor[i].Y, &meteor[i].Time);
}
sort(meteor, meteor + M); //按照时间升序排序
last = meteor[M - ].Time; //最后被毁灭的时间 for (int i = ; i < M; i++) //M个火球
{
for (int j = ; j < ; j++) { //5个方向
int x = meteor[i].X + dir[j][],
y = meteor[i].Y + dir[j][];
if ( check(x, y, meteor[i].Time) ) {
field[x][y] = meteor[i].Time; //x,y位置和周围扩散位置,破坏的时间
}
}
}
} bool check(int r, int c, int T)
{
return r >= && c >= && field[r][c] > T;
} int BFS(int r, int c, int T)
{
used[r][c] = true; //从原点开始
queue<Meteor> que;
Meteor cur;
cur.X = r, cur.Y = c, cur.Time = T;
que.push(cur); //开始的位置,和时间 while (!que.empty())
{
Meteor m = que.front(); que.pop();
for (int i = ; i < ; i++) { //遍历4个方向, 因为最后一个方向,是在原地
cur = m;
cur.X = m.X + dir[i][],
cur.Y = m.Y + dir[i][]; //周围位置
cur.Time++; //移到下个位置的时间
//如果火球落到该位置的时间>当前人的时间,表示还有机会移动
if (check(cur.X, cur.Y, cur.Time) && !used[cur.X][cur.Y]) {
used[cur.X][cur.Y] = true;
//说明当前位置时间永远不会被炸到
//last是最后被炸到的时间
if (field[cur.X][cur.Y] > last) {
return cur.Time; //则返回当前到达安全的时间
}
que.push(cur); //否则入队列
}
}
}
return -; //不存在安全位置
} void solve()
{
input();
if (field[][] == ) { //原点就被毁灭, 反应时间为0
printf("-1\n");
}
else {
printf("%d\n", BFS(, , ));
}
} int main()
{
solve();
return ;
}
分析:1. 还是经典的BFS问题,主要是要 对被摧毁的位置的时间进行记录(先升序处理)(以及波及到的位置进行时间标志).
2. 人行打算走下一步的时候, 先判断是否时间允许,允许标志为访问过(允许的时候,需要当前位置时间是否已经超过了 最后被毁灭位置的时间,是则返回 到达该安全位置的时间)。不允许则添加到队列中。
题目链接: http://poj.org/problem?id=3669
参考了这篇博客: http://www.cnblogs.com/ZefengYao/p/5935161.html
BFS搜索:POJ No 3669 Meteor Shower的更多相关文章
- POJ 3669 Meteor Shower(流星雨)
POJ 3669 Meteor Shower(流星雨) Time Limit: 1000MS Memory Limit: 65536K Description 题目描述 Bessie hears ...
- POJ 3669 Meteor Shower (BFS+预处理)
Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...
- 题解报告:poj 3669 Meteor Shower(bfs)
Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...
- POJ 3669 Meteor Shower BFS求最小时间
Meteor Shower Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31358 Accepted: 8064 De ...
- poj 3669 Meteor Shower(bfs)
Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...
- poj 3669 Meteor Shower
Me ...
- POJ 3669 Meteor Shower【BFS】
POJ 3669 去看流星雨,不料流星掉下来会砸毁上下左右中五个点.每个流星掉下的位置和时间都不同,求能否活命,如果能活命,最短的逃跑时间是多少? 思路:对流星雨排序,然后将地图的每个点的值设为该点最 ...
- POJ 3669 Meteor Shower BFS 水~
http://poj.org/problem?id=3669 题目大意: 一个人从(0,0)出发,这个地方会落下陨石,当陨石落在(x,y)时,会把(x,y)这个地方和相邻的的四个地方破坏掉,求该人到达 ...
- 【POJ 3669 Meteor Shower】简单BFS
流星雨撞击地球(平面直角坐标第一象限),问到达安全地带的最少时间. 对于每颗流星雨i,在ti时刻撞击(xi,yi)点,同时导致(xi,yi)和上下左右相邻的点在ti以后的时刻(包括t)不能再经过(被封 ...
随机推荐
- Python中,os.listdir遍历纯数字文件乱序如何解决
Python中,os.listdir遍历纯数字文件乱序如何解决 日常跑深度学习视觉相关代码时,常常需要对数据集进行处理.许多图像文件名是利用纯数字递增的方式命名.通常所用的排序函数sort(),是按照 ...
- Alpha阶段综合报告
1.版本测试报告 1.1在测试过程中总共发现了多少Bug?每个类别的Bug分别为多少个? Bug分类 Bug内容 Fixed 编辑博文时改变文字格式会刷新界面 Can’t reproduced 无 N ...
- 0422数学口袋精灵bug发现
团队成员的博客园地址: 蔡彩虹:http://home.cnblogs.com/u/caicaihong/ 曾治业:http://www.cnblogs.com/zzy999/ 蓝叶:http://w ...
- PAT 甲级 1137 Final Grading
https://pintia.cn/problem-sets/994805342720868352/problems/994805345401028608 For a student taking t ...
- gitlab邮箱服务配置
配置邮箱服务的用途 有合并请求时,邮件通知 账号注册时,邮件验证 修改密码时,通过邮件修改 配置步骤: .开启QQ邮箱的smtp服务(不建议使用163邮箱,发几次之后,就不能发送) 设置-->账 ...
- HTTP压力测试工具wrk的安装及测试
本次在VMware虚拟机的CentOS6.3系统中进行安装wrk压测工具,具体如下: 一.预先安装需求项 为了安装顺利,不受权限的限制,首先可以把用户切换为root用户# su + 输入root用户对 ...
- [转帖]UsingDiskspdforSQLServer --- 改天做下练习 官方文档.
Using DiskSpd in SQL Server environments Writer: Robert Beene Contributors: Jose Barreto, Ramu Konid ...
- implement min heap
class MinHeap{ private ArrayList<Integer> arr; private int DEFAULT_LEN = 10; public MinHeap(){ ...
- Java的checked exception与unchecked exception
在Java中exception分为checked exception和unchecked异常,两者有什么区别呢? 从表象来看, checked异常就是需要在代码中try ... catch ...的异 ...
- 【jQuery插件】pagepiling滚屏插件使用
pagePiling.js 这款 jQuery 插件可以帮助前端开发人员轻松实现全屏滚动效果. 支持所有的主流浏览器,包括IE8+,支持移动设备.下面详细讲解下pagePiling.js的使用步骤. ...