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 ...
随机推荐
- Python Tkinter 基础控件学习
# -*- coding: utf-8 -*- from Tkinter import * def btn_click(): b2['text'] = 'clicked' evalue = e.get ...
- USACO 1.2 Palindromic Squares (进制转换,回文)
/* ID:twd30651 PROG:palsquare LANG:C++ */ #include<iostream> #include<fstream> #include& ...
- backtrack5实现局域网DNS欺骗
前言:不得不说Linux下的神器挺多,越来越喜欢Linux了.. . 測试环境 linux backtrack 5 windows xp 先在Linux下开 ...
- 曲根英语万词---二、evoke
曲根英语万词---二.evoke 一.总结 一句话总结:evoke v.唤起,引起 词根:-voc-, -vok- [词根含义]:声音,叫喊 1.consecrate? v,供奉,奉为神圣 -ate, ...
- Dictionary as a set of counters
Suppose you are given a string and you want to count how many times each letters appears. There are ...
- 提高realm存储速率
我的数据量大约有2.5M,但是完全存储到数据库差不多用了11秒,有没有比较好的方法提高存储效率 提高realm存储速率 >> android这个答案描述的挺清楚的:http://www.g ...
- Linux环境下源码安装PostgreSQL
1.下载PostgreSQL源码包,并保存到Linux操作系统的一个目录下 2.解压PostgreSQL源码包 :tar zxvf postgresql-9.2.4.tar.gz 或 tar jxvf ...
- logsource and ALO
1.首先配置sourcedb上的nfs服务,oggstd上挂载sourcedb的online redo和archive log的目录 oggsource上配置: vi /etc/export ...
- 隐私:随机选择 MAC 地址
隐私:随机选择 MAC 地址 从 Android 8.0 开始,Android 设备在未连接到网络的情况下探测新网络时会使用随机 MAC 地址. 在 Android 9 中,您可以启用开发者选项(默认 ...
- js循环匹配组合成新对象或js循环组合新数据
var Arry=[ {name: "vehicleTravelLicenseCopyBack", id: "a1"}, {name: "vehicl ...