POJ 3369 Meteor Shower (BFS,水题)
题意:给定 n 个炸弹的坐标和爆炸时间,问你能不能逃出去。如果能输出最短时间。
析:其实这个题并不难,只是当时没读懂,后来读懂后,很容易就AC了。
主要思路是这样的,先标记所有的炸弹的位置,和时间,在数组中标记就好,只要赋值给它的爆炸时间就好,注意如果有多个,要赋值最小的那个,
然后用BFS走就行了。
代码如下:
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
using namespace std ; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f3f;
const double eps = 1e-8;
const int maxn = 300 + 15;
const int dr[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};
int m, n;
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
int s[maxn][maxn];
int vis[maxn][maxn];
struct node{
int x, y, d;
node(int xx, int yy, int dd) : x(xx), y(yy), d(dd) { }
}; int bfs(){
queue<node> q;
q.push(node(0, 0, 0));
vis[0][0] = 1;
if(!s[0][0]) return -1;
while(!q.empty()){
node u = q.front(); q.pop();
for(int i = 0; i < 4; ++i){
int x = u.x + dr[i];
int y = u.y + dc[i];
if(x < 0 || y < 0 || x > 305 || y > 305 || vis[x][y] || s[x][y] <= u.d+1) continue;
vis[x][y] = 1;
if(s[x][y] == INF) return u.d+1;
q.push(node(x, y, u.d+1));
}
}
return -1;
} int main(){
while(scanf("%d", &n) == 1){
int x, y, t;
for(int i = 0; i < 305; ++i)
for(int j = 0; j < 305; ++j)
s[i][j] = INF;
for(int i = 0; i < n; ++i){
scanf("%d %d %d", &x, &y, &t);
if(x > 0) s[x-1][y] = min(t, s[x-1][y]);
if(y > 0) s[x][y-1] = min(t, s[x][y-1]);
s[x+1][y] = min(t, s[x+1][y]);
s[x][y+1] = min(t, s[x][y+1]);
s[x][y] = min(t, s[x][y]);
}
memset(vis, 0, sizeof(vis));
printf("%d\n", bfs());
}
return 0;
}
POJ 3369 Meteor Shower (BFS,水题)的更多相关文章
- POJ 3669 Meteor Shower BFS 水~
http://poj.org/problem?id=3669 题目大意: 一个人从(0,0)出发,这个地方会落下陨石,当陨石落在(x,y)时,会把(x,y)这个地方和相邻的的四个地方破坏掉,求该人到达 ...
- POJ 3984 - 迷宫问题 - [BFS水题]
题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ...
- POJ 3669 Meteor Shower BFS求最小时间
Meteor Shower Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31358 Accepted: 8064 De ...
- POJ 3126 Prime Path bfs, 水题 难度:0
题目 http://poj.org/problem?id=3126 题意 多组数据,每组数据有一个起点四位数s, 要变为终点四位数e, 此处s和e都是大于1000的质数,现在要找一个最短的路径把s变为 ...
- POJ 3669 Meteor Shower(流星雨)
POJ 3669 Meteor Shower(流星雨) Time Limit: 1000MS Memory Limit: 65536K Description 题目描述 Bessie hears ...
- poj 3080 Blue Jeans(水题 暴搜)
题目:http://poj.org/problem?id=3080 水题,暴搜 #include <iostream> #include<cstdio> #include< ...
- poj 3669 Meteor Shower(bfs)
Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...
- POJ 3669 Meteor Shower (BFS+预处理)
Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...
- 题解报告:poj 3669 Meteor Shower(bfs)
Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...
随机推荐
- [LeetCode#249] Group Shifted Strings
Problem: Given a string, we can "shift" each of its letter to its successive letter, for e ...
- Android 开机动画启动过程详解
Android 开机会出现3个画面: 1. Linux 系统启动,出现Linux小企鹅画面(reboot)(Android 1.5及以上版本已经取消加载图片): 2. Android平台启动初始化,出 ...
- UVa 11426 (欧拉函数 GCD之和) GCD - Extreme (II)
题意: 求sum{gcd(i, j) | 1 ≤ i < j ≤ n} 分析: 有这样一个很有用的结论:gcd(x, n) = i的充要条件是gcd(x/i, n/i) = 1,因此满足条件的x ...
- eval绑定decimal数据后,如何去掉后面没有意义的0?
假如有个数字是 25.00 就应该只显示 25 ,而如果是25.3 则还是显示 25.3 Score.ToString("g0") 这样就可以去掉 decimal 后面多 ...
- 页面多个Jquery版本共存的冲突问题,解决方法!
示例如下: <script type="text/javascript" src="jquery.js"></script> <s ...
- liunx之zip格式的解压命令
zip -r myfile.zip ./* 将当前目录下的所有文件和文件夹全部压缩成myfile.zip文件,-r表示递归压缩子目录下所有文件. 2.unzip unzip -o -d /home/s ...
- Javascript Utils.js
\ var Utils ={//字符串格式化StringFormat: function () {if (arguments.length == 0)return null;var str = arg ...
- javascript将json转字符串
例如: var last=obj.toJSONString(); //将JSON对象转化为JSON字符 或者 var last=JSON.stringify(obj); //将JSON对象转化为JSO ...
- 1048 图的宽度优先遍历序列 c语言
描述 图(graph)是数据结构 G=(V,E),其中V是G中结点的有限非空集合,结点的偶对称为边(edge):E是G中边的有限集合.设V={0,1,2,……,n-1},图中的结点又称为顶点(vert ...
- [原创]C语言利用pcre正则表达式库
C语言使用正则表达式,可以利用pcre库,这个比较不错的哦. 在使用过程中,利用python进行测试正则表达式是否OK,后发现出现了问题.如下所示: regex.c:11:18: warning: u ...