题意:就是问你能不能在火烧到你之前,走出一个矩形区域,如果有,求出最短的时间

分析:两遍BFS,然后比较边界

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cmath>
#include<map>
#include<queue>
#include<stdlib.h>
#include<string>
#include<set>
using namespace std;
typedef long long LL;
const int maxn=;
const int INF=0x3f3f3f3f;
char s[maxn][maxn];
int mp[maxn][maxn];
int v[maxn][maxn];
int n,m;
struct Point
{
int x,y;
Point() {}
Point(int a,int b)
{
x=a,y=b;
}
} o,t;
queue<Point>q,e;
int dx[]= {,,-,};
int dy[]= {-,,,};
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=; i<=n; ++i)
scanf("%s",s[i]+);
for(int i=; i<=n; ++i)
{
for(int j=; j<=m; ++j)
{
mp[i][j]=v[i][j]=-;
if(s[i][j]=='F')q.push(Point(i,j)),mp[i][j]=;
else if(s[i][j]=='J')e.push(Point(i,j)),v[i][j]=;
}
}
while(!q.empty())
{
o=q.front();
q.pop();
for(int i=; i<; ++i)
{
t.x=o.x+dx[i];
t.y=o.y+dy[i];
if(t.x<||t.x>n||t.y<||t.y>m)continue;
if(s[t.x][t.y]=='#'||mp[t.x][t.y]!=-)continue;
mp[t.x][t.y]=mp[o.x][o.y]+;
q.push(t);
}
}
while(!e.empty())
{
o=e.front();
e.pop();
for(int i=; i<; ++i)
{
t.x=o.x+dx[i];
t.y=o.y+dy[i];
if(t.x<||t.x>n||t.y<||t.y>m)continue;
if(s[t.x][t.y]=='#'||v[t.x][t.y]!=-)continue;
v[t.x][t.y]=v[o.x][o.y]+;
e.push(t);
}
}
int ans=INF;
for(int i=;i<=n;++i)
{
if(v[i][]!=-)
{
if(mp[i][]==-||v[i][]<mp[i][])
ans=min(ans,v[i][]);
}
if(v[i][m]!=-)
{
if(mp[i][m]==-||v[i][m]<mp[i][m])
ans=min(ans,v[i][m]);
}
}
for(int i=;i<=m;++i)
{
if(v[][i]!=-)
{
if(mp[][i]==-||v[][i]<mp[][i])
ans=min(ans,v[][i]);
}
if(v[n][i]!=-)
{
if(mp[n][i]==-||v[n][i]<mp[n][i])
ans=min(ans,v[n][i]);
}
}
if(ans==INF)printf("IMPOSSIBLE\n");
else printf("%d\n",ans+);
}
return ;
}

UVA 11624 Fire! BFS搜索的更多相关文章

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

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

  2. uva 11624 Fire!(搜索)

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

  3. UVA 11624 Fire! (bfs)

    算法指南白书 分别求一次人和火到达各个点的最短时间 #include<cstdio> #include<cstring> #include<queue> #incl ...

  4. UVA 11624 Fire! bfs 难度:0

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

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

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

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

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

  7. UVA 11624 Fire!【两点BFS】

    Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the m ...

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

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

  9. UVA 11624 - Fire! 图BFS

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

随机推荐

  1. Elasticsearch从0.90(0.90.x)到1.2(1.x)API的变化-二

    本文为官方文档的译文加个人理解.作者翻译时,elasticsearch(下面简称es)的版本为1.2.2. 请支持原创:http://www.cnblogs.com/donlianli/p/38367 ...

  2. Elasticsearch从0.90到1.2的不兼容变化-系统和设置

      本文为官方文档的翻译加个人理解.作者翻译时,elasticsearch(下面简称es)的版本为1.2.1.   1.系统级别及设置方面 1.1 es启动时,默认是作为一个前台程序启动.如果你想让e ...

  3. REST Design Concerns

    Less Requests, More data; one of the core RESTful API design paradigms is the concept of less API re ...

  4. 斯坦福数据挖掘Introduction

    感谢敖山.薛霄老师把我引进了统计学和现代服务业的大门.......至少是长见识了. 查相似项检索时发现的. 中间一部分资料来自厦门大学数据库实验室,感谢大牛们的传道授业,爱你们. 查资料时发现很多计算 ...

  5. where, group by, having

    where vs having 当一个sql语句中存在where子句,会先执行where,然后执行group by,然后执行having. 一般来说,only use 'having' when yo ...

  6. 安装ADT Cannot complete the install because one or more required items could not be found.

    点击进行安装,将会弹出 错误提示是: Cannot complete the install because one or more required items could not be found ...

  7. Android 如何切换到 Transform API?

    摘要: 如果你的 Android 构建中涉及到字节码插装(bytecode instrumentation),或者应用中提供了进行插装的插件,并希望它能支持 Instant Run,那么你必须切换到 ...

  8. linux的定时任务crontab

    每隔一分钟执行以下语句: #打印当前时间: date "+%Y-%m-%d %T" 保存为/usr/test/test.sh 查看系统中当前用户有多少个定时任务: crontab ...

  9. QT5.7交叉编译安装到arm(好多系列文章)

    以下采用的系统为ubuntu16.04,开发板为迅为iTOP4412,4.3寸屏. 下载qt5.7源码qt-everywhere-opensource-src-5.7.0.tar.xz http:// ...

  10. RedMine项目管理系统安装问题(Windows版一键安装包)

    安装准备: 操作环境:VMware10 下安装的windows10 系统 使用软件:<bitnami-redmine---windows-installer.exe> 问题描述: 安装过程 ...