BFS(两点搜索) UVA 11624 Fire!
/*
BFS:首先对火搜索,求出火蔓延到某点的时间,再对J搜索,如果走到的地方火已经烧到了就不入队,直到走出边界。
*/
/************************************************
Author :Running_Time
Created Time :2015-8-4 8:11:54
File Name :UVA_11624.cpp
*************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int MAXN = 1e3 + ;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + ;
char maze[MAXN][MAXN];
bool vis[MAXN][MAXN];
int step[MAXN][MAXN];
int dx[] = {-, , , };
int dy[] = {, , -, };
int n, m; bool judge_f(int x, int y) {
if (x < || x > n || y < || y > m || vis[x][y] || maze[x][y] == '#') return false;
return true;
} bool judge_j(int x, int y) {
if (x < || x > n || y < || y > m) return true;
return false;
} void BFS_F(void) {
memset (step, INF, sizeof (step));
memset (vis, false, sizeof (vis));
queue<pair<int, int> > Q;
for (int i=; i<=n; ++i) {
for (int j=; j<=m; ++j) {
if (maze[i][j] == 'F') {
Q.push (make_pair (i, j)); vis[i][j] = true;
step[i][j] = ;
}
}
}
while (!Q.empty ()) {
int x = Q.front ().first, y = Q.front ().second; Q.pop ();
for (int i=; i<; ++i) {
int tx = x + dx[i], ty = y + dy[i];
if (!judge_f (tx, ty)) continue;
step[tx][ty] = step[x][y] + ;
Q.push (make_pair (tx, ty)); vis[tx][ty] = true;
}
}
} void BFS_J(void) {
memset (vis, false, sizeof (vis));
queue<pair<int, int> > Q;
for (int i=; i<=n; ++i) {
for (int j=; j<=m; ++j) {
if (maze[i][j] == 'J') {
Q.push (make_pair (i, j)); step[i][j] = ; vis[i][j] = true; break;
}
}
}
while (!Q.empty ()) {
int x = Q.front ().first, y = Q.front ().second; Q.pop ();
for (int i=; i<; ++i) {
int tx = x + dx[i], ty = y + dy[i];
if (judge_j (tx, ty)) {
printf ("%d\n", step[x][y] + ); return ;
}
if (step[x][y] + >= step[tx][ty] || vis[tx][ty] || maze[tx][ty] == '#') continue;
Q.push (make_pair (tx, ty)); step[tx][ty] = step[x][y] + ; vis[tx][ty] = true;
}
}
puts ("IMPOSSIBLE");
} int main(void) { //UVA 11624 Fire!
int T; scanf ("%d", &T);
while (T--) {
scanf ("%d%d", &n, &m);
for (int i=; i<=n; ++i) {
scanf ("%s", maze[i] + );
}
BFS_F (); BFS_J ();
} return ;
}
BFS(两点搜索) UVA 11624 Fire!的更多相关文章
- BFS(两点搜索) FZOJ 2150 Fire Game
题目传送门 题意:'#'表示草地,两个人在草地上点火,相邻的草地会烧起来,每烧一格等1秒,问最少要等几秒草地才烧完 分析:这题和UVA 11624 Fire!有点像,那题给定了两个点,这题两点不确定, ...
- UVa 11624 Fire!(着火了!)
UVa 11624 - Fire!(着火了!) Time limit: 1.000 seconds Description - 题目描述 Joe works in a maze. Unfortunat ...
- UVA - 11624 Fire! bfs 地图与人一步一步先后搜/搜一次打表好了再搜一次
UVA - 11624 题意:joe在一个迷宫里,迷宫的一些部分着火了,火势会向周围四个方向蔓延,joe可以向四个方向移动.火与人的速度都是1格/1秒,问j能否逃出迷宫,若能输出最小时间. 题解:先考 ...
- UVA 11624 Fire!【两点BFS】
Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the m ...
- UVA 11624 Fire! BFS搜索
题意:就是问你能不能在火烧到你之前,走出一个矩形区域,如果有,求出最短的时间 分析:两遍BFS,然后比较边界 #include<cstdio> #include<algorithm& ...
- UVA 11624 - Fire! 图BFS
看题传送门 昨天晚上UVA上不去今天晚上才上得去,这是在维护么? 然后去看了JAVA,感觉还不错昂~ 晚上上去UVA后经常连接失败作死啊. 第一次做图的题~ 基本是照着抄的T T 不过搞懂了图的BFS ...
- UVA 11624 Fire!(广度优先搜索)
题目大意:在一个N*M的迷宫内,J代表某人(只有一个),F代表火(可能不只一个),#代表墙,火每分钟会向四周除了墙以外的地方扩散一层,问人能否在没被火烧到 之前逃出迷宫,若能逃出输出最短时间.很明显的 ...
- UVa 11624 Fire!(BFS)
Fire! Time Limit: 5000MS Memory Limit: 262144KB 64bit IO Format: %lld & %llu Description Joe ...
- uva 11624 Fire!(搜索)
开始刷题啦= = 痛并快乐着,学到新东西的感觉其实比看那些无脑的小说.电视剧有意思多了 bfs裸体,关键是先把所有的着火点放入队列,分开一个一个做bfs会超时的 发现vis[][]是多余的,完全可以用 ...
随机推荐
- BZOJ 3754 Tree之最小方差树 MST
Description Wayne 在玩儿一个很有趣的游戏.在游戏中,Wayne 建造了N 个城市,现在他想在这些城市间修一些公路,当然并不是任意两个城市间都能修,为了道路系统的美观,一共只有M 对城 ...
- Jquery判断某个字符串是否在数组中
使用$.inArray方法判断,如果存在则返回0,不存在返回-1,结果如下: 另外也可以将数组转为字符串,并使用正则表达式处理
- codevs1128 导弹拦截
题目描述 Description 经过11 年的韬光养晦,某国研发出了一种新的导弹拦截系统,凡是与它的距离不超过其工作半径的导弹都能够被它成功拦截.当工作半径为0 时,则能够拦截与它位置恰好相同的导弹 ...
- codevs3285 转圈游戏
题目描述 Description n 个小伙伴(编号从 0 到 n-1)围坐一圈玩游戏.按照顺时针方向给 n 个位置编号,从0 到 n-1.最初,第 0 号小伙伴在第 0 号位置,第 1 号小伙伴在第 ...
- 对付 MySQL 的死连接,Sleep的进程的来源探究[转]
当前的连接数:mysql> show status like '%Threads_connected%';+-------------------+-------+| Variable_name ...
- hdu_1085_Holding Bin-Laden Captive!_201404261008
Holding Bin-Laden Captive! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- codevs——2750 心系南方灾区
2750 心系南方灾区 时间限制: 1 s 空间限制: 2000 KB 题目等级 : 青铜 Bronze 题解 查看运行结果 题目描述 Description 现在我国南方正在承受百年 ...
- Eclipse完成Maven + Spring Boot + Mybatis + jsp
Spring Boot 完成WEB项目开发 开发工具:eclipse 框架:Maven:Spring Boot:Mybatis 界面:jsp:javascript:css 前言: 在SpringBoo ...
- 网上Unused Index Script 脚本的问题
曾经使用过网上下载的脚本查询没有使用过的Index比方SQL SERVER – 2008 – Unused Index Script – Download,事实上如今看起来这个脚本是有一些问题. 脚本 ...
- for in、for和EnumerateObjectsUsingBlock遍历的区别
1.对于集合中对象数很多的情况下,for in 的遍历速度非常之快,但小规模的遍历并不明显(还没普通for循环快) 2. 如果在for in 循环里,对这个数组进行了修改的话,无论是增,删,修改数组元 ...