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

思路: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. Spark Mllib里如何建立向量标签(图文详解)

    不多说,直接上干货! 注意: val pos = LabeledPoint(1, vd) val neg = LabeledPoint(2, vs) 除了这两种建立向量标签.还可以从数据库中获取固定格 ...

  2. C# Mutex互斥锁

    Mutex 构造函数 (Boolean, String, Boolean) public Mutex ( bool initiallyOwned, string name, out bool crea ...

  3. ngnix集群产生的问题

    还可使用zookeper解决

  4. dynomite:高可用多数据中心同步

    https://github.com/Netflix/dynomite Dynomite, inspired by Dynamo whitepaper, is a thin, distributed ...

  5. 详解Java构造方法为什么不能覆盖,我的钻牛角尖病又犯了....

    一 看Think in Java,遇到个程序 class Egg2 { protected class Yolk { public Yolk() { System.out.println(" ...

  6. JS移动端浏览器取消右划后退的几种方法

    在开发过程中,发现我们公司所使用的APP有点BUG,在APP中打开网页.H5应用之后,处于首页时,轻微的右划触发了后退事件,导致直接退出网页或者H5应用的页面,这样使得很多需要交互的手势没办法使用.本 ...

  7. Android商城开发系列(七)—— 使用RecyclerView展示首页数据

    前面我们讲到了使用OkHttp请求网络和FastJson解析数据了,接下来我们就开始把获取到的数据通过数据适配器展示在页面上了.Adapter是用来帮助填充数据的中间桥梁,简单点说就是:将各种数据以合 ...

  8. js构造方法

    <!DOCTYPE html><html><head><meta charset="UTF-8"><title>Java ...

  9. 【TensorFlow入门完全指南】模型篇·线性回归模型

    首先呢,进行import,对于日常写代码来说,第二行经常写成:import numpy as np,这样会更加简洁.第三行import用于绘图. 定义了学习率.迭代数epoch,以及展示的学习步骤,三 ...

  10. 为了少点击几次,自己写了一个Chrome插件

    缘由 chrome应用商店有三款二维码插件,自己一直使用的第一款.这三款插件有且只有一个功能就是生成当前页面的URL的二维码. 其实这个功能基本上满足了需要移动端开发在微信里打开页面进行调试的情况. ...