uva 11624(bfs)
11624 - Fire!
Time limit: 1.000 seconds
Joe works in a maze. Unfortunately, portions of the maze have
caught on re, and the owner of the maze neglected to create a re
escape plan. Help Joe escape the maze.
Given Joe's location in the maze and which squares of the maze
are on re, you must determine whether Joe can exit the maze before
the re reaches him, and how fast he can do it.
Joe and the re each move one square per minute, vertically or
horizontally (not diagonally). The re spreads all four directions
from each square that is on re. Joe may exit the maze from any
square that borders the edge of the maze. Neither Joe nor the re
may enter a square that is occupied by a wall.
Input
The rst line of input contains a single integer, the number of test
cases to follow. The rst line of each test case contains the two
integers R and C, separated by spaces, with 1 R; C 1000. The
following R lines of the test case each contain one row of the maze. Each of these lines contains exactly
C characters, and each of these characters is one of:
#, a wall
., a passable square
J, Joe's initial position in the maze, which is a passable square
F, a square that is on re
There will be exactly one J in each test case.
Output
For each test case, output a single line containing `IMPOSSIBLE' if Joe cannot exit the maze before the
re reaches him, or an integer giving the earliest time Joe can safely exit the maze, in minutes.
Sample Input
2
4 4
####
#JF#
#..#
#..#
3 3
###
#J.
#.F
Sample Output
3
IMPOSSIBLE
简单bfs
开始提议理解错了,wrong了好几次,才知道原来可以有多个火源地。
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#define ll long long
#define _cle(m, a) memset(m, a, sizeof(m))
#define repu(i, a, b) for(int i = a; i < b; i++)
#define MAXN 1005
char s[MAXN];
int ma[MAXN][MAXN];
bool vis[MAXN][MAXN];
int d[][] = {{, }, {, }, {, -}, {-, }};
bool flag;
struct P
{
int x, y, s;
P() : x(), y(), s() {}
P(int x_, int y_, int s_) : x(x_), y(y_), s(s_) {}
};
P J, F;
queue<P> q, Q;
int r, c; int Judge(int x, int y)
{
if(x > && y > && x <= r && y <= c) return ;
return ;
} void init()
{
while(!q.empty()) q.pop();
while(!Q.empty()) Q.pop();
_cle(ma, );
_cle(vis, false);
flag = false;
} int bfs()
{
vis[J.x][J.y] = ;
q.push(J);
int last = ;
while(!q.empty())
{
P t = q.front();
q.pop();
if(t.x == || t.y == || t.x == r || t.y == c)
return t.s + ;
if(flag && last != t.s + )
{
int siz = Q.size();
while(siz--)
{
P p = Q.front();
Q.pop();
repu(i, , )
{
int dx = p.x + d[i][];
int dy = p.y + d[i][];
if(Judge(dx, dy) && ma[dx][dy] == )
{
ma[dx][dy] = ;
Q.push(P(dx, dy, t.s));
}
}
}
last = t.s + ;
}
repu(i, , )
{
int dx = t.x + d[i][];
int dy = t.y + d[i][];
if(Judge(dx, dy) && !vis[dx][dy] && ma[dx][dy] == )
{
vis[dx][dy] = ;
q.push(P(dx, dy, t.s + ));
}
}
}
return -;
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
init();
scanf("%d%d", &r, &c);
repu(i, , r)
{
scanf("%s", s);
repu(j, , c) if(s[j] == '.') ma[i + ][j + ] = ;
else if(s[j] == 'F')
{
Q.push(P(i + , j + , ));
flag = true;
ma[i + ][j + ] = ;
}
else if(s[j] == 'J')
{
J.x = i + , J.y = j + , J.s = ;
ma[i + ][j + ] = ;
}
}
int re = bfs();
if(re == -) printf("IMPOSSIBLE");
else printf("%d", re);
puts("");
}
return ;
}
uva 11624(bfs)的更多相关文章
- UVA 11624 BFS的妙用
题意: 迷宫里起火了,有若干个障碍物,有多个起火点,起火点每经过一个时间间隔就向它的上下左右相邻的格子扩散. 有个倒霉的人好像叫做“Joe”,他要逃出来,他每次可以向上下左右任意移动一格,但是即要避开 ...
- UVa 11624 (BFS) Fire!
也是一个走迷宫的问题,不过又有了点变化. 这里迷宫里有若干把火,而且火每秒也是向四个方向蔓延的.问人是否能走出迷宫. 我用了两遍BFS,第一遍把所有着火的格子加入队列,然后计算每个格子着火的时间. 第 ...
- UVA - 11624 Fire! bfs 地图与人一步一步先后搜/搜一次打表好了再搜一次
UVA - 11624 题意:joe在一个迷宫里,迷宫的一些部分着火了,火势会向周围四个方向蔓延,joe可以向四个方向移动.火与人的速度都是1格/1秒,问j能否逃出迷宫,若能输出最小时间. 题解:先考 ...
- BFS(两点搜索) UVA 11624 Fire!
题目传送门 /* BFS:首先对火搜索,求出火蔓延到某点的时间,再对J搜索,如果走到的地方火已经烧到了就不入队,直到走出边界. */ /******************************** ...
- UVA 11624 UVA 10047 两道用 BFS进行最短路搜索的题
很少用bfs进行最短路搜索,实际BFS有时候挺方便得,省去了建图以及复杂度也降低了O(N*M): UVA 11624 写的比较挫 #include <iostream> #include ...
- E - Fire! UVA - 11624(bfs + 记录火到达某个位置所需要的最小时间)
E - Fire! UVA - 11624 题目描述 乔在迷宫中工作.不幸的是,迷宫的一部分着火了,迷宫的主人没有制定火灾的逃跑计划.请帮助乔逃离迷宫.根据乔在迷宫中的位置以及迷宫的哪个方块着火,你必 ...
- UVa 11624 Fire!(着火了!)
UVa 11624 - Fire!(着火了!) Time limit: 1.000 seconds Description - 题目描述 Joe works in a maze. Unfortunat ...
- UVa 11624,两次BFS
题目链接:http://vjudge.net/contest/132239#problem/A 题目链接:https://uva.onlinejudge.org/external/116/11624. ...
- UVA 11624 Fire! bfs 难度:0
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
随机推荐
- C# 线程(四):生产者和消费者
From : http://kb.cnblogs.com/page/42530/ 前面说过,每个线程都有自己的资源,但是代码区是共享的,即每个线程都可以执行相同的函数.这可能带来的问题就是几个线程同时 ...
- HTML笔记(七)head相关元素<base> & <meta>
<head>元素是所有头部元素的容器. 可添加的标签有:<title>.<base>.<link>.<meta>.<script> ...
- 转一篇讲camera的 mb好多年不搞3d 都忘光了
Camera定义 游戏中,Camera用来向用户展示场景,Camera就像一个摄像机,摄像机里面的景象就是Camera的展示范围,如下图所示: 在3D空间中Camera被定义为一个位置,有一个单位“方 ...
- Python学习(10)元组
目录 Python 元组 访问元组 修改元组 删除元组 元组运算符 元组索引,截取 无关闭分隔符 元组内置函数 Python 元组 Python的元组与列表类似,不同之处在于元组的元素不能修改. 元组 ...
- JS实现复选框全选全不选以及子复选框带动全选框的选中
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- NTFS reparse point
https://en.wikipedia.org/wiki/NTFS_reparse_point NTFS HARD link: since Windows NT4: files on the sam ...
- eclipse xml 注释快捷键
注释:CTRL + SHIFT + / 撤销注释:CTRL + SHIFT + \
- hdu 4288 Coder
线段树好题,和 15 年的广东省省赛 C 题有相似之处,一开始我的思路有偏差,看了别人的博客后感觉处处技巧都是精华,主要是区间合并的技巧一时很难想到,先附上代码: #include<cstdio ...
- vi编辑器简单应用(摘抄)
摘抄于 vi编辑器的使用 (2) (3) 1 vi编辑器的基本使用 1.1 vi的启动 打开: $ vi example.c 只读打开 $ vi –R example.c 1.2 vi的工作模式 1. ...
- Bootstrap文本对齐风格
在排版中离不开文本的对齐方式.在CSS中常常使用text-align来实现文本的对齐风格的设置.其中主要有四种风格: ☑ 左对齐,取值left ☑ 居中对齐,取值center ☑ 右对齐,取值r ...