Problem 2150 Fire Game

Accept: 2185    Submit: 7670
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

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

Sample Output

Case 1: 1
Case 2: -1
Case 3: 0
Case 4: 2
 
 
题意:两个“变态”在给定的区域内烧草坪,给定区域为N*M的矩形阵,起先两个人同时各选定矩阵中的一块草坪并开始烧,并且火会蔓延,只考虑当前草坪的上下左右四个格子,如果这些临近的格子中有草坪,那么火会蔓延至临近的格子中,此时火延续的时间加1.
问两个人分别应该选定哪两块草坪开始烧,使得火以最短的时间烧完矩阵中所有的草坪,如果火灭了任然还有草坪没被烧,则判定为失败,输出-1,否则输出火的最短延续时间.
思路:广度优先搜索,从两个点起始点同时开始搜索即可。
AC代码:

#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<cstring>
using namespace std;
const int N_MAX = + ;
int N,M;
char field[N_MAX][N_MAX];
bool vis[N_MAX][N_MAX];
int dx[] = { -,,, };
int dy[] = { ,,-, };
queue<pair<int,int> >que;
int num[N_MAX][N_MAX]; int bfs(int x1,int y1,int x2,int y2) {//返回 烧完所有草需要的时间
memset(vis, , sizeof(vis));//记录走过的点
memset(num, , sizeof(num));//记录到达某点的时间
int Max=;
que.push(make_pair(x1,y1));
que.push(make_pair(x2, y2));
vis[x1][y1] = vis[x2][y2] = ;
while (!que.empty()) {
int xx=que.front().first;
int yy = que.front().second;
que.pop();
for (int i = ; i < ; i++) {
int x = xx + dx[i];
int y = yy + dy[i];
if (x >= && x < N&&y >= && y < M&&!vis[x][y]&&field[x][y] == '#') {
vis[x][y] = true;
que.push(make_pair(x, y));
num[x][y] = num[xx][yy]+;
if (Max < num[x][y])Max = num[x][y];
}
}
}
for (int i = ; i < N;i++)
for (int j = ; j < M; j++)
if (field[i][j] == '#'&&!vis[i][j]) {
Max = INT_MAX;
} return Max;
} int main() {
int T,number;
scanf("%d", &T);
int cs = ;
while (T--) {
number = ;
cs++;
scanf("%d%d",&N,&M);
memset(field, , sizeof(field));
for (int i = ; i < N;i++) {
for (int j = ; j < M;j++) {
scanf(" %c",&field[i][j]);
if (field[i][j] == '#')
number++;
}
}
if (number <= ) {//!!!!草坪数小于2不用搜索了
printf("Case %d: %d\n", cs, );
continue;
}
int min_time=INT_MAX;
for (int i = ; i < N*M;i++) {
int x1 = i / M; int y1 = i%M;
if (field[x1][y1] != '#')continue;
for (int j = i+; j < N*M;j++) {
int x2 = j / M; int y2 = j%M;
if (field[x2][y2] != '#')continue;
int tmp= bfs(x1, y1, x2, y2);
if (tmp < min_time)
min_time = tmp;
}
} if (min_time == INT_MAX)min_time = -;
printf("Case %d: %d\n",cs,min_time); }
return ;
}

最新文章

  1. HTTPS那些事(三)攻击实例与防御(转载)
  2. 解决window2012 IIS8 配置的网站无法下载exe文件的问题
  3. XH
  4. daterangepicker 双日历/格式化日期/日期限制minDate,maxDate
  5. css翻页样式
  6. JS来添加弹出层,并且完成锁屏
  7. Leetcode: Insert Delete GetRandom O(1)
  8. cojs 疯狂的粉刷匠 疯狂的斐波那契 题解报告
  9. 关于socket阻塞与非阻塞情况下的recv、send、read、write返回值(转载)
  10. ios7新特性3-Map Kit新特性
  11. 【POJ2699】The Maximum Number of Strong Kings(网络流)
  12. android开源应用(主要是博客上带有分析的)收集 【持续更新】
  13. 『Kruscal重构树 Exkruscal』
  14. 1、Qt Project之基本文件打开与保存
  15. Remove Duplicate Letters(Java 递归与非递归)
  16. 2016年,谁是最受欢迎的 Java EE 服务器?
  17. 洛谷P1938 找工就业
  18. Log4net的配置-按照日期+文件大小混合分割
  19. hashCode和equals方法的区别与联系
  20. javascript总结16:数组array12

热门文章

  1. JavaScript深入浅出第2课:函数是一等公民是什么意思呢?
  2. 【转】PCA for opencv
  3. 快速启动mongodb服务
  4. 简单了解hash
  5. BZOJ 4368: [IOI2015]boxes纪念品盒
  6. 洛谷P1085不高兴的津津
  7. 反射的妙用-类名方法名做参数进行方法调用实例demo
  8. 大数据学习——sparkSql对接hive
  9. python补漏----isinstance 和 issubclass
  10. 利用Python分析羊车门问题