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
fire 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
 
这道破题真是烦人啊,找bug找了好久,真的是要看清楚条件,首先这道题与以往的bfs题不同,人在动,火也在动,而且起火的地方不只一处,题目说了portions,一开始我忽略了,发现了这个条件可以对火的蔓延bfs一次看看火到达每一个格子需要多久,然后对人的行动队列搜索,如果能在火蔓延到之前赶到这个格子就可以走的。
还有就是了解图的范围最少只有一个格子。
 
 
代码:
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <queue>
using namespace std;
char pi[][];
int vis[][];
int fire[][];
struct que
{
int x,y,t;
}temp;
int dir[][]={,,,,,-,-,};
int main()
{
int T,m,n,sx=,sy=,flag; queue<que> q;
//std::ios::sync_with_stdio(false);
//std::cin.tie(0);
cin>>T;
while(T--)
{
cin>>n>>m;
flag=;
memset(vis,,sizeof(vis));
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
cin>>pi[i][j];
fire[i][j]=;
}
}
while(!q.empty())q.pop();
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(pi[i][j]=='J')sx=i,sy=j;
else if(pi[i][j]=='F')
{
fire[i][j]=;
temp.x=i,temp.y=j,temp.t=;
q.push(temp);
}
else if(pi[i][j]=='#')vis[i][j]=;
}
}
while(!q.empty())
{
for(int k=;k<;k++)
{
int tx=q.front().x+dir[k][];
int ty=q.front().y+dir[k][];
if(tx<||ty<||tx>=n||ty>=m||vis[tx][ty]||q.front().t+>=fire[tx][ty])continue;
temp.x=tx,temp.y=ty,temp.t=q.front().t+;
fire[tx][ty]=temp.t;
q.push(temp);
}
q.pop();
}
while(!q.empty())q.pop();
temp.x=sx,temp.y=sy,temp.t=;
vis[sx][sy]=;
q.push(temp);
while(!q.empty())
{
if(!q.front().x||!q.front().y||q.front().x==n-||q.front().y==m-)
{
flag=;
cout<<q.front().t+<<endl;
break;
}
for(int i=;i<;i++)
{
int tx=q.front().x+dir[i][];
int ty=q.front().y+dir[i][];
if(tx<||ty<||tx>=n||ty>=m||vis[tx][ty]||fire[tx][ty]<=q.front().t+)continue;
temp.x=tx,temp.y=ty,temp.t=q.front().t+;
vis[tx][ty]=;
q.push(temp);
}
q.pop();
}
if(flag==)cout<<"IMPOSSIBLE"<<endl;
}
}

Fire! 又是图 bfs的更多相关文章

  1. QDUOJ 生化危机 邻接表存图+BFS

    生化危机 发布时间: 2015年10月10日 18:05   时间限制: 1000ms   内存限制: 256M 描述 X博士想造福人类, 研发一种可以再生肢体的药物, 可是很不幸......研究失败 ...

  2. BZOJ_3073_[Pa2011]Journeys_线段树优化建图+BFS

    BZOJ_3073_[Pa2011]Journeys_线段树优化建图+BFS Description Seter建造了一个很大的星球,他准备建造N个国家和无数双向道路.N个国家很快建造好了,用1..N ...

  3. UVA 11624 - Fire! 图BFS

    看题传送门 昨天晚上UVA上不去今天晚上才上得去,这是在维护么? 然后去看了JAVA,感觉还不错昂~ 晚上上去UVA后经常连接失败作死啊. 第一次做图的题~ 基本是照着抄的T T 不过搞懂了图的BFS ...

  4. ACM: FZU 2150 Fire Game - DFS+BFS+枝剪 或者 纯BFS+枝剪

    FZU 2150 Fire Game Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  5. 算法系列之图--BFS

    广度优先搜索以源结点s为出发点,算法始终将已发现和未发现结点之间的边界,沿其广度方向向外扩展.也即算法需要在发现所有距离源结点s为k的所有结点之后才会去发现距离源结点距离为k+1的其他结点. talk ...

  6. HDU 4444 Walk (离散化建图+BFS+记忆化搜索) 绝对经典

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4444 题意:给你一些n个矩形,给你一个起点,一个终点,要你求从起点到终点最少需要转多少个弯 题解:因为 ...

  7. foj 2150 Fire Game(bfs暴力)

         Problem Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M ...

  8. hdu 4444 Walk (离散化+建图+bfs+三维判重 好题)

    Walk Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submi ...

  9. ACM:图BFS,迷宫

    称号: 网络格迷宫n行m单位列格组成,每个单元格无论空间(使用1表示),无论是障碍(使用0为了表示).你的任务是找到一个动作序列最短的从开始到结束,其中UDLR同比分别增长.下一个.左.向右移动到下一 ...

随机推荐

  1. 距离为K的节点 All Nodes Distance K in Binary Tree

    2018-07-26 17:38:37 问题描述: 问题求解: 解法一. 第一种解法是使用Graph + BFS.换言之,就是将二叉树转化为无向图,然后在无向图中使用BFS进行层次遍历即可. 这种解法 ...

  2. SqlServer和Oracle判断表和列是否存在

    SqlServer .判断表Users是否存在 if object_id(N'Users',N'U') is not null print '存在' else print '不存在' .判断表User ...

  3. GitHub 中国区前 100 名到底是什么样的人?

    转一下CSDN的文章, 这里有些人挺厉害的. http://geek.csdn.net/news/detail/66000

  4. [.NET开发] C# 读写文件

    1.C#读文件 按行读取文件: public void Read(string path) { StreamReader sr = new StreamReader(path,Encoding.Def ...

  5. 3.4 复杂的x86指令举例

    计算机组成 3 指令系统体系结构 3.4 复杂的x86指令举例 x86作为复杂指令系统的代表,自然会有不少相当复杂的指令.在这一节我们将会看到其中有代表性的一些例子. 关于复杂的x86指令,我们这里举 ...

  6. codeforces 547c// Mike and Foam// Codeforces Round #305(Div. 1)

    题意:给出数组arr和一个空数组dst.从arr中取出一个元素到dst为一次操作.问每次操作后dst数组中gcd等于1的组合数.由于数据都小于10^6,先将10^6以下的数分解质因数.具体来说从2开始 ...

  7. Confluence 6 的 Crowd 设置

    名字(Name) 输入一个有意义的服务器名字,会让你在 Crowd 服务器中更好的识别你的目录服务器: Crowd Server Example Company Crowd 服务器URL(Server ...

  8. php 浮点数

    $num = 10.4567; //第一种:利用round()对浮点数进行四舍五入 echo round($num,2); //10.46 //第二种:利用sprintf格式化字符串 $format_ ...

  9. sql 智能提示

    依次打开SSMS—>工具—>选项—>文本编辑器—>Transact-SQL—>IntelliSense—>检查右侧窗体是否启用!!

  10. iOS UI-表格控制器(UITableView)-基本使用

    tableView的常见属性 cell的常见属性 一.一般情况 #import "ViewController.h" @interface ViewController ()< ...