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 ...
随机推荐
- Spring4.x、SpringMVC和DButils整合
tomcat 8.Spring 4.X.JDK1.8 需要jar包: 1)日志组件:log4j # debug < info < warn < error log4j.rootLog ...
- selenium常用方法,简版介绍
WebElement 接口共计16个------------接口 代表一个HTML元素.通常,所有与页面交互有关的有趣操作都将通过此界面执行. void clear() void click() We ...
- .net core mvc全局设置跨域访问
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory ...
- HttpClient向后端的WebAPI工程发送HTTP的Post请求时,返回超过了最大请求长度的异常的解决方法
文章中的内容以及解决思路参考(转载)的 http://www.jb51.net/article/88698.htm 在WPF项目中通过HttpClient向后端的WebAPI工程发送HTTP的Post ...
- html5的使用
<!DOCTYPE html><html lang="en"><head> <meta charest="UTF-8" ...
- Jquery ajax 与 lazyload的混合使用(实现图片异步加载)
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- Java的HashMap和HashTable(转)
来源:http://www.cnblogs.com/devinzhang/archive/2012/01/13/2321481.html 1. HashMap 1) hashmap的数据结构 Has ...
- C# 连接 postgres失败
小程序完成了,要发布到客户的服务器上: 我的测试机:win10 64 数据库:PG9.6.5 64 客户服务器:win7 32 数据库: ...
- DB错误代码大全
db2错误代码大全 sqlcode sqlstate 说明000 00000 SQL语句成功完成01xxx SQL语句成功完成,但是有警告+012 01545 未限定的列名被解释为一个有相互关系的引 ...
- 百度地图API 基础入门
一.注册账号,获取密钥 流程-注册-登录-控制台-创建应用-获取密钥: 1.你想要调取百度地图,首先,你需要注册一个百度账号,获取密匙. 2.密钥获取以后,引入到你需要调用百度地图的界面中. 二.创建 ...