题意:火星撞地球,你要跑到一个永远安全的地方,求最短时间

思路:bfs+预处理

这题的数据量比较大,所以需要进行预处理

  1. 对每个位置设上时间(被撞的最早时间) 未被撞的设为-1

    for (int j = 0; j<5; j++) { //预处理
    int dx = x + dir[j][0], dy = y + dir[j][1];
    if (dx < 0 || dx >= maxn || dy < 0 || dy >= maxn) continue;
    g[dx][dy] = g[dx][dy] == -1 ? d : min(g[dx][dy], d);
    }

  2. 进队列的时候时间要+1,最先到达安全的地方为最短时间

解决问题的代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <stack>
using namespace std;
#define INF 0x7fffffff
#define LL long long
const int maxn = + ; struct node { int x, y, time; }; //点结构体
int g[maxn][maxn]; //图
int dir[][] = { , , , , , -, , , -, }; //方向数组 int bfs() {
if (g[][] == ) return -; //从(0,0)开始搜
if (g[][] == -) return ; //起点安全,不用躲了
node tmp, now;
tmp.x = tmp.y = tmp.time = ;
queue<node> q;
q.push(tmp);
while (!q.empty()) {
now = q.front();
q.pop();
for (int i = ; i<; i++) {
tmp.x = now.x + dir[i][], tmp.y = now.y + dir[i][];
tmp.time = now.time + ;
if (tmp.x < || tmp.y < || tmp.x >= maxn || tmp.y >= maxn) continue;
if (g[tmp.x][tmp.y] == -) return tmp.time;
if (tmp.time >= g[tmp.x][tmp.y]) continue;
g[tmp.x][tmp.y] = tmp.time;
q.push(tmp);
}
}
return -;
} int main() {
int n, x, y, d;
while (scanf("%d", &n) != EOF) {
memset(g, -, sizeof(g));
for (int i = ; i<n; i++) {
scanf("%d%d%d", &x, &y, &d);
for (int j = ; j<; j++) { //预处理
int dx = x + dir[j][], dy = y + dir[j][];
if (dx < || dx >= maxn || dy < || dy >= maxn) continue;
g[dx][dy] = g[dx][dy] == - ? d : min(g[dx][dy], d);
}
}
printf("%d\n", bfs());
}
return ;
}

poj 3669 火星撞地球问题 bfs算法的更多相关文章

  1. bfs(火星撞地球)

    Meteor Shower 链接:https://ac.nowcoder.com/acm/contest/997/I来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 327 ...

  2. 【POJ 3669 Meteor Shower】简单BFS

    流星雨撞击地球(平面直角坐标第一象限),问到达安全地带的最少时间. 对于每颗流星雨i,在ti时刻撞击(xi,yi)点,同时导致(xi,yi)和上下左右相邻的点在ti以后的时刻(包括t)不能再经过(被封 ...

  3. poj 2251 三维地图最短路径问题 bfs算法

    题意:给你一个三维地图,然后让你走出去,找到最短路径. 思路:bfs 每个坐标的表示为 x,y,z并且每个点都需要加上时间 t struct node{ int x, y, z; int t;}; b ...

  4. 【POJ - 3669】Meteor Shower(bfs)

    -->Meteor Shower Descriptions: Bessie听说有场史无前例的流星雨即将来临:有谶言:陨星将落,徒留灰烬.为保生机,她誓将找寻安全之所(永避星坠之地).目前她正在平 ...

  5. POJ 3669 Meteor Shower【BFS】

    POJ 3669 去看流星雨,不料流星掉下来会砸毁上下左右中五个点.每个流星掉下的位置和时间都不同,求能否活命,如果能活命,最短的逃跑时间是多少? 思路:对流星雨排序,然后将地图的每个点的值设为该点最 ...

  6. POJ 3126 Prime Path(BFS算法)

    思路:宽度优先搜索(BFS算法) #include<iostream> #include<stdio.h> #include<cmath> #include< ...

  7. BFS算法(——模板习题与总结)

    首先需要说明的是BFS算法(广度优先算法)本质上也是枚举思想的一种体现,本身效率不是很高,当数据规模很小的时候还是可以一试的.其次很多人可能有这样的疑问,使用搜索算法的时候,到底选用DFS还是BFS, ...

  8. 【2018.07.30】(广度优先搜索算法/队列)学习BFS算法小记

    一些BFS参考的博客: https://blog.csdn.net/ldx19980108/article/details/78641127 https://blog.csdn.net/u011437 ...

  9. POJ 3669 Meteor Shower(流星雨)

    POJ 3669 Meteor Shower(流星雨) Time Limit: 1000MS    Memory Limit: 65536K Description 题目描述 Bessie hears ...

随机推荐

  1. E. Mike and Foam 容斥原理

    http://codeforces.com/problemset/problem/548/E 这题是询问id,如果这个id不在,就插入这个id,然后求a[id1] ,  a[id2]互质的对数. 询问 ...

  2. Codeforces Round #377 (Div. 2) 被坑了

    http://codeforces.com/contest/732/problem/B 题目要求任意两个连续的日子都要 >= k 那么如果a[1] + a[2] < k,就要把a[2]加上 ...

  3. Spark Mllib里的分布式矩阵(行矩阵、带有行索引的行矩阵、坐标矩阵和块矩阵概念、构成)(图文详解)

    不多说,直接上干货! Distributed  matrix : 分布式矩阵 一般能采用分布式矩阵,说明这数据存储下来,量还是有一定的.在Spark Mllib里,提供了四种分布式矩阵存储形式,均由支 ...

  4. 【Java】 jar解压与压缩

    jar解压与压缩 命令格式:jar {c t x u f }[ v m e 0 M i ][-C 目录]文件名 # 解压,到当前目录 jar -xvf source.jar # 打包,不进行压缩 ja ...

  5. 结合源码看nginx-1.4.0之nginx内存管理详解

    目录 0. 摘要 1. nginx内存结构设计 2. nginx内存数据结构 3. nginx内存管理原理 4. 一个简单的内存模型 5. 小结 6. 参考资料 0. 摘要 内存管理,是指软件运行时对 ...

  6. GIT SSH免登录密码实现更新(git pull)、推送(git push)操作

     一.使用场景 现在有两台服务器A和B,在A服务器上搭建有git版本代码仓库,现要实现B服务器SSH免密码登录A服务器,并能够从A服务器拉取.推送代码! 二.操作步骤 1.在B服务器项目根目录下执行以 ...

  7. 如何删除 CentOS 6 更新后产生的多余的内核?

    第一种方法:通过命令的方式解决多余的内核 1.首先查看当前内核的版本号: [root@jxatei ~]# uname  -a Linux jxatei.server2.6.32-573.1.1.el ...

  8. 【LeetCode】2.Add Two Numbers 链表数相加

    题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...

  9. 如何在Sierra运行 Specials K 的patch

    https://github.com/ApolloZhu/CORE-Keygen-and-Special-K-for-Sierra-Utility/blob/master/Special%20K%20 ...

  10. Codeforces Round #Pi (Div. 2) 567E President and Roads ( dfs and similar, graphs, hashing, shortest paths )

    图给得很良心,一个s到t的有向图,权值至少为1,求出最短路,如果是一定经过的边,输出"YES",如果可以通过修改权值,保证一定经过这条边,输出"CAN",并且输 ...