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

思路: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. AD中添加中文字符丝印的方法:

    一 一般中文丝印: 用快捷键L打开层管理,在View options中勾选convert special 选项: 用快捷键P,S文本中输入你要的汉字,选中ture type,在select ture ...

  2. Eclipse - 安装语言包

    Open the install wizard with 'Help' > 'Install new software...' add the Babel p2 repository: http ...

  3. c#基础2-out-ref

    //out参数要求在方法的内部 ; JiangJin(ref salary1); Console.WriteLine(salary1); Console.ReadKey(); 必须为其赋值 out.r ...

  4. NodeJS学习视频

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

  5. uvm_reg_predictor——寄存器模型(十一)

    保存寄存器的值 观察DUT寄存器值的变化. //---------------------------------------------------------------------------- ...

  6. uvm_reg_block——寄存器模型(七)

    这是寄存器模型的顶层 //------------------------------------------------------------------------ // Class: uvm_ ...

  7. LayUI 完美兼容Vue.js

    <div id="app"> <form class="layui-form" action=""> <div ...

  8. group - 用户组文件

    DESCRIPTION(描述) /etc/group 是一个ASCII码的文件,它定义了用户所属的组.文件中每行包括一条记录,其格式如下: group_name:passwd:GID:user_lis ...

  9. nginx之HTTP模块配置

     listen   指令只能使用与server字段里 如果本地调用可以监听本地Unix套接字文件,性能更加,因为不用走内核网络协议栈 listen unix:/var/run/nginx.sock; ...

  10. polygon 画图

    cityscape数据集,我现在想根据json文件中的polygon画出整个road的区域,这是运行的脚本.这个文件必须使用coco的pythonAPI的包,把这个脚本放在pythonAPI文件夹下就 ...