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)的更多相关文章

  1. UVA 11624 BFS的妙用

    题意: 迷宫里起火了,有若干个障碍物,有多个起火点,起火点每经过一个时间间隔就向它的上下左右相邻的格子扩散. 有个倒霉的人好像叫做“Joe”,他要逃出来,他每次可以向上下左右任意移动一格,但是即要避开 ...

  2. UVa 11624 (BFS) Fire!

    也是一个走迷宫的问题,不过又有了点变化. 这里迷宫里有若干把火,而且火每秒也是向四个方向蔓延的.问人是否能走出迷宫. 我用了两遍BFS,第一遍把所有着火的格子加入队列,然后计算每个格子着火的时间. 第 ...

  3. UVA - 11624 Fire! bfs 地图与人一步一步先后搜/搜一次打表好了再搜一次

    UVA - 11624 题意:joe在一个迷宫里,迷宫的一些部分着火了,火势会向周围四个方向蔓延,joe可以向四个方向移动.火与人的速度都是1格/1秒,问j能否逃出迷宫,若能输出最小时间. 题解:先考 ...

  4. BFS(两点搜索) UVA 11624 Fire!

    题目传送门 /* BFS:首先对火搜索,求出火蔓延到某点的时间,再对J搜索,如果走到的地方火已经烧到了就不入队,直到走出边界. */ /******************************** ...

  5. UVA 11624 UVA 10047 两道用 BFS进行最短路搜索的题

    很少用bfs进行最短路搜索,实际BFS有时候挺方便得,省去了建图以及复杂度也降低了O(N*M): UVA 11624 写的比较挫 #include <iostream> #include ...

  6. E - Fire! UVA - 11624(bfs + 记录火到达某个位置所需要的最小时间)

    E - Fire! UVA - 11624 题目描述 乔在迷宫中工作.不幸的是,迷宫的一部分着火了,迷宫的主人没有制定火灾的逃跑计划.请帮助乔逃离迷宫.根据乔在迷宫中的位置以及迷宫的哪个方块着火,你必 ...

  7. UVa 11624 Fire!(着火了!)

    UVa 11624 - Fire!(着火了!) Time limit: 1.000 seconds Description - 题目描述 Joe works in a maze. Unfortunat ...

  8. UVa 11624,两次BFS

    题目链接:http://vjudge.net/contest/132239#problem/A 题目链接:https://uva.onlinejudge.org/external/116/11624. ...

  9. UVA 11624 Fire! bfs 难度:0

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

随机推荐

  1. git学习笔记07-冲突了怎么办-那就解决冲突呗

    比如一个人自己创建了分支feature1进行修改提交之后提交,另一个人在master上修改然后提交. master分支和feature1分支各自都分别有新的提交,变成了这样: 这种情况下,Git无法执 ...

  2. 转载java源代码阅读方法

    刚才在论坛不经意间,看到有关源码阅读的),不禁又有一种激动. 源码阅读,我觉得最核心有三点:技术基础+强烈的求知欲+耐心. 说到技术基础,我打个比方吧,如果你从来没有学过Java,或是任何一门编程语言 ...

  3. yii 验证码的使用

    在HappyController 中加入 public function actions(){ return array( // captcha action renders the CAPTCHA ...

  4. Python学习笔记day5

    模块 1.自定义模块 自定义模块就是在当前目录下创建__init__.py这个空文件,这样外面的程序才能识别此目录为模块包并导入 上图中libs目录下有__init__.py文件,index.py程序 ...

  5. iOS - UIProgressView

    前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIProgressView : UIView <NSCoding> @available(iOS 2. ...

  6. [转载] 分享D瓜哥最近攒的资料(架构方面)

    原文: http://www.diguage.com/archives/41.html 扯扯蛋 以前见过零零散散地介绍一些知名网站架构的分析文章.最近D瓜哥也想研究一下各大知名网站的架构.所以,就搜集 ...

  7. 初识redis——mac下搭建redis环境

    一.redis简介 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合)和zset(有 ...

  8. linux终端下 编译c语言程序

    linux终端下,编译C语言程序步骤为: 采用vi进行源代码编写,编写完成后,:wq存盘退出,如: vi test.c 在命令行下,运行gcc编译程序,生成执行码,如: gcc  -o test te ...

  9. hdu 1058 Humble Numbers

    这题应该是用dp来做的吧,但一时不想思考了,写了个很暴力的,类似模拟打表,然后排序即可,要注意的是输出的格式,在这里wa了一发,看了别人的代码才知道哪些情况没考虑到. #include<cstd ...

  10. new Date()时间对象

    <!DOCTYPE html><html lang="zh-CN"><head> <meta charset="UTF-8&qu ...