(FZU 2150) Fire Game (bfs)
题目链接:http://acm.fzu.edu.cn/problem.php?pid=2150
Problem Description
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
Sample Output
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std;
#define INF 0x3f3f3f3f
#define ll long long
#define met(a,b) memset(a,b,sizeof(a))
#define N 109
int vis[N][N];
char str[N][N];
int dir[][]={{,},{-,},{,},{,-}};
int k,n,m;
struct node
{
int x,y,temp;
}a[N];///统计干草的坐标
int pan()///判断干草是否都能被点燃
{
int x,y;
for(int i=;i<k;i++)
{
x=a[i].x;y=a[i].y;
if(!vis[x][y])
return ;
}
return ;
}
int bfs(node a,node b)
{
int team=;
met(vis,);
queue<node>Q;
node q,p;
Q.push(a);Q.push(b);
vis[a.x][a.y]=;vis[b.x][b.y]=;
while(Q.size())
{
q=Q.front();
Q.pop();
for(int i=;i<;i++)
{
p.x=q.x+dir[i][];
p.y=q.y+dir[i][];
p.temp=q.temp+;
if(p.x>= && p.x<n&& p.y>= && p.y<m && str[p.x][p.y]=='#' && !vis[p.x][p.y])
{
vis[p.x][p.y]=;
team=max(team,p.temp);
Q.push(p);
}
}
}
if(pan())
return team;
else
return INF;
}
int main()
{
int t,con=;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&m);
for(int i=;i<n;i++)
scanf("%s",str[i]);
k=;
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(str[i][j]=='#')
{
a[k].x=i;
a[k].y=j;
a[k++].temp=;
}
}
}
int ans=INF;
for(int i=;i<k;i++)
{
for(int j=i;j<k;j++)
{
ans=min(bfs(a[i],a[j]),ans);
}
}
printf("Case %d: ",con++);
if(ans==INF)
printf("-1\n");
else
printf("%d\n",ans);
}
return ;
}
(FZU 2150) Fire Game (bfs)的更多相关文章
- FZU 2150 Fire Game (bfs+dfs)
Problem Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M board ...
- FZU - 2150 Fire Game bfs+双起点枚举
题意,10*10的地图,有若干块草地“#”,草地可以点燃,并在一秒后点燃相邻的草地.有墙壁‘·‘阻挡.初始可以从任意两点点火.问烧完最短的时间.若烧不完输出-1. 题解:由于100的数据量,直接暴力. ...
- FZU 2150 Fire Game(点火游戏)
FZU 2150 Fire Game(点火游戏) Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description - 题目描述 ...
- fzu 2150 Fire Game 【身手BFS】
称号:fzupid=2150"> 2150 Fire Game :给出一个m*n的图,'#'表示草坪,' . '表示空地,然后能够选择在随意的两个草坪格子点火.火每 1 s会向周围四个 ...
- FZU 2150 fire game (bfs)
Problem 2150 Fire Game Accept: 2133 Submit: 7494Time Limit: 1000 mSec Memory Limit : 32768 KB ...
- FZU 2150 Fire Game (暴力BFS)
[题目链接]click here~~ [题目大意]: 两个熊孩子要把一个正方形上的草都给烧掉,他俩同一时候放火烧.烧第一块的时候是不花时间的.每一块着火的都能够在下一秒烧向上下左右四块#代表草地,.代 ...
- FZU 2150 Fire Game
Fire Game Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit St ...
- FZU 2150 Fire Game 【两点BFS】
Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns) ...
- Fire Game (FZU 2150)(BFS)
题解:一开始想错了,以为只要烧完就是那个答案,但是这不是最优的结果,需要每两个点都bfs一遍,找到如果能够全部烧完,找到花费时间最小的,如果不能return -1.在bfs的时候,记录答案的方法参考了 ...
随机推荐
- postgresql自定义类型并返回数组
转自 https://blog.csdn.net/victor_ww/article/details/44415895 create type custom_data_type as ( id int ...
- SQL Server 提供的各种数据访问接口
在创建SQL Server的链接服务器时,可以看到有如下几种访问接口,其中我们常用的只有1.3.4.6.其中4是安装Oracle客户端才会出现的接口,3.6是由于我电脑上装了2008R2和2012两个 ...
- c/c++ 标准库 pair 介绍
标准库 pair 介绍 问题:map里的元素由key和value组成,这个key和value的组合是什么类型呢??? 答案:pair类型 pair介绍: 它是模板 有2个公有成员可供访问. first ...
- 数据挖掘---Pandas的学习
Pandas介绍(panel + data + analysis) 为什么使用Pandas 便捷的数据处理能力 读取文件方便 封装了Matplotlib.Nu ...
- 华为无线AP4030,FIA--FAT模式更改
因为买回来的时候才注意到是APfit模式的,只是想作为一个无线路由点接入网络的,只有更改模式,在网上找了很多,实验了两天也还是成功了. 1.准备工具:网线.console线一条.TFTP软件或者FTP ...
- UEditor学习笔记1
首先对于前段时间遇到的一些总结做下记录: 0 == '' => true transition,渐变属性,其值可以是某属性,也可以是all,如transition: all 0.3s:鼠标放到 ...
- Spring国际化模块
1.Spring3.1.0实现原理分析(二).国际化(i18n) https://blog.csdn.net/roberts939299/article/details/69666291
- 单片机与android手机通信(控制LED小灯亮灭)
1.单片机实验板功能设计 为验证数据通信内容,让单片机板上的四个按键与android手机客户端上的四个LED灯相互控制:为达到上述基本实验要求,采用单字符传输数据即可,硬件需设计两块相同的单片机电路板 ...
- linux运行级别和开机流程
linux有七个运行级别 运行级别0:系统停机状态,系统默认运行级别不能设为0,否则不能正常启动 运行级别1:单用户工作状态,root权限,用于系统维护,禁止远程登陆 运行级别2:多用户状态(没有NF ...
- day14(1)--递归、匿名函数、内置函数
一.递归 定义:本质上是回溯和递推 回溯:询问答案的过程 递推:推出答案的过程 前提: 回溯到一个有结果的值开始递推 回溯与递推的条件要有规律 方式: 直接递归:自己调用自己 间接递归:通过别人来调用 ...