UVA 11642 Fire!
Fire!
This problem will be judged on UVA. Original ID: 11624
64-bit integer IO format: %lld Java class name: Main
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 Specification
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.
Sample Input
2
4 4
####
#JF#
#..#
#..#
3 3
###
#J.
#.F
Output Specification
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.
Output for Sample Input
3
IMPOSSIBLE 解题:bfs...让火先走,再人走。。判断走出去的是人还是火
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = ;
char mp[maxn][maxn];
bool vis[maxn][maxn];
int n,m;
struct node{
int x,y,t;
bool isFire;
node(int a = ,int b = ,int c = ,bool isfire = true){
x = a;
y = b;
t = c;
isFire = isfire;
}
};
queue<node>q;
bool isIn(int x,int y){
return x < n && x >= && y < m && y >= ;
}
int bfs(){
static const int dir[][] = {-,,,,,-,,};
while(!q.empty()){
node now = q.front();
q.pop();
for(int i = ; i < ; ++i){
int tx = now.x + dir[i][];
int ty = now.y + dir[i][];
if(isIn(tx,ty)){
if(!vis[tx][ty]){
vis[tx][ty] = true;
q.push(node(tx,ty,now.t+,now.isFire));
}
}else if(!now.isFire) return now.t+;
}
}
return -;
}
int main(){
int kase,px,py;
scanf("%d",&kase);
while(kase--){
scanf("%d %d",&n,&m);
memset(vis,false,sizeof(vis));
while(!q.empty()) q.pop();
for(int i = ; i < n; ++i){
scanf("%s",mp[i]);
for(int j = ; j < m; ++j){
if(mp[i][j] == 'F'){
vis[i][j] = true;
q.push(node(i,j,,true));
}else if(mp[i][j] == '#') vis[i][j] = true;
else if(mp[i][j] == 'J') vis[px = i][py = j] = true;
}
}
q.push(node(px,py,,false));
int ans = bfs();
if(ans == -) puts("IMPOSSIBLE");
else printf("%d\n",ans);
}
return ;
}
/*
2
4 4
####
#JF#
#..#
#..#
3 3
###
#J.
#.F */
UVA 11642 Fire!的更多相关文章
- UVa 11624 Fire!(着火了!)
UVa 11624 - Fire!(着火了!) Time limit: 1.000 seconds Description - 题目描述 Joe works in a maze. Unfortunat ...
- BFS(两点搜索) UVA 11624 Fire!
题目传送门 /* BFS:首先对火搜索,求出火蔓延到某点的时间,再对J搜索,如果走到的地方火已经烧到了就不入队,直到走出边界. */ /******************************** ...
- UVA - 11624 Fire! bfs 地图与人一步一步先后搜/搜一次打表好了再搜一次
UVA - 11624 题意:joe在一个迷宫里,迷宫的一些部分着火了,火势会向周围四个方向蔓延,joe可以向四个方向移动.火与人的速度都是1格/1秒,问j能否逃出迷宫,若能输出最小时间. 题解:先考 ...
- UVA 11624 - Fire! 图BFS
看题传送门 昨天晚上UVA上不去今天晚上才上得去,这是在维护么? 然后去看了JAVA,感觉还不错昂~ 晚上上去UVA后经常连接失败作死啊. 第一次做图的题~ 基本是照着抄的T T 不过搞懂了图的BFS ...
- 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[][]是多余的,完全可以用 ...
- UVA 11624 Fire! (bfs)
算法指南白书 分别求一次人和火到达各个点的最短时间 #include<cstdio> #include<cstring> #include<queue> #incl ...
- (简单) UVA 11624 Fire! ,BFS。
Description Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the ow ...
随机推荐
- 2、java注释、标识符、数据类型、类型转换
一.三种注释:单行注释.多行注释.文档注释(只能在类前或者方法前,@author作者) 二.java使用的编码为unicode码[0-65535] 包含ASCII码,在0-255中 ASCII码( ...
- pydev 安装
pydev断断续续空余时间安装了好几天,终于安装上了,需要注意的几点有, 1.插件地址 http://update-production-pydev.s3.amazonaws.com/pydev/up ...
- Cannot find the class file for javax.servlet.ServletContext.
当eclipse中新导入的Java Project的时候.往往会碰到各种各样的问题,以下是个典型的问题: Cannot find the class file for javax.servlet.Se ...
- sass03 变量、样式导入
demo1.scss @import "css.css"; //导入css文件 @import "http://ss/xx"; //导入css文件 @impor ...
- hdoj--3183--A Magic Lamp(贪心)
A Magic Lamp Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- 12.红黑树set
#include <iostream> //红黑树(自动保证平衡,自动生成平衡查找树) #include <set> #include <cstring> #inc ...
- jquery-validator中js校验及标签校验的使用
jquery-validator中js校验及标签校验的使用: 1.项目中引入jquery.validate.js 官方网站:http://bassistance.de/ http://jquery ...
- 《剑指offer》二进制中1的个数
一.题目描述 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 二.牛客网提供的框架 class Solution { public: int NumberOf1(int n) { } ...
- linux中的swap
1. 也许你会经常遇到一个经典的swap大小设置问题(比如狗血的面试题). 很多人多会说内存的2倍.. 但是个人认为一般而言 swap 不要设置太大,最好不要超过4G. 2. 进程申请内存不足时,发现 ...
- 列表的所有的input,将它的值以键值对的形式存放到一个数组里
要求的格式 代码块 $('.btn-confirm').on('tap',function(){ var arr={}; var name = $("input[name='insuranc ...