Fire! (双bfs+预处理)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2671
题目:
Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the maze neglected to create a fire escape plan. Help Joe escape the maze. Given Joe’s location in the maze and which squares of the maze are on fire, you must determine whether Joe can exit the maze before the fire reaches him, and how fast he can do it. Joe and the fire each move one square per minute, vertically or horizontally (not diagonally). The fire spreads all four directions from each square that is on fire. Joe may exit the maze from any square that borders the edge of the maze. Neither Joe nor the fire may enter a square that is occupied by a wall.
Input
The first line of input contains a single integer, the number of test cases to follow. The first 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 fire 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 IMPOSSIBL
题意:Joe要逃离着火的森林,Joe和火都只能往上下左右四个方向转移,Joe到达边界即可离开,问最小逃离步数,如果不能输出IMPOSSIBL。坑点:是逃离后的步数,因而要在到达边界的步数上+1,火有多个(因为这个WA了好久==!)。
思路:先用t数组预处理出火到达每个地方的时间,然后再对Joe进行bfs即可。
代码实现如下:
#include <queue>
#include <cstdio>
#include <cstring>
using namespace std; const int inf = 0x3f3f3f3f;
int T, r, c, ans, sx, sy;
char mp[][];
int vis[][], t[][]; struct node{
int x, y;
int step;
}nw, nxt; int dx[] = {, -, , }, dy[] = {, , , -}; void bfs1() {
queue<node> q;
for(int i = ; i< r; i++) {
for(int j = ; j < c; j++) {
if(mp[i][j] == 'F') {
nw.x = i, nw.y = j;
t[i][j] = ;
q.push(nw);
}
}
}
while(!q.empty()) {
nw = q.front(), q.pop();
for(int i = ; i < ; i++) {
nxt.x = nw.x + dx[i], nxt.y = nw.y + dy[i];
if(nxt.x >= && nxt.x < r && nxt.y >= && nxt.y < c && mp[nxt.x][nxt.y] != '#' && t[nxt.x][nxt.y] > t[nw.x][nw.y] + ) {
t[nxt.x][nxt.y] = t[nw.x][nw.y] + ;
q.push(nxt);
}
}
}
} void bfs2(int x, int y) {
nw.x = x, nw.y = y, nw.step = ;
vis[x][y] = ;
queue<node> q;
q.push(nw);
while(!q.empty()) {
nw = q.front(), q.pop();
if(nw.x == || nw.x == r - || nw.y == || nw.y == c - ) {
ans = nw.step + ;
return;
}
for(int i = ; i < ; i++) {
nxt.x = nw.x + dx[i], nxt.y = nw.y + dy[i];
if(nxt.x >= && nxt.x < r && nxt.y >= && nxt.y <c && mp[nxt.x][nxt.y] != '#' && nw.step + < t[nxt.x][nxt.y] && vis[nxt.x][nxt.y] == ) {
vis[nxt.x][nxt.y] = ;
nxt.step = nw.step + ;
q.push(nxt);
}
}
}
} int main() {
scanf("%d", &T);
while(T--) {
scanf("%d%d", &r, &c);
for(int i = ; i < r; i++) {
scanf("%s", mp[i]);
for(int j = ; j < c; j++) {
if(mp[i][j] == 'J') {
sx = i, sy = j;
}
}
}
memset(vis, , sizeof(vis));
memset(t, inf, sizeof(t));
ans = inf;
bfs1();
bfs2(sx, sy);
if(ans >= inf) printf("IMPOSSIBLE\n");
else printf("%d\n", ans);
}
return ;
}
Fire! (双bfs+预处理)的更多相关文章
- uva11624 Fire! (bfs预处理)
题目链接:https://vjudge.net/problem/UVA-11624 题意:给一个1000×1000的矩阵,有几个着火点和Joe,着火点和Joe每个单位时间均移动一个单位,求Joe逃出的 ...
- BZOJ-1189 紧急疏散evacuate BFS预处理+最大流+二分判定+神建模!!
绝世污题,垃圾题,浪费我一整天青春! 1189: [HNOI2007]紧急疏散evacuate Time Limit: 10 Sec Memory Limit: 162 MB Submit: 1262 ...
- HDU - 1430 魔板 (bfs预处理 + 康托)
对于该题可以直接预处理初始状态[0, 1, 2, 3, 4, 5, 6, 7]所有可以到达的状态,保存到达的路径,直接打印答案即可. 关于此处的状态转换:假设有初始状态为2,3,4,5,0,6,7,1 ...
- 【2016 ICPC亚洲区域赛北京站 E】What a Ridiculous Election(BFS预处理)
Description In country Light Tower, a presidential election is going on. There are two candidates, ...
- HDU 3533 Escape(BFS+预处理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3533 题目大意:给你一张n* m的地图,人在起点在(0,0)要到达终点(n,m)有k(k<=10 ...
- bzoj 1415(概率dp和bfs预处理)
感觉挺经典的一道题目. 先用 bfs 预处理下一步走到的位置.因为每一步走法都是固定的,所以可以用dp的方法来做. 1415: [Noi2005]聪聪和可可 Time Limit: 10 Sec M ...
- UVA 11624 - Fire! 图BFS
看题传送门 昨天晚上UVA上不去今天晚上才上得去,这是在维护么? 然后去看了JAVA,感觉还不错昂~ 晚上上去UVA后经常连接失败作死啊. 第一次做图的题~ 基本是照着抄的T T 不过搞懂了图的BFS ...
- FZU - 2150 Fire Game bfs+双起点枚举
题意,10*10的地图,有若干块草地“#”,草地可以点燃,并在一秒后点燃相邻的草地.有墙壁‘·‘阻挡.初始可以从任意两点点火.问烧完最短的时间.若烧不完输出-1. 题解:由于100的数据量,直接暴力. ...
- CSUOJ2031-Barareh on Fire(双向BFS)
Barareh on Fire Submit Page Description The Barareh village is on fire due to the attack of the virt ...
随机推荐
- Debian实验机 常用命令
1.开启中文输入法 fcitx 2. 开启无线连接 wicd 3. 远程连接 ssh root@XXX.XXX.XXX.XXX 4. 启动Ulipad ~/ulipad-master# python ...
- TCP系列24—重传—14、F-RTO虚假重传探测
一.虚假重传 在一些情况下,TCP可能会在没有数据丢失的情况下初始化一个重传,这种重传就叫做虚假重传(Spurious retransmission).发生虚假重传的原因可能是包传输中重排序.传输中发 ...
- 软工网络15团队作业4——Alpha阶段敏捷冲刺-2
一.当天站立式会议照片: 二.项目进展 昨天已完成的工作: 微信公众号平台注册"小程序"的账号; 下载微信官方的小程序开发工具,这个是编辑小程序和上传审核小程序必须的工具; 大家一 ...
- mysql授权远程连接
查一下你的MYSQL用户表里, 是否允许远程连接 1.授权 mysql>grant all privileges on *.* to 'root'@'%' identified by ...
- linux解压zip
用 unzip 的先安装 yum install -y unzip #unzip file.zip -d /root -d指解压路径 ,不写的话默认当前目录
- 关于c++中public & private方法调用问题
class IDNoIdentifier { public: IDNoIdentifier(); ~IDNoIdentifier(); typedef vector<cv::Rect> C ...
- Android基础------通知栏
前言:Android通知栏提示笔记 通知几乎是每一款app都拥有的功能 1.发送通知 发送一个通知栏必须用到两个类: NotificationManager . Notification. Noti ...
- 【bzoj3754】Tree之最小方差树 最小生成树
题目描述 给出一张无向图,求它的一棵生成树,使得选出的所有边的方差最小.输出这个最小方差. 输入 第一行两个正整数N,M 接下来M行,每行三个正整数Ui,Vi,Ci N<=100,M<=2 ...
- 虚拟机如何进入BIOS
- [luogu5048] [Ynoi2019模拟赛] Yuno loves sqrt technology III
题目链接 洛谷. Solution 思路同[BZOJ2724] [Violet 6]蒲公英,只不过由于lxl过于毒瘤,我们有一些更巧妙的操作. 首先还是预处理\(f[l][r]\)表示\(l\sim ...