-->Fire!

直接上中文

Descriptions:

乔在迷宫中工作。不幸的是,迷宫的一部分着火了,迷宫的主人没有制定火灾的逃跑计划。请帮
助乔逃离迷宫。根据乔在迷宫中的位置以及迷宫的哪个方块着火,你必须确定火焰烧到他之前,
乔是否可以离开迷宫,如果能离开他能跑多快。
乔和火每分钟移动一个方格,上、下、左、右,四个方向中的一个。火势向四个方向同时蔓延。
乔可以从迷宫的任何一个边界逃离迷宫。无论是乔还是火都不会到达有墙的位置。

输入

第一行输入包含一个整数,即测试次数
每个测试用例的第一行包含两个
整数R和C,用空格分隔,1≤R,C≤1000
下面R行中,每一行都包含C个字符,以及每个字符是以下之一:
# 代表墙
. 代表空地,火和乔是可通行的
J 乔在迷宫中最初的位置,火和乔是可通行的
F 代表火
在每组测试中只有一个J

输出

对于每个测试用例,如果在火蔓延的时候烧到了乔,则乔无法逃出迷宫,输出'IMPOSSIBLE'
如果乔能逃出迷宫,则输出乔最快可以在几分钟内安全逃出迷宫,每组输出占一行

样例输入

2
4 4
####
#JF#
#..#
#..#
3 3
###
#J.
#.F

样例输出

3
IMPOSSIBLE

题目链接:

https://vjudge.net/problem/UVA-11624

注意火不止一个,火是固定的且火走过的地方时间也是一样的,所以可以先把火走的地方需要的时间处理一下,然后再对人bfs,多加一个条件,人在这个地方的时间要小于火在这个地方的时间

AC代码

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define MEM(x,y) memset(x,y,sizeof(x))
using namespace std;
int T,n,m;
char mp[][];//原始地图
int visfire[][];//记录火是否烧过
int vispeople[][];//记录人是否走过
int firetime[][];//火经过这个地方的时间
int peopletime[][];//人经过这个地方的时间
int dt[][]= {{,},{-,},{,},{,-}};//四个方向
struct node
{
int x,y;//坐标
};
node now,net;
queue<node>fire;//两个队列 火和人
queue<node>people;
void judgetime()//预处理火经过的地方的时间
{
while(!fire.empty())
{
now=fire.front();
fire.pop();
for(int i=; i<; i++)//四种走法
{
int tx=now.x+dt[i][];
int ty=now.y+dt[i][];
if(tx>=&&tx<n&&ty>=&&ty<m&&!visfire[tx][ty]&&mp[tx][ty]!='#')
{
net.x=tx;
net.y=ty;
visfire[tx][ty]=;//标记已烧过
fire.push(net);//入队
firetime[tx][ty]=firetime[now.x][now.y]+;//时间加1
}
}
}
}
void bfs()//人
{
int f=;
while(!people.empty())
{
now=people.front();
people.pop();
if(now.x<=||now.x>=n-||now.y<=||now.y>=m-)//人走出来了
{
f=;
cout<<peopletime[now.x][now.y]+<<endl;
break;
}
for(int i=; i<; i++)//四种走法
{
net.x=now.x+dt[i][];
net.y=now.y+dt[i][];
if(net.x>=&&net.x<n&&net.y>=&&net.y<m&&!vispeople[net.x][net.y]&&mp[net.x][net.y]=='.'&&peopletime[now.x][now.y]+<firetime[net.x][net.y])
{
vispeople[net.x][net.y]=;//标记已走过
people.push(net);//入队
peopletime[net.x][net.y]=peopletime[now.x][now.y]+;//时间+1
}
}
}
if(f==)
cout<<"IMPOSSIBLE"<<endl;
}
int main()
{
cin>>T;
while(T--)
{
MEM(firetime,INF);//初始化
MEM(peopletime,INF);
MEM(visfire,);
MEM(vispeople,);
cin>>n>>m;
for(int i=; i<n; i++)
{
for(int j=; j<m; j++)
{
cin>>mp[i][j];
if(mp[i][j]=='J')
{
now.x=i;
now.y=j;
vispeople[i][j]=;
people.push(now);
peopletime[i][j]=;
}
if(mp[i][j]=='F')
{
now.x=i;
now.y=j;
visfire[i][j]=;
fire.push(now);
firetime[i][j]=;
}
}
}
judgetime();
// for(int i=0; i<n; i++)
// {
// for(int j=0; j<m; j++)
// {
// cout<<firetime[i][j]<<" ";
// }
// cout<<endl;
// }
bfs();
while(!fire.empty())//清空队列
{
fire.pop();
}
while(!people.empty())
{
people.pop();
}
}
}

