poj 3669 火星撞地球问题 bfs算法
题意:火星撞地球,你要跑到一个永远安全的地方,求最短时间
思路:bfs+预处理
这题的数据量比较大,所以需要进行预处理
- 对每个位置设上时间(被撞的最早时间) 未被撞的设为-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);
} - 进队列的时候时间要+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算法的更多相关文章
- bfs(火星撞地球)
Meteor Shower 链接:https://ac.nowcoder.com/acm/contest/997/I来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 327 ...
- 【POJ 3669 Meteor Shower】简单BFS
流星雨撞击地球(平面直角坐标第一象限),问到达安全地带的最少时间. 对于每颗流星雨i,在ti时刻撞击(xi,yi)点,同时导致(xi,yi)和上下左右相邻的点在ti以后的时刻(包括t)不能再经过(被封 ...
- poj 2251 三维地图最短路径问题 bfs算法
题意:给你一个三维地图,然后让你走出去,找到最短路径. 思路:bfs 每个坐标的表示为 x,y,z并且每个点都需要加上时间 t struct node{ int x, y, z; int t;}; b ...
- 【POJ - 3669】Meteor Shower(bfs)
-->Meteor Shower Descriptions: Bessie听说有场史无前例的流星雨即将来临:有谶言:陨星将落,徒留灰烬.为保生机,她誓将找寻安全之所(永避星坠之地).目前她正在平 ...
- POJ 3669 Meteor Shower【BFS】
POJ 3669 去看流星雨,不料流星掉下来会砸毁上下左右中五个点.每个流星掉下的位置和时间都不同,求能否活命,如果能活命,最短的逃跑时间是多少? 思路:对流星雨排序,然后将地图的每个点的值设为该点最 ...
- POJ 3126 Prime Path(BFS算法)
思路:宽度优先搜索(BFS算法) #include<iostream> #include<stdio.h> #include<cmath> #include< ...
- BFS算法(——模板习题与总结)
首先需要说明的是BFS算法(广度优先算法)本质上也是枚举思想的一种体现,本身效率不是很高,当数据规模很小的时候还是可以一试的.其次很多人可能有这样的疑问,使用搜索算法的时候,到底选用DFS还是BFS, ...
- 【2018.07.30】(广度优先搜索算法/队列)学习BFS算法小记
一些BFS参考的博客: https://blog.csdn.net/ldx19980108/article/details/78641127 https://blog.csdn.net/u011437 ...
- POJ 3669 Meteor Shower(流星雨)
POJ 3669 Meteor Shower(流星雨) Time Limit: 1000MS Memory Limit: 65536K Description 题目描述 Bessie hears ...
随机推荐
- 【持续更新】Java 时间相关
直接上代码: import java.util.*; import java.text.SimpleDateFormat; public class HelloWorld { public stati ...
- Kendo MVVM 数据绑定(十一) Value
Kendo MVVM 数据绑定(十一) Value Value 绑定可以把 ViewModel 的某个属性绑定到 DOM 元素或某个 UI 组件的 Value 属性.当用户修改 DOM 元素或 UI ...
- mui选择时间、选择日期
完整代码: <header class="mui-bar mui-bar-nav"> <a class="mui-action-back mui-i ...
- Android 调节图片工具类
package com.base.changeimage; import android.graphics.Bitmap; import android.graphics.Canvas; import ...
- Docker 给运行中的容器添加映射端口
方法1 1.获得容器IP 将container_name 换成实际环境中的容器名 docker inspect `container_name` | grep IPAddress 2. iptable ...
- excel如何显示多个独立窗口
https://blog.csdn.net/tigaobansongjiahuan8/article/details/76861084
- 洛谷 P2905 [USACO08OPEN]农场危机Crisis on the Farm
题目描述 约翰和他的奶牛组建了一只乐队“后街奶牛”,现在他们正在牧场里排练.奶牛们分成一堆 一堆,共1000)堆.每一堆里,30只奶牛一只踩在另一只的背上,叠成一座牛塔.牧场 里还有M(1 < ...
- 使用JS的画布制作一个瞄准镜
<canvas width="600" height="500" id="myCanvas"></canvas> & ...
- 为DataGridView控件实现复选功能
实现效果: 知识运用: DataGridViewCheckBoxColumn类 实现代码: private class Fruit { public int Price { get; set; } p ...
- 安全错误使用CORS在IE10与Node和Express及XMLHttpRequest: 网络错误 0x4c7, 操作已被用户取消
在IE下:VUE项目,后台替换为https请求之后,vue热更新请求挂起,控制台报错:XMLHttpRequest: 网络错误 0x4c7, 操作已被用户取消.但是chrome与Firefox正常 当 ...