Fire! 又是图 bfs
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
Sample Input
2
4 4
####
#JF#
#..#
#..#
3 3
###
#J.
#.F
Sample Output
3
IMPOSSIBLE
#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的更多相关文章
- QDUOJ 生化危机 邻接表存图+BFS
生化危机 发布时间: 2015年10月10日 18:05 时间限制: 1000ms 内存限制: 256M 描述 X博士想造福人类, 研发一种可以再生肢体的药物, 可是很不幸......研究失败 ...
- BZOJ_3073_[Pa2011]Journeys_线段树优化建图+BFS
BZOJ_3073_[Pa2011]Journeys_线段树优化建图+BFS Description Seter建造了一个很大的星球,他准备建造N个国家和无数双向道路.N个国家很快建造好了,用1..N ...
- UVA 11624 - Fire! 图BFS
看题传送门 昨天晚上UVA上不去今天晚上才上得去,这是在维护么? 然后去看了JAVA,感觉还不错昂~ 晚上上去UVA后经常连接失败作死啊. 第一次做图的题~ 基本是照着抄的T T 不过搞懂了图的BFS ...
- ACM: FZU 2150 Fire Game - DFS+BFS+枝剪 或者 纯BFS+枝剪
FZU 2150 Fire Game Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- 算法系列之图--BFS
广度优先搜索以源结点s为出发点,算法始终将已发现和未发现结点之间的边界,沿其广度方向向外扩展.也即算法需要在发现所有距离源结点s为k的所有结点之后才会去发现距离源结点距离为k+1的其他结点. talk ...
- HDU 4444 Walk (离散化建图+BFS+记忆化搜索) 绝对经典
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4444 题意:给你一些n个矩形,给你一个起点,一个终点,要你求从起点到终点最少需要转多少个弯 题解:因为 ...
- foj 2150 Fire Game(bfs暴力)
Problem Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M ...
- hdu 4444 Walk (离散化+建图+bfs+三维判重 好题)
Walk Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submi ...
- ACM:图BFS,迷宫
称号: 网络格迷宫n行m单位列格组成,每个单元格无论空间(使用1表示),无论是障碍(使用0为了表示).你的任务是找到一个动作序列最短的从开始到结束,其中UDLR同比分别增长.下一个.左.向右移动到下一 ...
随机推荐
- iconv编码转换
环境:cocos2dx 3.10 1.vs环境下编译windows版本,需要增加头文件和链接库①cocos2d-x-3.10\external\win32-specific\icon\include② ...
- Codeforces 916B - Jamie and Binary Sequence (changed after round)
思路: 先取出二进制的每一位,判断总个数是不是小于等于k,如果大于k则不能构成. 通过观察可以发现,每一位的一个可以转换成下一位的两个,因为要使最大位尽可能小,所以如果最大位的所有的个数都可以转换成下 ...
- English trip -- VC(情景课)1 E Writing
Talk with a partner ['pɑːtnə] (伙伴) 与同伴说一说 Comple the words 写全单词 first second third last name area c ...
- 4-3 atom订阅源
部分章节还没有实做练习. 网上购买了安道的Rails指南中文版.联系了这个作者问了一个问题Rails5的翻译问题. try(), 判断是否存在,不存在的话返回nil.例子:pasting @perso ...
- Prefix Product Sequence CodeForces - 487C (数论,构造)
大意: 构造一个[1,2,...n]的排列, 使得前缀积模n为[0,1,...,n-1]的排列 这种构造都好巧妙啊, 大概翻一下官方题解好了 对于所有>=6的合数$n$, 有$(n-1)! \e ...
- mysql 随机获取数据并插入到数据库中
insert into result (user_id, activity_id, number) select user_id, activity_id from `activity_record` ...
- 用了皮肤控件之后,报错:容量超出了最大容量 参数名:capacity
用了皮肤控件之后,报错:容量超出了最大容量 参数名:capacity MessageBox.show()错误!!容量超出了最大容量.参数名: capacity 解决方案: 设置 skin.SkinDi ...
- java.net.SocketException: Broken pipe
java.net.SocketException: Broken pipe 生产上遇到一个问题,socket发生Broken pipe错误,如下 这个问题跟踪了好几个月,始终没有模拟出为什么会发生Br ...
- java深浅拷贝
转载:http://atjava.iteye.com/blog/1722501 首先我们看看浅拷贝和深拷贝的定义 浅拷贝:只复制一个对象,对象内部存在的指向其他对象数组或者引用则不复制 深拷贝:对象, ...
- James Whittaker:经营成功的测试职业生涯
转注:这篇文章出自 James A. Whittaker,Google的工程总监,负责Google部分产品的测试,包括Chrome.地图.GoogleWebApp.在加盟Google之前,James在 ...