【UVA - 11624】Fire!的更多相关文章

  1. 【巧妙算法系列】【Uva 11464】 - Even Parity 偶数矩阵

    偶数矩阵(Even Parity, UVa 11464) 给你一个n×n的01矩阵(每个元素非0即1),你的任务是把尽量少的0变成1,使得每个元素的上.下.左.右的元素(如果存在的话)之和均为偶数.比 ...

  2. 【贪心+中位数】【UVa 11300】 分金币

    (解方程建模+中位数求最短累积位移) 分金币(Spreading the Wealth, UVa 11300) 圆桌旁坐着n个人,每人有一定数量的金币,金币总数能被n整除.每个人可以给他左右相邻的人一 ...

  3. 【UVa 10881】Piotr's Ants

    Piotr's Ants Porsition:Uva 10881 白书P9 中文改编题:[T^T][FJUT]第二届新生赛真S题地震了 "One thing is for certain: ...

  4. 【UVa 116】Unidirectional TSP

    [Link]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  5. 【UVa 1347】Tour

    [Link]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  6. 【UVA 437】The Tower of Babylon(记忆化搜索写法)

    [题目链接]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  7. 【uva 1025】A Spy in the Metro

    [题目链接]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  8. 【POJ 2152】 Fire

    [题目链接] 点击打开链接 [算法] 同样是树形DP,但是比较难,笔者做这题看了题解 令f[i][j]表示在以i为根的子树中 1.在以i为根的子树中建一些消防站 2.在节点j必须建一个消防站 3.以i ...

  9. 【Uva 11584】Partitioning by Palindromes

    [Link]:https://cn.vjudge.net/contest/170078#problem/G [Description] 给你若干个只由小写字母组成的字符串; 问你,这个字符串,最少能由 ...

随机推荐

  1. js -- 捆绑

    1.环境配置 主要參考网址: http://cocos2d.cocoachina.com/bbs/forum.php?mod=viewthread&tid=10226&extra=pa ...

  2. ATS项目更新(3) 远程同步到执行机器

    1: echo %time% 2: 3: 4: rem ** ipc and mapping 5: c: 6: net use x: /del 7: net use y: /del 8: net us ...

  3. 构建自己的PHP框架(视图装载)

    完整项目地址:https://github.com/Evai/Aier 视图装载类要做的工作其实很简单: 1. 根据视图名称找到视图文件,支持文件夹 2. 更加方便,更加优雅地把变量的值传递进视图 本 ...

  4. linux_crontab_定时删除

    #每天2:30 删除4天前qac的原始log30 2 * * * find /home/iknow/ETL/RetlPull/retl-pull/data/qac -name qac.log.new. ...

  5. Emgu-WPF学习使用-中值模糊

    原文:Emgu-WPF学习使用-中值模糊 实现效果: 实现途径: 前提:Image File-> System.Drawing.Bitmap->Image<Bgr, byte> ...

  6. WPF GridSplitter最好设置HorizontalAlignment和VerticalAlignment,否则不可以左右移动

    <Window x:Class="XamlTest.Window5"        xmlns="http://schemas.microsoft.com/winf ...

  7. install windows service

    install windows serivce e.g @echo offecho ---------------------------------------------------------- ...

  8. iOS UIScrollView使用Autolayout

    最近项目在迭代更新的时候,需要在之前用Autolayout写的界面里添加一个button,添加完这个button后,iPhone5,iPhone4显示不全了.遇到整个问题后很自然就想到了用UIScro ...

  9. 海康SDK编程指南

    转至心澄欲遣 目前使用的海康SDK包括IPC_SDK(硬件设备),Plat_SDK(平台),其中两套SDK都需单独调用海康播放库PlayCtrl.dll来解码视频流,返回视频信息和角度信息.本文仅对视 ...

  10. csdn token

    http://download.csdn.net/download/pp_haitun/9614126 http://dl.download.csdn.net/down11/20160826/28b9 ...