UVA11624 Fire! —— BFS
题目链接:https://vjudge.net/problem/UVA-11624
题解:
坑点:“portions of the maze havecaught on fire”, 表明了起火点不唯一。
火和人使用同一种结构体,用id来区别类型。BFS求解:首先将所有火苗入队,然后人再入队(肯定要火苗先入队,因为人要根据火当前烧到哪里来进行移动)。
对于一个尝试点:如果当前的id是人,且走出界,则逃生成功。如果没有走出界,则:
写法一(模拟过程): 如果没有走出界:如果id是火,且此地方是通道,则不管有没有vis过,都把它烧了,即把地图改为‘F’;如果id是人,且此地方是通道且没有被vis过,则进去。
写法二:其实火苗的本质作用是什么?就是禁止人vis那个地方,相当于会扩散的墙。有一点需要注意:对于一个通道,如果人比火先访问,那么其他相邻的通道,都是人先访问的(BFS的特点),所以火在扩散的时候,不用把周围也改为‘F’,直接把它标为vis,对于vis过的点,火和人都 不能再访问。那么我们就不用把火和人分开来处理了:对于一个合法的尝试点,如果此点是通道,则把它标为已经vis,之后入队即可。
写法一:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e3+; int n, m;
char g[MAXN][MAXN];
int vis[MAXN][MAXN], dir[][] = {,,,,-,,,-}; struct node
{
int x, y, id, step; //id为是人是火的标签
}; queue<node>que;
int bfs()
{
ms(vis, );
while(!que.empty()) que.pop(); node now, tmp;
for(int i = ; i<=n; i++) //先将所有的火一次性放进队列中
for(int j = ; j<=m; j++)
{
if(g[i][j]=='F')
{
now.x = i; now.y = j;
now.step = ; now.id = ;
vis[now.x][now.y] = ;
que.push(now);
}
} for(int i = ; i<=n; i++) //将人放进队列中
for(int j = ; j<=m; j++)
{
if(g[i][j]=='J')
{
now.x = i; now.y = j;
now.step = ; now.id = ;
vis[now.x][now.y] = ;
que.push(now);
}
} while(!que.empty())
{
now = que.front();
que.pop(); for(int i = ; i<; i++)
{
tmp.x = now.x + dir[i][];
tmp.y = now.y + dir[i][];
tmp.step = now.step + ;
tmp.id = now.id;
if(tmp.id== && (tmp.x< || tmp.x>n || tmp.y< || tmp.y>m) ) //如果是人,且走出界,则逃生成功
return tmp.step; if(tmp.x>= && tmp.x<=n && tmp.y>= && tmp.y<=m) //位置合法
{
//如果是火,并且可以燃烧,不过他有没有vis过,都把它给烧了
if(tmp.id== && g[tmp.x][tmp.y]=='.' || g[tmp.x][tmp.y]=='J' )
{
g[tmp.x][tmp.y] = 'F'; //这个地方变成火了
que.push(tmp);
}
//如果是人,并且这个地方是通道可没有vis过,则可行
if(tmp.id== && g[tmp.x][tmp.y]=='.' && !vis[tmp.x][tmp.y])
{
vis[tmp.x][tmp.y] = ;
que.push(tmp);
}
}
}
}
return -;
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d", &n, &m);
for(int i = ; i<=n; i++)
scanf("%s", g[i]+); int ans = bfs();
if(ans==-)
puts("IMPOSSIBLE");
else
printf("%d\n", ans);
}
}
写法二:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e3+; int n, m;
char g[MAXN][MAXN];
int vis[MAXN][MAXN], dir[][] = {,,,,-,,,-}; struct node
{
int x, y, id, step;
}; queue<node>que; int bfs()
{
ms(vis, );
while(!que.empty()) que.pop(); node now, tmp;
for(int i = ; i<=n; i++)
for(int j = ; j<=m; j++)
{
if(g[i][j]=='F')
{
now.x = i; now.y = j;
now.step = ; now.id = ;
vis[now.x][now.y] = ;
que.push(now);
}
} for(int i = ; i<=n; i++)
for(int j = ; j<=m; j++)
{
if(g[i][j]=='J')
{
now.x = i; now.y = j;
now.step = ; now.id = ;
vis[now.x][now.y] = ;
que.push(now);
}
} while(!que.empty())
{
now = que.front();
que.pop(); for(int i = ; i<; i++)
{
tmp.x = now.x + dir[i][];
tmp.y = now.y + dir[i][];
tmp.step = now.step + ;
tmp.id = now.id;
if(tmp.id== && (tmp.x< || tmp.x>n || tmp.y< || tmp.y>m) ) //逃生成功
return tmp.step; if(tmp.x>= && tmp.x<=n && tmp.y>= && tmp.y<=m
&& !vis[tmp.x][tmp.y] && g[tmp.x][tmp.y]=='.' ) //如果此地方是通道且没有被vis,则把它标为vis
{
vis[tmp.x][tmp.y] = ;
que.push(tmp);
}
}
}
return -;
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d", &n, &m);
for(int i = ; i<=n; i++)
scanf("%s", g[i]+); int ans = bfs();
if(ans==-)
puts("IMPOSSIBLE");
else
printf("%d\n", ans);
}
}
UVA11624 Fire! —— BFS的更多相关文章
- uva11624 - Fire!
uva11624 - Fire! 火在蔓延,人在走.火会蔓延,不会熄灭,我们可以确定某个点着火的时间(广搜).对于J来说,要是他走到某点的时间比火蔓延到该点的时间要短,那么他走到该点的时候,火还没蔓延 ...
- uva11624 Fire! (bfs预处理)
题目链接:https://vjudge.net/problem/UVA-11624 题意:给一个1000×1000的矩阵,有几个着火点和Joe,着火点和Joe每个单位时间均移动一个单位,求Joe逃出的 ...
- UVA11624(bfs)
题意:给一张图,有火源,有障碍物,剩下的是道路,火源在下一分钟能够让上下左右四个方向的道路也着火,告诉人的位置,问最短时间能逃出去的时间是多少: 思路:一个bfs用来求出所有的火源能蔓延到的地方,另一 ...
- UVA 11624 Fire! (bfs)
算法指南白书 分别求一次人和火到达各个点的最短时间 #include<cstdio> #include<cstring> #include<queue> #incl ...
- UVA - 11624 Fire! bfs 地图与人一步一步先后搜/搜一次打表好了再搜一次
UVA - 11624 题意:joe在一个迷宫里,迷宫的一些部分着火了,火势会向周围四个方向蔓延,joe可以向四个方向移动.火与人的速度都是1格/1秒,问j能否逃出迷宫,若能输出最小时间. 题解:先考 ...
- Fire!(BFS)
Fire! Time Limit:1000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Descr ...
- UVA 11624 Fire! BFS搜索
题意:就是问你能不能在火烧到你之前,走出一个矩形区域,如果有,求出最短的时间 分析:两遍BFS,然后比较边界 #include<cstdio> #include<algorithm& ...
- UVA 11624 Fire! bfs 难度:0
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- ACM: FZU 2150 Fire Game - DFS+BFS+枝剪 或者 纯BFS+枝剪
FZU 2150 Fire Game Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
随机推荐
- 【网摘】sql 语句修改字段名称以及字段类型
网上摘抄,备份使用: 修改字段名: 下例将表 customers 中的列 contact title 重命名为 title. EXEC sp_rename 'customers.[contact ti ...
- noj 2068 爱魔法的露露 [线性扫一遍]
njczy2010 2068 Accepted 325MS 8052K 1450Byte G++ 2014-11-13 11:20:40.0 爱魔法的露露 时间限制(普通/Java) : 1200 ...
- Scrapy学习-1-入门
基础知识 爬虫发展史 爬虫去重 1. 存储到数据库中 存取速度慢 2. 存储到内存中的集合里,内存占用十分大 当爬取数据有1亿条时 1*10**8*2Byte*50str_len/1024/102 ...
- govalidator----结构体tag验证
github地址:https://github.com/asaskevich/govalidator govalidator支持内置支持的验证tag和自定义验证tag: package main im ...
- 【Java TCP/IP Socket】Java NIO Socket VS 标准IO Socket
简介 Java NIO从JDK1.4引入,它提供了与标准IO完全不同的工作方式. NIO包(java.nio.*)引入了四个关键的抽象数据类型,它们共同解决传统的I/O类中的一些问题. 1. ...
- [转] java中volatile关键字的含义
在java线程并发处理中,有一个关键字volatile的使用目前存在很大的混淆,以为使用这个关键字,在进行多线程并发处理的时候就可以万事大吉. Java语言是支持多线程的,为了解决线程并发的问题,在语 ...
- 【Mybatis】 Mybatis在xml文件中处理大于号小于号的方法【问题】
处理大于小于号的方法: https://www.cnblogs.com/winner-0715/p/6132755.html 第一种方法:用转义字符把">"和"&l ...
- Shannon-Fano-Elias编码的C语言实现
Shannon-Fano-Elias编码 一.理论分析 Shannon-Fano-Elias编码是利用累积分布函数来分配码字. 不失一般性,假定取X={1,2,-m}.如果对于全部的x,有p(x)&g ...
- RNN与LSTM
Recurrent Neural Networks Recurrent neural networks are networks with loops in them, allowing inform ...
- BUPT复试专题—三元组(2016)
题目描述 给你一个长度为m的数组(数组元素从0到m-1),如果数组里有a[i]+a[j]==a[k](i,j,k大于等于0并且小于m),便称之为三元组.现在给你一个数组,让你求三元组的个数. 例如m为 ...