算法: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!的更多相关文章

  1. UVa 11624 Fire!(着火了!)

    UVa 11624 - Fire!(着火了!) Time limit: 1.000 seconds Description - 题目描述 Joe works in a maze. Unfortunat ...

  2. UVA - 11624 Fire! bfs 地图与人一步一步先后搜/搜一次打表好了再搜一次

    UVA - 11624 题意:joe在一个迷宫里,迷宫的一些部分着火了,火势会向周围四个方向蔓延,joe可以向四个方向移动.火与人的速度都是1格/1秒,问j能否逃出迷宫,若能输出最小时间. 题解:先考 ...

  3. BFS(两点搜索) UVA 11624 Fire!

    题目传送门 /* BFS:首先对火搜索,求出火蔓延到某点的时间,再对J搜索,如果走到的地方火已经烧到了就不入队,直到走出边界. */ /******************************** ...

  4. UVA 11642 Fire!

    Fire! Time Limit: 1000ms Memory Limit: 131072KB This problem will be judged on UVA. Original ID: 116 ...

  5. UVA 11624 - Fire! 图BFS

    看题传送门 昨天晚上UVA上不去今天晚上才上得去,这是在维护么? 然后去看了JAVA,感觉还不错昂~ 晚上上去UVA后经常连接失败作死啊. 第一次做图的题~ 基本是照着抄的T T 不过搞懂了图的BFS ...

  6. E - Fire! UVA - 11624(bfs + 记录火到达某个位置所需要的最小时间)

    E - Fire! UVA - 11624 题目描述 乔在迷宫中工作.不幸的是,迷宫的一部分着火了,迷宫的主人没有制定火灾的逃跑计划.请帮助乔逃离迷宫.根据乔在迷宫中的位置以及迷宫的哪个方块着火,你必 ...

  7. UVA 11624 Fire!(广度优先搜索)

    题目大意:在一个N*M的迷宫内,J代表某人(只有一个),F代表火(可能不只一个),#代表墙,火每分钟会向四周除了墙以外的地方扩散一层,问人能否在没被火烧到 之前逃出迷宫,若能逃出输出最短时间.很明显的 ...

  8. UVa 11624 Fire!(BFS)

    Fire! Time Limit: 5000MS   Memory Limit: 262144KB   64bit IO Format: %lld & %llu Description Joe ...

  9. uva 11624 Fire!(搜索)

    开始刷题啦= = 痛并快乐着,学到新东西的感觉其实比看那些无脑的小说.电视剧有意思多了 bfs裸体,关键是先把所有的着火点放入队列,分开一个一个做bfs会超时的 发现vis[][]是多余的,完全可以用 ...

随机推荐

  1. GitHub 相关内容

    1. Git是分布式版本控制系统 集中式版本控制系统:版本库是集中存放在中央服务器的,而干活的时候,用的都是自己的电脑,所以要先从中央服务器取得最新的版本,然后开始干活,干完活了,再把自己的活推送给中 ...

  2. memcached全面剖析

    memcached介绍如今,越来越多的Web应用程序开始使用memcached这个高速的缓存服务器软件.然而,memcached的基础知识远远未能像其他Web技术那样普及,memcached在国内的大 ...

  3. Python 番外 消息队列设计精要

    消息队列已经逐渐成为企业IT系统内部通信的核心手段.它具有低耦合.可靠投递.广播.流量控制.最终一致性等一系列功能,成为异步RPC的主要手段之一.当今市面上有很多主流的消息中间件,如老牌的Active ...

  4. HTML&CSS基础学习笔记1.21-语义化标签

    语义化标签 “语义化”指的是机器在需要更少的人类干预的情况下能够研究和收集信息,让网页能够被机器理解,最终让人类受益. HTML 标签语义化是让大家直观的认识标签和属性的用途和作用,很明显<hx ...

  5. python 微信推送模板消息

    #!/usr/bin/env python #-*- coding: utf-8 -*- import httplib import json import MySQLdb #从数据库中获取acces ...

  6. Spring ioc 原理

    java程序员都知道:java程序中的每个业务逻辑至少需要两个或以上的对象来协作完成,通常,每个对象在使用他的合作对象时,自己均要使用像new object() 这样的语法来完成合作对象的申请工作.你 ...

  7. 在OCX初始化时获取其在网页中的DOM对象

    OCX初始化的时候会调用SetClientSite,会传入IOleClientSite对象. CComQIPtr<IOleControlSite, &IID_IOleControlSit ...

  8. Thinkphp显示系统常量信息的方法(php的用法)

    输入 :public function Main()    {        dump(get_defined_constants(true));    }显示系统信息, 其中: 'APP_PATH' ...

  9. JavaScript 数组中查找符合条件的值

    数组实例的find方法,用于找出第一个符合条件的数组成员.它的参数是一个回调函数,所有数组成员依次执行该回调函数,直到找出第一个返回值为true的成员,然后返回该成员.如果没有符合条件的成员,则返回u ...

  10. JAVA模拟表单提交

    这是我网上搜的,自己使用也蛮方便,所以上传供大家分享. package wzh.Http;   import java.io.BufferedReader; import java.io.IOExce ...