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. es6模块 nodejs模块和 typescript模块

    es6模块 import和export nodejs模块 require和module.exports typescript模块 module和export

  2. php7 编译 win32ps 模块

    碰到了很多问题 ,但最终都解决了,感觉不错. 1)下载 php source, php sdk, 以及 win32ps的源代码 2) 参照下面的连接进行编译. https://wiki.php.net ...

  3. centos7: vsftpd安装及启动

    安装: yum -y install vsftpd service vsftpd start  注意这句:centos7不能这么启动了 chkconfig vsftpd on vsftpd.conf配 ...

  4. 雷林鹏分享:Ruby 模块(Module)

    Ruby 模块(Module) 模块(Module)是一种把方法.类和常量组合在一起的方式.模块(Module)为您提供了两大好处. 模块提供了一个命名空间和避免名字冲突. 模块实现了 mixin 装 ...

  5. python打印cookies获取cookie

    def test_002_buy_ticket(self): data = [{"}] print(data) data = json.dumps(data) cookies = self. ...

  6. 基因家族收缩和扩张分析 & Selective loss pathway & 泛基因组

    套路 这通常就是基因组组装后的必做分析,通过比较基因组学的手段进行分析,可以知道所研究物种在进化过程中哪些核心基因家族发生了变化,从而导致了其特殊的适应性机制的形成. 参考: Extremotoler ...

  7. pip 源 替换国内源

    网上收集来的pip源地址: 阿里云 http://mirrors.aliyun.com/pypi/simple/中国科技大学 https://pypi.mirrors.ustc.edu.cn/simp ...

  8. LeetCode--136--只出现一次的数字

    问题描述: 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次.找出那个只出现了一次的元素. 说明: 你的算法应该具有线性时间复杂度. 你可以不使用额外空间来实现吗? 示例 1: ...

  9. Ubuntu18.04配置静态ip遇到的报错

    说明: 因为很多测试环境在这个Ubuntu系统上装着,却由于虚拟服务器的重启.断电,每次Ip发生变化,就得更新环境,所以需要把该环境的ip配置成静态的,一劳永逸. 根据以往配置Ubuntu静态ip的经 ...

  10. python-day47--pymysql模块

    一.安装导入 #安装 pip3 install pymysql 二.使用 1 .基本使用 import pymysql # 链接,拿到游标 conn=pymysql.connect(host='loc ...