UVA - 11624 Fire! 【BFS】
题意
有一个人 有一些火
人 在每一秒 可以向 上下左右的空地走 火每秒 也会向 上下左右的空地 蔓延
求 人能不能跑出来 如果能 求最小时间
思路
有一个 坑点 火是 可能有 多处 的 样例中 只有一处
然后 先让 火 蔓延 再让人走
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】的更多相关文章
- uva 11624 Fire! 【 BFS 】
按白书上说的,先用一次bfs,求出每个点起火的时间 再bfs一次求出是否能够走出迷宫 #include<cstdio> #include<cstring> #include&l ...
- UVA 11624 - Fire! 图BFS
看题传送门 昨天晚上UVA上不去今天晚上才上得去,这是在维护么? 然后去看了JAVA,感觉还不错昂~ 晚上上去UVA后经常连接失败作死啊. 第一次做图的题~ 基本是照着抄的T T 不过搞懂了图的BFS ...
- UVa 11624 Fire!(BFS)
Fire! Time Limit: 5000MS Memory Limit: 262144KB 64bit IO Format: %lld & %llu Description Joe ...
- (简单) UVA 11624 Fire! ,BFS。
Description Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the ow ...
- UVA - 11624 Fire! 双向BFS追击问题
Fire! Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of ...
- UVA_11624 Fire! 【BFS】
一.题面 略 二.题意分析 一个迷宫中,有一个人Joe和一个或多个起火点,起火点可以蔓延,人可以走动,都只能走4个方向,问人能走出去的最少步数,如果不能输出不可能.很多大佬说是两遍BFS,先一遍火,记 ...
- BFS(两点搜索) UVA 11624 Fire!
题目传送门 /* BFS:首先对火搜索,求出火蔓延到某点的时间,再对J搜索,如果走到的地方火已经烧到了就不入队,直到走出边界. */ /******************************** ...
- UVa 11624 Fire!(着火了!)
UVa 11624 - Fire!(着火了!) Time limit: 1.000 seconds Description - 题目描述 Joe works in a maze. Unfortunat ...
- UVA 11624 Fire!【两点BFS】
Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the m ...
随机推荐
- mac搭建安卓开发环境
下载 android studio,利用android studio自带sdk manager下载安卓sdk,在sdk manager中设置相关代理地址,下载完sdk后,在bash_profile中把 ...
- HPU 3639--Hawk-and-Chicken【SCC缩点反向建图 && 求传递的最大值】
Hawk-and-Chicken Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- OpenCV机器学习库函数--SVM
svm分类算法在opencv3中有了很大的变动,取消了CvSVMParams这个类,因此在参数设定上会有些改变. opencv中的svm分类代码,来源于libsvm. #include "o ...
- openTK学习
简介 the Open Tool Kit (OpenTK), 是对 OpenGL.OpenAL.OpenCL 的跨平台的封装,使用 C# 编写,它可以用在Mono.dotNet的语言:c#.VB.C+ ...
- TensorFlow笔记六:基于cifar10数据库的AlexNet识别
准确率只有70%,cpu版本的TF居然跑了两天才跑完,其他方法将继续尝试. 生成数据目录: import numpy as np import os train_label = {} for i in ...
- JavDroider的作品展示
好久没有写博客了,很懊悔,尽管说实习和项目那边的任务有点多,可是我想每天抽出时间出来写一篇文章总结一下当天所习所得并不困难! 好了,今天以一篇个人作品介绍来又一次开启我的博客~ 实习单位的门户站点 一 ...
- linux 之体验(JDK7+Tomcat7+MySQL5.5)部署环境
---------------------------------------------------------------------------------------------------- ...
- 利用 LeakCanary 来检查 Android 内存泄漏
前言 你被概率性的 OOM 困扰么?有时候,OOM 像幽灵一样,挥之不去,可真想把它揪出来时,又捉之不着.或许,是时候用 LeakCanary 来诊断一下了.它是一个用来检查 Android 下内存泄 ...
- jquery方法
$.inArray(被判断的量,ArrayName); 如果存在返回索引值,如果不存在返回-1 $.unique() 数组去重 根据去重前后的长度,判断是否有重复 $.each(被遍历的数组,f ...
- eeplat开发平台概念理解
近期在学习eeplat的开发.发现其中有非常多概念实在让人easy忘记,所以谨以此文记录一笔. eeplat的开发文档里说eeplat是元数据驱动的,这个元数据什么意思.在我理解就是后台的数据库里面的 ...