[宽度优先搜索] FZU-2150 Fire Game
Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns). At the beginning, each grid of this board is consisting of grass or just empty and then they start to fire all the grass. Firstly they choose two grids which are consisting of grass and set fire. As we all know, the fire can spread among the grass. If the grid (x, y) is firing at time t, the grid which is adjacent to this grid will fire at time t+1 which refers to the grid (x+1, y), (x-1, y), (x, y+1), (x, y-1). This process ends when no new grid get fire. If then all the grid which are consisting of grass is get fired, Fat brother and Maze will stand in the middle of the grid and playing a MORE special (hentai) game. (Maybe it’s the OOXX game which decrypted in the last problem, who knows.)
You can assume that the grass in the board would never burn out and the empty grid would never get fire.
Note that the two grids they choose can be the same.
Input
The first line of the date is an integer T, which is the number of the text cases.
Then T cases follow, each case contains two integers N and M indicate the size of the board. Then goes N line, each line with M character shows the board. “#” Indicates the grass. You can assume that there is at least one grid which is consisting of grass in the board.
1 <= T <=100, 1 <= n <=10, 1 <= m <=10
Output
For each case, output the case number first, if they can play the MORE special (hentai) game (fire all the grass), output the minimal time they need to wait after they set fire, otherwise just output -1. See the sample input and output for more details.
Sample Input
4
3 3
.#.
###
.#.
3 3
.#.
#.#
.#.
3 3
...
#.#
...
3 3
###
..#
#.#
Sample Output
Case 1: 1
Case 2: -1
Case 3: 0
Case 4: 2 题目描述:两个无所畏惧的小孩在草地上同时防火,问最短把草烧光用的时间。
解题思路:宽度优先搜索一下,在队列里加个时间就可以了。
#include<stdio.h>
#include<algorithm>
#include<queue>
#define inf 0x7fffffff
using namespace std;
class node
{
public:
int x,y,time;
}hpair;
queue<node> q;
int n,m,glnum;
char map[15][15];
const int dx[2][4]={{0,0,1,-1},{-1,1,0,0}};
class search
{
private:
int i,j,step,x,y,visit[15][15],xx,yy,time;
public:
int bfs(int x1,int y1,int x2,int y2)
{
for(i=0;i<11;++i)
for(j=0;j<11;++j)
visit[i][j]=0;
q.push((class node){x1,y1,0});
q.push((class node){x2,y2,0});
visit[x1][y1]=visit[x2][y2]=1;
step=0;
while(!q.empty())
{
x=q.front().x;y=q.front().y;time=q.front().time;q.pop();
if(time>step) step=time;
for(i=0;i<4;++i)
{
xx=x+dx[0][i];yy=y+dx[1][i];
if(xx>=0&&yy>=0&&xx<n&&yy<m&&map[xx][yy]=='#'&&!visit[xx][yy])
{
q.push((class node){xx,yy,time+1});
--glnum;
visit[xx][yy]=1;
}
}
}
return step;
}
}Search;
int main()
{
int T,i,j,k,u,ans,cnt,num,t;
scanf("%d",&T);
for(cnt=1;cnt<=T;++cnt)
{
scanf("%d%d",&n,&m);
ans=inf;num=0;
for(i=0;i<n;++i)
{
scanf("%s",&map[i]);
for(j=0;j<m;++j)
if(map[i][j]=='#')
++num;
}
for(i=0;i<n;++i)
for(j=0;j<m;++j)
for(k=0;k<n;++k)
for(u=0;u<m;++u)
if(map[i][j]=='#'&&map[k][u]=='#')
{
glnum=num;
if(i==k&&j==u) --glnum; else glnum-=2;
t=Search.bfs(i,j,k,u);
if(glnum==0) ans=min(t,ans);
}
if(ans==inf) printf("Case %d: %d\n",cnt,-1); else printf("Case %d: %d\n",cnt,ans);
}
return 0;
}
[宽度优先搜索] FZU-2150 Fire Game的更多相关文章
- 挑战程序2.1.5 穷竭搜索>>宽度优先搜索
先对比一下DFS和BFS 深度优先搜索DFS 宽度优先搜索BFS 明显可以看出搜索顺序不同. DFS是搜索单条路径到 ...
- 【算法入门】广度/宽度优先搜索(BFS)
广度/宽度优先搜索(BFS) [算法入门] 1.前言 广度优先搜索(也称宽度优先搜索,缩写BFS,以下采用广度来描述)是连通图的一种遍历策略.因为它的思想是从一个顶点V0开始,辐射状地优先遍历其周围较 ...
- 【BFS宽度优先搜索】
一.求所有顶点到s顶点的最小步数 //BFS宽度优先搜索 #include<iostream> using namespace std; #include<queue> # ...
- FZU 2150 Fire Game(点火游戏)
FZU 2150 Fire Game(点火游戏) Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description - 题目描述 ...
- 层层递进——宽度优先搜索(BFS)
问题引入 我们接着上次“解救小哈”的问题继续探索,不过这次是用宽度优先搜索(BFS). 注:问题来源可以点击这里 http://www.cnblogs.com/OctoptusLian/p/74296 ...
- BFS算法的优化 双向宽度优先搜索
双向宽度优先搜索 (Bidirectional BFS) 算法适用于如下的场景: 无向图 所有边的长度都为 1 或者长度都一样 同时给出了起点和终点 以上 3 个条件都满足的时候,可以使用双向宽度优先 ...
- 宽度优先搜索--------迷宫的最短路径问题(dfs)
宽度优先搜索运用了队列(queue)在unility头文件中 源代码 #include<iostream>#include<cstdio>#include<queue&g ...
- 算法基础⑦搜索与图论--BFS(宽度优先搜索)
宽度优先搜索(BFS) #include<cstdio> #include<cstring> #include<iostream> #include<algo ...
- 搜索与图论②--宽度优先搜索(BFS)
宽度优先搜索 例题一(献给阿尔吉侬的花束) 阿尔吉侬是一只聪明又慵懒的小白鼠,它最擅长的就是走各种各样的迷宫. 今天它要挑战一个非常大的迷宫,研究员们为了鼓励阿尔吉侬尽快到达终点,就在终点放了一块阿尔 ...
随机推荐
- golang获取文件的md5
直接展示代码 func md5sum(filepath string) (string, error) { f, err := os.Open(filepath) if err != nil { st ...
- 有些人表面风光,背地里for循环怎么写都要百度
代码地址 https://github.com/ljshLLW/homework 题目 最大连续子数组和(最大子段和) 给定n个整数(可能为负数)组成的序列a[1],a[2],a[3],-,a[n], ...
- linux普通帐号可以临时切换到root(添加用户到sudoers中)
一般,进入terminal之后,默认是普通账户能操作的功能,能访问的目录有限,需要临时切换到root账户 那么此时就需要配置sudoers文件,可以让普通用户通过sudo命令临时切换到root账户 首 ...
- NABCD原则
1.我们的产品 <随堂小测APP> 是为了解决 <老师们> 的痛苦, 2-N.他们需要 随时组织课堂测验, 但是现有的方案并没有很好地解决这些需求,3-A.我们有独特的办法 ...
- Flask Web框架
Flask依赖两个外部库:Werkzeug和Jinja2.Werkzeug是一个WSGI(在Web应用和多种服务器之间的标准Python接口)工具集:Jinja2负责渲染模板.所以在安装Flask之前 ...
- 关于bootstrap框架美化的实例教程(python)
经过上一章的内容,其实就页面层来说已结可以很轻松的实现功能了,但是很明显美观上还有很大的欠缺,现在有一些很好的前端css框架,如AmazeUI,腾讯的WeUI等等,这里推荐一个和flask集成很好的b ...
- C语言--第4次作业
1.本章学习总结 1.1思维导图 1.2本章学习体会及代码量学习体会 1.2.1学习体会 初识数组:这几周第一次接触数组,感觉有点懵,是一个很陌生的知识点,但是运用范围及其广泛,大大简化了程序,增大了 ...
- css复合选择器的权重
选择器的权重 标签选择器的权重为0001 class选择器的权重为0010 id选择器的权重为0100 属性选择器的权重为0010 伪类选择器的权重为0010 伪元素选择器的权重为0010 包含选择器 ...
- 从Scratch到Python之角色与造型
从Scratch到Python之角色与造型 继续讲解通过python turtle从积木编程过渡到代码编程的技巧.角色是scratch中很重要的主角,每个角色可以更换不同的造型或者音效,堆叠不同的积木 ...
- P2678 跳石头
传送门 思路: 二分跳跃的最短距离 mid .暴力判断如果有两个石头直接的距离小于 mid ,就把这个石头拿走.如果拿走的石头数目 cnt ≤ m,说明二分的答案可行,ans = mid,接着二分更短 ...