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 ...
随机推荐
- 【Todo】深入理解Javascript系列
真的很好,要看 http://www.cnblogs.com/TomXu/archive/2011/12/15/2288411.html
- struct TABLE_SHARE
struct TABLE_SHARE { TABLE_SHARE() {} /* Remove gcc warning */ /** Category of this table. */ TABLE_ ...
- 【转】iOS中定时器NSTimer的使用
原文网址:http://www.cnblogs.com/zhulin/archive/2012/02/02/2335866.html 1.初始化 + (NSTimer *)timerWithTimeI ...
- 延迟加载图片的 jQuery 插件——lazyload.js
lazyload 这个 jQuery 插件,是用来缓冲加载图片的插件.如果一篇文章很长有很多图片的话,下载图片就需要很多时间.而这款插件,会检测你的滚动情况,只有你要看到那个图片的时 候,它才会从后台 ...
- #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)宏的运行机理:1. ( (TYPE *)0 ) 将零转型为TY ...
- 【转】ACE开发环境搭建
Windows平台 1) 下载ACE源码 ACE官方网址:http://www.cs.wustl.edu/~schmidt/ACE.html ACE下载地址:http://downloa ...
- POJ 2481 Cows
Cows Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 16546 Accepted: 5531 Description ...
- 《C++ primer》--第10章
习题10.21 解释map和set容器的差别,以及他们各自适用的情况. 解答: map容器和set容器的差别在于: map容器是键-值对的集合,而set容器只是键的集合: map类型适用于需要了解键与 ...
- 七中滤波方法测试matlab实现
http://blog.163.com/xiaheng0804@126/blog/static/1205282120132129471816/ 创建两个混合信号,便于更好测试滤波器效果.同时用七中滤波 ...
- android studio SDK版本的调节
android { compileSdkVersion 17 buildToolsVersion "17.0.0" defaultConfig { minSdkVersion 17 ...