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)不能再经过(被封 ...
随机推荐
- 23_IO_第23天(字节流、字符流)_讲义
今日内容介绍 1.字节流 2.字符流 01输入和输出 * A:输入和输出 * a: 参照物 * 到底是输入还是输出,都是以Java程序为参照 * b: Output * 把内存中的数据存储到持久化设备 ...
- win10频繁提示证书即将过期怎么办
最近几天每次开机都会提示许可证即将过期 ”Windows+R”打开“运行”窗口,输入“slmgr.vbs -xpr”并点击“确定”,弹出的窗口确实显示过期时间在本月1.29过期 百度各种激活方法后,发 ...
- Delphi中BCD和Currency类型
用了这些年的Delphi,竟然对Currency及TBCDField一知半解,下文给了很好的讲解,值得一读. 一. BCD类型 BCD即Binary-Coded Decimal?,在Del ...
- docker删除为<none>的镜像
$ docker stop $(docker ps -a | grep "Exited" | awk '{print $1 }') //停止容器 $ docker rm ...
- [转贴]infoQ VSTS被拆成5个部分,以Azure DevOps服务形式推出
VSTS被拆成5个部分,以Azure DevOps服务形式推出 http://www.infoq.com/cn/news/2018/09/vsts-divide5parts-azuredevops?u ...
- Construct BST from given preorder traversal
Given preorder traversal of a binary search tree, construct the BST. For example, if the given trave ...
- scala 有 + 运算符吗? - 03
scala 有运算符吗? 答案是没有. package com.msym /** * Created by ACER on 2017/7/4. */ object Demo { def main(ar ...
- ADO.NET:C#/SQL Server
1.首次要准备的(工具)是:a.Microsoft Visual Studio Ultimate 2012;b.Microsoft SQL Server Management Studio ; 2.首 ...
- BZOJ3157/BZOJ3516 国王奇遇记(矩阵快速幂/数学)
由二项式定理,(m+1)k=ΣC(k,i)*mi.由此可以构造矩阵转移,将mi*ik全部塞进去即可,系数即为组合数*m.复杂度O(m3logn),因为大常数喜闻乐见的T掉了. #include< ...
- BZOJ 2527 Meteors | 整体二分
BZOJ 2527 Meteors 题意 一个圆环上有m个位置,编号为1~m,分别属于n个国家. 有k个时刻,每个时刻都会给圆环上的一个区间中每个位置的值加上一个数. 每个国家有一个目标,问对于每个国 ...