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. http和WebSocket

    有关http和WebSocket 的区别网上有很多的质料. 个人在此仅仅是记录以下自己的学习心得,自己的理解. 1. http协议是用在应用层的协议,他是基于tcp协议的,http协议建立链接也必须要 ...

  2. Android中如何实现EditText的自动换行

    要实现EditText的自动换行需要实现如下设置: <EditText android:id="@+id/function_lifingcost_edit_txtRemark" ...

  3. English trip V1 - 7.My dream car 我梦想的车 Teacher:Lamb Key: famous for

    中华In this lesson you will learn to describe an object(目标). 课上内容(Lesson) famous for   以…著称,闻名 国家(名词)  ...

  4. 启动Eclipse时发生An internal error occurred during: "Initializing Java Tooling"错误

    详细提示如下: An internal error occurred during: "Initializing Java Tooling". Illegal exception ...

  5. python-day33--进程间通信(IPC)

    方式:队列(推荐使用) 一.基本情况 1.可以往队列里放任意类型的数据 2. 队列:先进先出 3. q=Queue(3) #可以设置队列中最多可以进入多少个值,也可以不设置 q.put('first' ...

  6. VS2010-自定义控件

    1.自定义控件 (1)新建—项目,项目模板选择“类库”,取名smControl,填写项目文件保存目录,点击确定 (2)完成后在解决方案资源管理器中删除类Class1 (3)添加“用户控件”——在解决方 ...

  7. 一、Object类

    1.Object类是所有类的父类 声明一个类的时候,实际上已经默认继承了Object类 package property; public class Hero extends Object{ Stri ...

  8. OC 属性关键字

    // 属性关键字 /* 1.ARC下(自动管理内存,自动引用计数)(IOS) strong,weak ----------------------------------------- 2.MRC下( ...

  9. C# 格式化表

    C#格式化数值结果表 字符 说明 示例 输出 C 货币 string.Format("{0:C3}", 2) $2.000 D 十进制 string.Format("{0 ...

  10. 快速切题 cf118A

    这教导人们一定要看题,要看题,元音包含了‘y’,完毕,要看题啊 #include <cstring> #include <cstdio> #include <cctype ...