(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的时候,记录答案的方法参考了 ...
随机推荐
- 转:Log Explorer使用说明恢复被误删除的数据
一.介绍 Log Explorer主要用于对MSSQLServer的事物分析和数据恢复.你可以浏览日志.导出数据.恢复被修改或者删除的数据(包括执行过update,delete,drop和trunca ...
- LeetCode算法题-Intersection of Two Arrays II(Java实现)
这是悦乐书的第208次更新,第220篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第76题(顺位题号是350).给定两个数组,编写一个函数来计算它们的交集.例如: 输入: ...
- JavaScript闭包理解【关键字:普通函数、变量访问作用域、闭包、解决获取元素标签索引】
一.闭包(Closure)模糊概述 之前总觉得闭包(Closure)很抽象而且难理解,百度一下"闭包"名词,百度的解释是:“闭包是指可以包含自由(未绑定到特定对象)变量的代 ...
- C - 继续畅通工程 最小生成树
省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).现得到城镇道路统计表,表中列出了任意两城镇间修建道路的费用,以及该道路是否已经 ...
- 隔离 docker 容器中的用户-------分享链接
https://www.cnblogs.com/sparkdev/p/9614326.html
- 在Ubuntu18.04上使用Anaconda
启动Anaconda Navigator 图形化界面 $ source ~/anaconda3/bin/activate root $ anaconda-navigator 查看目前的conda版本: ...
- ASP.Net:Javascript 通过PageMethods 调用后端WebMethod方法 + 多线程数据处理 示例
ASP.Net:Javascript 通过PageMethods 调用后端WebMethod方法 + 多线程数据处理 示例 2012年04月27日 16:59:16 奋斗的小壁虎 阅读数:4500 ...
- Android-App性能测试工具GT的使用方法
参考链接: https://www.cnblogs.com/syw20170419/p/7228145.html?utm_source=itdadao&utm_medium=referral ...
- wxWidgets 在 Linux 下开发环境配置
本文基于 CodeBlocks (16.0.1) 和 wxWidgets (3.0.2) 搭建 Linux 下 GUI 开发环境. 1. 安装 CodeBlocks Ubuntu 默认的源当前 Cod ...
- Luogu4983 忘情
Luogu4983 忘情 定义序列 \(x_1,\ x_2,\ \cdots,\ x_n\) 的值为 \(\frac{((\displaystyle\sum_{k=1}^nx_k\times \bar ...