Fire Game FZU - 2150 (bfs)
Accept: 3772 Submit: 12868
Time Limit: 1000 mSec Memory Limit : 32768 KB
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<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<set>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-10
#define PI acos(-1.0)
#define ll long long
int const maxn = ;
const int mod = 1e9 + ;
int gcd(int a, int b) {
if (b == ) return a; return gcd(b, a % b);
}
char maze[maxn][maxn];
int ans;
struct point
{
int x,y;
};
int n,m;
int dis[maxn][maxn];
queue<point>que;
int dx[]={,,-,};
int dy[]={,,,-};
int bfs(int x1,int y1,int x2,int y2)
{
while(!que.empty())
que.pop();
memset(dis,INF,sizeof(dis));
point p1,p2,next;
p1.x=x1;
p1.y=y1;
p2.x=x2;
p2.y=y2;
dis[x1][y1]=;
dis[x2][y2]=;
que.push(p1);
que.push(p2);
while(!que.empty())
{
point p=que.front();
que.pop();
for(int i=;i<;i++)
{
int nx=p.x+dx[i];
int ny=p.y+dy[i];
if(nx>= && nx<n && ny>= && ny<m && maze[nx][ny]=='#' && dis[nx][ny]>dis[p.x][p.y]+)
{
dis[nx][ny]=dis[p.x][p.y]+;
next.x=nx;
next.y=ny;
que.push(next);
}
}
}
int maxx=;
for(int i=;i<n;i++)
for(int j=;j<m;j++)
if(maze[i][j]=='#')
maxx=max(maxx,dis[i][j]);
return maxx;
}
int main()
{
int t; scanf("%d",&t);
int ca=;
while(t--)
{
int ans=INF;
scanf("%d %d",&n,&m);
for(int i=;i<n;i++)
for(int j=;j<m;j++)
cin>>maze[i][j];
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(maze[i][j]=='#')
{
for(int ii=;ii<n;ii++)
{
for(int jj=;jj<m;jj++)
{
if(maze[ii][jj]=='#')
{
int temp=bfs(i,j,ii,jj);
ans=min(ans,temp);
}
}
}
}
}
}
if(ans==INF)
ans=-;
printf("Case %d: %d\n",ca++,ans);
}
}
Fire Game FZU - 2150 (bfs)的更多相关文章
- Fire Again CodeForces - 35C (BFS)
After a terrifying forest fire in Berland a forest rebirth program was carried out. Due to it N rows ...
- 深搜(DFS)广搜(BFS)详解
图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...
- 【算法导论】图的广度优先搜索遍历(BFS)
图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...
- 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现
1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...
- 【BZOJ5492】[HNOI2019]校园旅行(bfs)
[HNOI2019]校园旅行(bfs) 题面 洛谷 题解 首先考虑暴力做法怎么做. 把所有可行的二元组全部丢进队列里,每次两个点分别向两侧拓展一个同色点,然后更新可行的情况. 这样子的复杂度是\(O( ...
- 深度优先搜索(DFS)和广度优先搜索(BFS)
深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...
- 图的 储存 深度优先(DFS)广度优先(BFS)遍历
图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...
- 数据结构与算法之PHP用邻接表、邻接矩阵实现图的广度优先遍历(BFS)
一.基本思想 1)从图中的某个顶点V出发访问并记录: 2)依次访问V的所有邻接顶点: 3)分别从这些邻接点出发,依次访问它们的未被访问过的邻接点,直到图中所有已被访问过的顶点的邻接点都被访问到. 4) ...
- 层层递进——宽度优先搜索(BFS)
问题引入 我们接着上次“解救小哈”的问题继续探索,不过这次是用宽度优先搜索(BFS). 注:问题来源可以点击这里 http://www.cnblogs.com/OctoptusLian/p/74296 ...
随机推荐
- Oracle子查询和多表查询
多表查询需要用到表的连接 连接可以分为:(自行百度) 交叉连接(数字逻辑的笛卡尔积,不做解释) 等值连接 例如:select * from t_a, t_b where t_a.xx = t_b.xx ...
- nginx中文手册内容说明
1.什么是nginx? Nginx 是一个高性能的 Web 和反向代理服务器, 它具有有很多非常优越的特性: 作为 Web 服务器:相比 Apache,Nginx 使用更少的资源,支持更多的并发连接, ...
- Docker | 第一章:Docker简介
前言 作为本系列的起始章节,本章节主要是对Docker的相关概念进行简单阐述下.自此也是查阅了相关资料,奈何也都是英文版居多,看的是有点头大的.现在悔不当初不好好学习英文了.o(︶︿︶)o 唉 Doc ...
- Java面向对象的练习。动物乐园
本次项目是:以面向对象的思想设计动物乐园系统. 动物乐园中有猫,狗,鸭子等成员,还可以增加新成员. 猫和鸭子都有自己的名字,都有自己的腿,但腿的条数不同,猫和鸭子会发出叫声,猫的叫声是:喵喵喵……,鸭 ...
- java 整型相除得到浮点型
public class TestFloatOrDouble { public static void main(String[] args) { Point num1 = new Point(84, ...
- AngularJS(九):路由
本文也同步发表在我的公众号“我的天空” AngularJS路由 AngularJS路由可以让我们通过不同的URL访问不同页面(似乎是废话),其价值主要体现在单页面的web应用中(single page ...
- 软件测试Lab 1 Junit and Eclemma
首先安装eclipse 然后下载hamcrest-core-1.3.jar,下载地址:http://mvnrepository.com/artifact/org.hamcrest/hamcrest-c ...
- JAXB介绍一
参考博客: https://www.cnblogs.com/chenbenbuyi/p/8283657.html https://www.cnblogs.com/cnsdhzzl/p/8390514. ...
- UVA Live Archive 4015 Cave (树形dp,分组背包)
和Heroes Of Might And Magic 相似,题目的询问是dp的一个副产物. 距离是不好表示成状态的,但是可以换一个角度想,如果知道了从一个点向子树走k个结点的最短距离, 那么就可以回答 ...
- Android(java)学习笔记154:采用HttpClient提交数据(qq登录案例)
1.Apache -Httpclient HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包 ...