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

思路: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. Main函数中的参数argc,argv的使用简单解析

    本篇文章是对Main函数中的参数argc,argv的使用进行了简单的分析介绍,需要的朋友参考下: C/C++语言中的main函数,经常带有参数argc,argv,如下:  int main(int a ...

  2. Smarty中的请求变量和保留变量的使用范例

    PHP中提供的超全局数组 Smarty中对应的请求变量 $_GET               <{$smarty.get}> $_POST                         ...

  3. 大都市 meg

    Description 在经济全球化浪潮的影响下,习惯于漫步在清晨的乡间小路的邮递员Blue Mary也开始骑着摩托车传递邮件了.不过,她经常回忆起以前在乡间漫步的情景. 昔日,乡下有依次编号为1.. ...

  4. NodeJS学习视频

    腾讯课堂初级课程 https://ke.qq.com/webcourse/index.html#course_id=196698&term_id=100233129&taid=1064 ...

  5. JS和jquery获取各种屏幕的宽度和高度的代码

    Javascript: 网页可见区域宽: document.body.clientWidth 网页可见区域高: document.body.clientHeight 网页可见区域宽: document ...

  6. PC端和手机端页面的一丢丢区别

    <!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8 ...

  7. GreenDao的简单使用说明(五)多表n:m

    在设计一些比较复杂的数据库结构的时候,我们会遇到表之间是n:m的关系,就是常说的多对多的关系,最常用的情况,就是用户权限这块,日常最常见的就是学生与老师的关系了,哪么我们来看一下GreenDao中如何 ...

  8. 初识SeekBar

    SeekBar拖动条,是Progress的间接子类 <SeekBar android:id="@+id/seekBar1" android:layout_width=&quo ...

  9. c# winform 关于DataGridView的一些操作

    转自:http://heisetoufa.iteye.com/blog/405317 设置字段名 设置字段值 设定单元格表示 Error图标 设定当前单元格 取得当前单元格内容 取得当前单元格的列 I ...

  10. EF6.0注意事项

    EF6 1.必须要添加Entitiframework 2.必须要添加必须要添加Entitiframework.Sqlserver 3.单元测试一定要有配置文件里面一定要有连接字符串和初始化配置文件节点 ...