uva Fire!
算法:BFS
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
IMPOSSIBLE
注意开始有多个火堆;
代码:
- #include <iostream>
- #include <cstring>
- #include <iomanip>
- #include <stdio.h>
- #include <queue>
- #include <algorithm>
- #define INF 100000000
- using namespace std;
- char ch[1005][1005];
- int b[1005][1005],c[1004][1005],qq,cnt,d[1005][2];
- int n,m,a[4][2]={-1,0,0,-1,0,1,1,0},p,q;
- struct dot
- {
- int x,y,step;
- };
- void bfs()
- { memset(c,0,sizeof(c));
- queue<dot>que;
- dot cur,loer;
- for(int i=0;i<cnt;i++)
- {
- cur.x=d[i][0];
- cur.y=d[i][1];
- cur.step=0;
- b[cur.x][cur.y]=0;
- c[cur.x][cur.y]=1;
- que.push(cur);
- }
- while(que.size())
- {
- loer=que.front();
- que.pop();
- for(int i=0;i<4;i++)
- {
- int dx,dy;
- dx=loer.x+a[i][0];
- dy=loer.y+a[i][1];
- if(dx>=0&&dx<n&&dy>=0&&dy<m&&ch[dx][dy]=='.'&&!c[dx][dy])
- {
- cur.x=dx;cur.y=dy;cur.step=loer.step+1;
- b[dx][dy]=min(cur.step,b[dx][dy]);
- c[dx][dy]=1;
- que.push(cur);
- }
- }
- }
- }
- void bsf()
- { memset(c,0,sizeof(c));
- queue<dot>que;
- dot cur,loer;
- cur.x=p;
- cur.y=q;
- cur.step=0;
- c[p][q]=1;
- que.push(cur);
- while(que.size())
- {
- loer=que.front();
- que.pop();
- if(loer.x==0||loer.x==n-1||loer.y==0||loer.y==m-1)
- { qq=1;
- cout<<loer.step+1<<endl;
- break;
- }
- for(int i=0;i<4;i++)
- {
- int dx,dy;
- dx=loer.x+a[i][0];
- dy=loer.y+a[i][1];
- if(dx>=0&&dx<n&&dy>=0&&dy<m&&ch[dx][dy]=='.'&&!c[dx][dy]&&b[dx][dy]>loer.step+1)
- {
- cur.x=dx;
- cur.y=dy;
- cur.step=loer.step+1;
- c[dx][dy]=1;
- que.push(cur);
- }
- }
- }
- }
- int main()
- {
- int T,i,j,k;
- cin>>T;
- while(T--)
- {
- cin>>n>>m;
- cnt=0;
- for(i=0;i<n;i++)
- {
- for(j=0;j<m;j++)
- {
- cin>>ch[i][j];
- b[i][j]=INF;
- if(ch[i][j]=='J')
- {
- p=i;q=j;
- ch[i][j]='.';
- }
- else if(ch[i][j]=='F')
- {
- d[cnt][0]=i;
- d[cnt++][1]=j;
- }
- }
- }
- qq=0;
- bfs();
- bsf();
- if(qq==0) cout<<"IMPOSSIBLE"<<endl;
- }
- return 0;
- }
uva Fire!的更多相关文章
- UVa 11624 Fire!(着火了!)
UVa 11624 - Fire!(着火了!) Time limit: 1.000 seconds Description - 题目描述 Joe works in a maze. Unfortunat ...
- UVA - 11624 Fire! bfs 地图与人一步一步先后搜/搜一次打表好了再搜一次
UVA - 11624 题意:joe在一个迷宫里,迷宫的一些部分着火了,火势会向周围四个方向蔓延,joe可以向四个方向移动.火与人的速度都是1格/1秒,问j能否逃出迷宫,若能输出最小时间. 题解:先考 ...
- BFS(两点搜索) UVA 11624 Fire!
题目传送门 /* BFS:首先对火搜索,求出火蔓延到某点的时间,再对J搜索,如果走到的地方火已经烧到了就不入队,直到走出边界. */ /******************************** ...
- UVA 11642 Fire!
Fire! Time Limit: 1000ms Memory Limit: 131072KB This problem will be judged on UVA. Original ID: 116 ...
- UVA 11624 - Fire! 图BFS
看题传送门 昨天晚上UVA上不去今天晚上才上得去,这是在维护么? 然后去看了JAVA,感觉还不错昂~ 晚上上去UVA后经常连接失败作死啊. 第一次做图的题~ 基本是照着抄的T T 不过搞懂了图的BFS ...
- E - Fire! UVA - 11624(bfs + 记录火到达某个位置所需要的最小时间)
E - Fire! UVA - 11624 题目描述 乔在迷宫中工作.不幸的是,迷宫的一部分着火了,迷宫的主人没有制定火灾的逃跑计划.请帮助乔逃离迷宫.根据乔在迷宫中的位置以及迷宫的哪个方块着火,你必 ...
- UVA 11624 Fire!(广度优先搜索)
题目大意:在一个N*M的迷宫内,J代表某人(只有一个),F代表火(可能不只一个),#代表墙,火每分钟会向四周除了墙以外的地方扩散一层,问人能否在没被火烧到 之前逃出迷宫,若能逃出输出最短时间.很明显的 ...
- UVa 11624 Fire!(BFS)
Fire! Time Limit: 5000MS Memory Limit: 262144KB 64bit IO Format: %lld & %llu Description Joe ...
- uva 11624 Fire!(搜索)
开始刷题啦= = 痛并快乐着,学到新东西的感觉其实比看那些无脑的小说.电视剧有意思多了 bfs裸体,关键是先把所有的着火点放入队列,分开一个一个做bfs会超时的 发现vis[][]是多余的,完全可以用 ...
随机推荐
- 剖析c++(二) 内置类型的内存形式
分布形式以及转换时候的截断(待编辑)
- nodejs个人配置
国内镜像,飞一般的感觉!编辑 ~/.npmrc 加入下面内容 registry = http://registry.cnpmjs.org npm config set registry http:/ ...
- sql server 数据库附加时程序集错误
在数据库detach和attach的过程中,如果在建立程序集的时候选择的权限集是无限制,并且在建立程序集的时候和后来attach的时候 采用的不是同一个用户,就可能造成部分功能无法使用.原因是由于在选 ...
- Flask学习记录之Flask-Admin
相信用过Django框架的都不会忘记它强大的Admin功能,Flask-admin是一款能够与Django Admin所媲美的扩展,能够快速创建Web管理界面,实现了用户.文件增删改查等常用功能:也可 ...
- Python基础第三天
三元运算 三元运算又叫三目运算,是对简单的条件语句的缩写,例如if判断 # 标准if判断语法 if 1 == 1: name = "yes" else: name = " ...
- Python正则匹配递归获得给出目录下的特定类型的文件小技巧
需求是酱的: 输入一个目录,这个目录包含检测目录的必备信息但不准确需要获得后加工一下,如给出目录:C:\Program Files\Common Files\DESIGNER,需要检测的目录是:C:\ ...
- 【课上OJ】掉入陷阱的数
对任意一个自然数N0,先将其各位数字相加求和,再将其和乘以3后加上1,变成一个新自然数N1,然后对N1重复这种操作,可以产生新自然数N2,多次重复这种操作运算,运算结果最终会得到一个固定不变的数Nk, ...
- UC/OS操作系统 (转)
1.和其他一些著名的嵌入式操作系统不同,uC/OS-II在单片机系统中的启动过程比较简单,不像有些操作系统那样,需要把内核编译成一个映像文件写入ROM中,上电复位后,再从ROM中把文件加载到RAM中去 ...
- laravel实现第三方登录(qq登录)
首先composer安装依赖: composer require socialiteproviders/qq 注册服务提供者(同时注释掉原有的Socialite提供者): 'providers' =& ...
- JSP(二)
一.pageContext对象 1>代表当前JSP页面的运行环境, [作用域仅仅局限于当前JSP页面中,出了该JSP页面, 原PageContext域对象被销毁] 2>封装了对 ...