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. LIST动态表格画线(动态列)

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  2. WPF基础学习第二天(高级控件)

    1.Menu菜单控件 Exp1: Code: <Window x:Class="菜单Menu.MainWindow" xmlns="http://schemas.m ...

  3. jQuery的.click,.bind,.unbind,.on,.off,.delegate,.undelegate

    .click与.bind .click和.bind都是给每个元素绑定事件,对于只绑定一个click事件,.bind事件的简写就是.click那种方式. 这两种方式都会出现两个问题: 第一个问题,如果要 ...

  4. IQ一个人的智力和对科学知识的理解掌握程度。 EQ对环境和个人情绪的掌控和对团队关系的运作能力。 AQ挫折商 一个人面对困境时减除自己的压力、渡过难关的能力。

    IQ: Intelligence Quotient 智商 一个人的智力和对科学知识的理解掌握程度. EQ: Emotional Quotient 情商 一个人对环境和个人情绪的掌控和对团队关系的运作能 ...

  5. equals()和hashcode()

    默认调用的情况: 1.集合在存放对象时,首先判断hashcode(),再判断equals如果都是true,认为是相同的两个元素不进行存储. 删除对象时,将从hashcode指定位置查找再删除 2.在h ...

  6. VB6.0

    1. 安装包来自 http://msdn.itellyou.cn/ 下载的文件为: sc_vb6_ent_cd1.iso sc_vb6_ent_cd2.iso 2.安装时,"请输入产品的 I ...

  7. bootstrap学习笔记<七>(图标,图像)

    图像 bootstrap为图像预加载提供了很简洁的样式.(CDN:http://placehold.it/140x140:) PS:该CDN链接后的140x140可以根据网站需要更换合适的尺寸.例如: ...

  8. hdu3007Buried memory(最小圆覆盖)

    链接 普通的暴力复杂度达到O(n^4),对于这题肯定是不行的. 解法:随机增量算法 参考http://www.2cto.com/kf/201208/149602.html algorithm:A.令C ...

  9. Mysql delete,truncate,drop

    1.delete 是DML(Data Manipulation Language),每次删除一行,作为事务记录在日志,可以回滚.delete from xxx 2.truncate是DDL(Data ...

  10. 代码中特殊的注释技术——TODO、FIXME和XXX的用处

    本文内容概要: 代码中特殊的注释技术--TODO.FIXME和XXX的用处. 前言:今天在阅读Qt  Creator的源代码时,发现一些注释中有FIXME英文单词,用英文词典居然查不到其意义!实际上, ...