题目链接:http://www.bnuoj.com/v3/contest_show.php?cid=6453#problem/I

题目描述:

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.

输入格式:

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

输出格式:

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.

样例输入:

4
3 3
.#.
###
.#.
3 3
.#.
#.#
.#.
3 3
...
#.#
...
3 3
###
..#
#.#

样例输出:

Case 1: 1
Case 2: -1
Case 3: 0
Case 4: 2

分析思路:

开始想要枚举每一个点,看是否能将所有#搜完,但结果TLE。。。结果发现是有办法减少枚举量的,因为可以先用数组将所有的#信息存储下来,然后再枚举搜索所有的#就能找到最终答案了,初始化的时候注意初始化为INF,因为有些#有可能是不能被遍历到的,也可以初始化为-1.先找出选择两个#并遍历后最大的dist,再找出这其中最小的一个其实答案。

源代码如下:

//TLE...应该搜去只有#的区域,尽量进行有用的搜索
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
#define MAXN 50
#define INF 100000000struct Node {int x, y;
};
char Maze[MAXN][MAXN];
int dist[MAXN][MAXN];
int dir[4][2] = {-1, 0, 0, -1, 1, 0, 0, 1};
int N, M;
Node grass[MAXN*MAXN];int check(int dx, int dy) {int flag = 1;if (dx < 0 || dx >= N || dy < 0 || dy >= M) flag = 0;if (dist[dx][dy] != INF) flag = 0;return flag;
}
int BFS(Node sa, Node sd) {queue<Node> q;Node pre, now;now.x = sa.x; now.y = sa.y;q.push(now);dist[now.x][now.y] = 0;now.x = sd.x; now.y = sd.y;q.push(now);dist[now.x][now.y] = 0;while (!q.empty()) {pre = q.front(); q.pop();for (int i = 0; i < 4; i++) {now.x = pre.x + dir[i][0];now.y = pre.y + dir[i][1];if (check(now.x, now.y) && Maze[now.x][now.y] == '#') {q.push(now);dist[now.x][now.y] = dist[pre.x][pre.y] + 1;}}}
}
int main() {FILE *p = freopen("test.txt", "r", stdin);int T, i, j, d, s;while (~scanf("%d", &T)) {int c = 1;while (T--) {scanf("%d%d", &N, &M);for (i = 0; i < N; i++) {scanf("%s", Maze[i]);}int k = 0, tmin = INF;for (i = 0; i < N; i++) {for (j = 0; j < M; j++) {if (Maze[i][j] == '#') {grass[k].x = i;grass[k].y = j; k++;}}}for (i = 0; i < k; i++) {for (j = i; j < k; j++) {for (d = 0; d < N; d++) {for (s = 0; s < M; s++) {dist[d][s] = INF;}}BFS(grass[i], grass[j]);int tmax = 0;for (d = 0; d < k; d++) {tmax = max(tmax, dist[grass[d].x][grass[d].y]);//printf("dist = %d\n", dist[grass[d].x][grass[d].y]);}if (tmax == INF) continue;tmin = min(tmin, tmax);}}if (tmin == INF) printf("Case %d: -1\n", c++);else printf("Case %d: %d\n", c++, tmin);}}return 0;
}

FZU:I. Fire Game相关推荐

  1. 苹果:Kindle Fire与Android平板鹬蚌相争,iPad渔翁得利

    苹果公司CEO Tim Cook和CFO Peter Oppenheimer刚刚接受了Barclays分析师Ben Reitzes的采访,两位高管称,Kindle Fire会使得Android市场更加 ...

  2. python argparse_Python 命令行之旅:argparse、docopt、click 和 fire 总结篇

    本文首发于HelloGitHub公众号,并发表于Prodesire 博客. 一.前言 在近半年的 Python 命令行旅程中,我们依次学习了 argparse.docopt.click 和 fire ...

  3. python argparse_Python 命令行之旅:初探 argparse

    本文首发于 HelloGitHub 公众号,并发表于 Prodesire 博客. 前言 你是否好奇过在命令行中敲入一段命令后,它是如何被解析执行的?是否考虑过由自己实现一个命令行工具,帮你执行和处理任 ...

  4. 微信小程序 fire_如何在Fire TV和Fire TV Stick上侧面加载应用程序

    微信小程序 fire Amazon's Fire TV and Fire TV stick technically runs Android-but you wouldn't know it from ...

  5. 设计模式适配器模式_21世纪的设计模式:适配器模式

    设计模式适配器模式 这是我的演讲的第三部分," 21世纪的设计模式" . 适配器模式桥接世界. 在一个世界中,我们有一个概念的界面. 在另一个世界,我们有不同的界面. 这两个接口有 ...

  6. 21世纪的设计模式:适配器模式

    这是我的演讲" 21世纪的设计模式"的第三部分. 适配器模式桥接世界. 在一个世界中,我们有一个概念的界面. 在另一个世界,我们有不同的界面. 这两个接口有不同的用途,但有时我们需 ...

  7. Python fire官方文档教学(自动生成命令行,个人觉得意义不大,不如argparse)

    0x00 简介 欢迎来到Python Fire指南! Python Fire是一个Python库,只需对Fire进行一次调用即可将任何Python组件转变为命令行界面. 让我们开始吧! 0x01 安装 ...

  8. python argparse_Python 命令行之旅:深入 argparse(二)

    本文首发于 HelloGitHub 公众号,并发表于 Prodesire 博客. 前言 在上一篇"深入 argparse(一)"的文章中,我们深入了解了 argparse 的包括参 ...

  9. 轻量化卷积神经网络:SqueezeNet、MobileNet、ShuffleNet、Xception

    一 引言 二 轻量化模型 2.1 SqueezeNet 2.2 MobileNet 2.3 ShuffleNet 2.4 Xception 三 网络对比 一 引言 自2012年AlexNet以来,卷积 ...

最新文章

  1. WPF:WPF显示PDF文档 之 编译 MoonPdfLib库
  2. Python3 实现批量图像数据增强(扩增)并复制xml标签文件【目标检测笔记】
  3. requests模块使用代理
  4. 29.怎样扩展现有类功能?
  5. java中开根号求三角形_Java编程 如何计算三角形的内切圆半径?
  6. mysql2005触发器修改成绩_创建、更改和删除触发器
  7. 我眼中的服务提供和服务消费
  8. java opencv 平移_Java中使用opencv
  9. 003redis事务特性
  10. php无限次执行函数,php-PHP一个方法根据传递值怎么执行多次?
  11. 博途数据类型wstring怎么用_解析博图数据块(昆仑通态触摸屏自动命名)
  12. struts2自己定义拦截器
  13. SharePoint 设置Library中文档的默认打开方式
  14. 《SPSS统计分析与行业应用实战》之诊断试验与ROC分析
  15. 全网最新Linux全套教程
  16. python matrix用法_详解使用python绘制混淆矩阵(confusion_matrix)
  17. [C#]WPF 3D 绘制一个正方体并调整视场角
  18. 判断火车票座位python代码_12306 火车票监控Python代码详解
  19. 光栅(Raster)性能优化
  20. POJ1250解题报告

热门文章

  1. 初入行Web前端开发新手必读
  2. Java源码之HashMap
  3. 真实解决poetry遇到ImportError: cannot import name ‘Tortoise‘ from ‘tortoise‘ (unknown location)
  4. AD2428W手册解读之系统调试
  5. 如何让mysql榨干电脑性能_榨干多核CPU?这个方法你必须试试!
  6. JAVA课程分为几部分_语文课程目标体系可以分为几个学段?
  7. xamarin.ios如何使用HockeyApp
  8. 《Armv8/armv9架构入门指南》-【第一章】- 前言
  9. Python tkinter canvas方法的Bitmap、stipple参数和组件Bitmap属性的使用以及xbm文件
  10. python 引用其他文件内的函数,Python中在脚本中引用其他文件函数的方法