题解:一开始想错了,以为只要烧完就是那个答案,但是这不是最优的结果,需要每两个点都bfs一遍,找到如果能够全部烧完,找到花费时间最小的,如果不能return -1。在bfs的时候,记录答案的方法参考了一下其他大神的思路,把能烧到地方都需要能够用个二维数组dis[ ]来标记烧到这个地方时所用的时间是多少。在经过一次两点的bfs后就需要找dis[ ]中最大的那个点,因为这一定是烧完的最后一个点。最后找能烧完的答案中最小的那个就可以了。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
#include<cstdio>
#include<queue>
using namespace std;
const int INF = 0x3f3f3f3f;
char gra[20][20];
int dis[20][20];
int dx[4] = {1,-1,0,0};
int dy[4] = {0,0,1,-1};
struct node
{
int x,y;
};
int T,n,m;
int bfs(int x1, int y1, int x2, int y2)
{
struct node now, next;
queue<node>q;
int tx, ty;
memset(dis,INF,sizeof(dis));
now.x = x1; now.y = y1;
q.push(now);
now.x = x2; now.y = y2;
q.push(now);
dis[x1][y1] = dis[x2][y2] = 0;
while(!q.empty())
{
now = q.front();
q.pop();
for(int i =0; i < 4; i ++)
{
tx = now.x + dx[i];
ty = now.y + dy[i];
if(tx >= 0 && tx < n && ty >= 0 && ty < m && gra[tx][ty] == '#' && dis[tx][ty] > dis[now.x][now.y] + 1)
{
dis[tx][ty] = dis[now.x][now.y] + 1;
next.x = tx;
next.y = ty;
q.push(next);
}
}
}
int maxn = 0;
for(int i = 0; i < n; i ++)
{
for(int j = 0; j < m; j ++)
{
if(gra[i][j] == '#')
{
maxn = max(maxn,dis[i][j]);
}
}
}
return maxn;
}
int main()
{
cin >> T;
for (int cas = 1; cas <= T; cas++)
{ cin >> n >> m;
for (int i = 0; i < n; i++)
{
cin >> gra[i];
}
int ans = INF;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (gra[i][j] == '#')
for (int k = 0; k < n; k++)
{
for (int p = 0; p < m; p++)
{
if (gra[k][p] == '#')
{
int temp = bfs(i, j, k, p);
ans = min(ans, temp);
}
}
}
}
}
if (ans == INF)
ans = -1;
cout << "Case " << cas << ": " << ans << endl;
}
return 0;
}

Problem

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

Fire Game (FZU 2150)(BFS)的更多相关文章

  1. poj1753(位运算压缩状态+bfs)

    题意:有个4*4的棋盘,上面摆着黑棋和白旗,b代表黑棋,w代表白棋,现在有一种操作,如果你想要改变某一个棋子的颜色,那么它周围(前后左右)棋子的颜色都会被改变(白变成黑,黑变成白),问你将所有棋子变成 ...

  2. HDU 4845 拯救大兵瑞恩(分层图状压BFS)

    拯救大兵瑞恩 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Sub ...

  3. [HNOI2006]最短母串问题(AC自动机+状态压缩+bfs)

    快要THUSC了,来水几道模板题吧. 这题其实是AC自动机模板.看到长度最短,首先就想到AC自动机.那么就直接暴力法来吧,把每个串建立在AC自动机上,建立fail指针,然后由于n<=12,可以把 ...

  4. Aizu 0531 "Paint Color" (坐标离散化+DFS or BFS)

    传送门 题目描述: 为了宣传信息竞赛,要在长方形的三合板上喷油漆来制作招牌. 三合板上不需要涂色的部分预先贴好了护板. 被护板隔开的区域要涂上不同的颜色,比如上图就应该涂上5种颜色. 请编写一个程序计 ...

  5. hdu4435 charge-station(先建后拆+bfs)

    charge-station Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  6. UVA 1599 Ideal Path(bfs1+bfs2,双向bfs)

    给一个n个点m条边(<=n<=,<=m<=)的无向图,每条边上都涂有一种颜色.求从结点1到结点n的一条路径,使得经过的边数尽量少,在此前提下,经过边的颜色序列的字典序最小.一对 ...

  7. 有向图的邻接矩阵表示法(创建,DFS,BFS)

    package shiyan; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; publi ...

  8. Paint the Grid Reloaded(缩点,DFS+BFS)

    Leo has a grid with N rows and M columns. All cells are painted with either black or white initially ...

  9. hdu 5438 Ponds(长春网络赛 拓扑+bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5438 Ponds Time Limit: 1500/1000 MS (Java/Others)     ...

随机推荐

  1. And Reachability CodeForces - 1169E (有向图可达性)

    大意: 给定序列$a$, 对所有的a[i]&a[j]>0, 从$i$向$j$连一条有向边, 给出$m$个询问$(x,y)$, 求是否能从$x$到达$y$. 裸的有向图可达性, 有向图可达 ...

  2. nop4.1用2008r2的数据库

    修改appsetting.json

  3. centos中mariadb的相关操作

    Tip 1 在使用mariadb中启动服务报错 : Failed to start mariadb.service: Unit not found. 解决办法: yum install -y mari ...

  4. PS 中混合模式

    1.正常模式 2. 溶解 3. 变暗    :  把两幅图中较暗的区域显示出来 4.正片叠底   总体变暗,把图层中较浅的颜色由下一图层较深的颜色显现(和滤色相反) 7. 深色  取较小的颜色 8. ...

  5. Unknown column '字段名' in 'field list' 错误解决方案

    当后台报这个错误的时候,说明实体类中的字段名出错了,解决方案: 1,检查数据库中的字段名与实体类中的字段名是否一致,特别要注意单词字母, 2,检查数据库中的字段是否与实体类中的字段一致.比如数据库中没 ...

  6. Hadoop 3相对于hadoop 2的新特性

    相对于之前主要生产发布版本Hadoop 2,Apache Hadoop 3整合许多重要的增强功能. Hadoop 3是一个可用版本,提供了稳定性和高质量的API,可以用于实际的产品开发.下面简要介绍一 ...

  7. Docker 部署mysql、tomcat笔记

    Docker 笔记整理 #.环境:Ubuntu 18.* #.安装 mysql 5.6.tomcat #.docker search mysql 报错:Error response from daem ...

  8. 关于登陆界面,页面没有刷新完毕,点击登陆跳转到一个接口的bug

    现象 输入完密码点击登陆就跳转到了如下的页面 分析原因: 第一:查看html页面   页面中的html  登陆用的是form表单  表单中还写了属性  action   即允许跳到某一个接口,这里是没 ...

  9. npm install 常用的几个参数

    npm install moduleName # 安装模块到项目目录下 npm install -g moduleName # -g 的意思是将模块安装到全局,具体安装到磁盘哪个位置,要看 npm c ...

  10. Anaconda--机器学习环境搭建

    使用Anaconda为机器学习和深度学习设置Python环境 一.安装Anaconda,并更新到最新版本 1.Anaconda 安装: 官方网站下载地址 https://www.anaconda.co ...