题意

有一个人 有一些火

人 在每一秒 可以向 上下左右的空地走 火每秒 也会向 上下左右的空地 蔓延

求 人能不能跑出来 如果能 求最小时间

思路

有一个 坑点 火是 可能有 多处 的 样例中 只有一处

然后 先让 火 蔓延 再让人走

BFS

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits> #define CLR(a) memset(a, 0, sizeof(a))
#define pb push_back using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss; const double PI = acos(-1);
const double E = exp(1);
const double eps = 1e-30; const int INF = 0x3f3f3f3f;
const int maxn = 1e3 + 5;
const int MOD = 1e9 + 7; string G[maxn]; int Move[8][2]
{
-1, 0,
1, 0,
0,-1,
0, 1,
-1, 1,
-1,-1,
1, 1,
1,-1,
}; int n, m;
int ans; bool ok(int x, int y)
{
if (x < 0 || x >= n || y < 0 || y >= m || G[x][y] != '.' )
return false;
return true;
} bool edge(int x, int y)
{
if (x == 0 || x == n - 1 || y == 0 || y == m - 1)
return true;
return false;
} struct Node
{
int x, y, step;
}tmp; queue <Node> fire, q; void bfs()
{
int len = fire.size();
for (int i = 0; i < len; i++)
{
int x = fire.front().x;
int y = fire.front().y;
fire.pop();
for (int j = 0; j < 4; j++)
{
tmp.x = x + Move[j][0];
tmp.y = y + Move[j][1];
if (ok(tmp.x, tmp.y))
{
G[tmp.x][tmp.y] = 'F';
fire.push(tmp);
}
}
}
len = q.size();
for (int i = 0; i < len; i++)
{
int x = q.front().x;
int y = q.front().y;
int step = q.front().step;
q.pop();
if (edge(x, y))
{
ans = step;
return;
}
for (int j = 0; j < 4; j++)
{
tmp.x = x + Move[j][0];
tmp.y = y + Move[j][1];
if (ok(tmp.x, tmp.y))
{
tmp.step = step + 1;
G[tmp.x][tmp.y] = '*';
q.push(tmp);
}
}
}
if (q.size())
bfs();
} int main()
{
int t;
cin >> t;
while (t--)
{
while (!fire.empty())
fire.pop();
while (!q.empty())
q.pop();
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++)
{
cin >> G[i];
for (int j = 0; j < m; j++)
{
if (G[i][j] == 'J')
{
tmp.x = i;
tmp.y = j;
tmp.step = 0;
q.push(tmp);
G[i][j] = '.';
}
else if (G[i][j] == 'F')
{
tmp.x = i;
tmp.y = j;
fire.push(tmp);
}
}
}
ans = -1;
bfs();
if (ans == -1)
printf("IMPOSSIBLE\n");
else
printf("%d\n", ans + 1);
}
}

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

  1. uva 11624 Fire! 【 BFS 】

    按白书上说的,先用一次bfs,求出每个点起火的时间 再bfs一次求出是否能够走出迷宫 #include<cstdio> #include<cstring> #include&l ...

  2. UVA 11624 - Fire! 图BFS

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

  3. UVa 11624 Fire!(BFS)

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

  4. (简单) UVA 11624 Fire! ,BFS。

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

  5. UVA - 11624 Fire! 双向BFS追击问题

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

  6. UVA_11624 Fire! 【BFS】

    一.题面 略 二.题意分析 一个迷宫中,有一个人Joe和一个或多个起火点,起火点可以蔓延,人可以走动,都只能走4个方向,问人能走出去的最少步数,如果不能输出不可能.很多大佬说是两遍BFS,先一遍火,记 ...

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

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

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

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

  9. UVA 11624 Fire!【两点BFS】

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

随机推荐

  1. Python中xml、字典、json、类四种数据的转换

    最近学python,觉得python很强很大很强大,写一个学习随笔,当作留念注:xml.字典.json.类四种数据的转换,从左到右依次转换,即xml要转换为类时,先将xml转换为字典,再将字典转换为j ...

  2. EasyMvc入门教程-高级控件说明(15)方位布局控件

    现在很多管理后台都流行全屏切割的布局,大体结构如下图所示: 大家注意到没,整个布局是五个部分组成:“东西南北中”,EasyMvc对应的实现的代码为: @(Html.Q().Layout().TextC ...

  3. python traceback学习(转)

    1. Python中的异常栈跟踪 之前在做Java的时候,异常对象默认就包含stacktrace相关的信息,通过异常对象的相关方法printStackTrace()和getStackTrace()等方 ...

  4. Win7如何自定义鼠标右键菜单 添加新建文本文档

    鼠标右键新建文本文档.reg REGEDIT4 [HKEY_CLASSES_ROOT\.txt] @="txtfile" "Content Type"=&quo ...

  5. Android 基于Retrofit+Rxjava搭建的简单易用的网络架构

    装逼開始之前,为大家推荐两篇文章,用来更好的学习Retrofit和Rxjava. 在这里我们要感谢互联网装逼行业勇于献身,甘于奉献的的大婶们. 我仅代表个人,给您们跪舔了. Retrofit:Retr ...

  6. vue 监听 watch 使用

    1.api https://cn.vuejs.org/v2/api/#watch 有2个配置: (1)深度 watcher deep: true(2)该回调将会在侦听开始之后被立即调用 immedia ...

  7. Ant Design 3.0 使用案例

    代码地址如下:http://www.demodashi.com/demo/12309.html 本文适合对象 有过React使用经验. 有过webpack使用经验. 了解node. DEMO使用方式 ...

  8. android中使用百度定位sdk实时的计算移动距离

    ;   //5秒刷新一次 private Handler refreshHandler = new Handler(){ //刷新界面的Handler public void handleMessag ...

  9. 【C语言天天练(二)】statickeyword

    引言:                 statickeyword不仅能够修饰变量.并且能够修饰函数.了解它的使用方法,不仅对阅读别人的代码有帮助,也有助于自己写出更加健壮的程序. 使用方法:     ...

  10. Jest — ElasticSearch Java 客户端

    1. 介绍 任何使用过Elasticsearch的人都知道,使用基于rest的搜索API构建查询可能是单调乏味且容易出错的. 在本教程中,我们将研究Jest,一个用于Elasticsearch的HTT ...