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 ;
}

FZOJ Problem 2150 Fire Game的更多相关文章

  1. FZU Problem 2150 Fire Game

    Problem 2150 Fire Game Accept: 145    Submit: 542 Time Limit: 1000 mSec    Memory Limit : 32768 KB P ...

  2. FZU Problem 2150 Fire Game(bfs)

    这个题真要好好说一下了,比赛的时候怎么过都过不了,压点总是出错(vis应该初始化为inf,但是我初始化成了-1....),wa了n次,后来想到完全可以避免这个问题,只要入队列的时候判断一下就行了. 由 ...

  3. FZU 2150 fire game (bfs)

    Problem 2150 Fire Game Accept: 2133    Submit: 7494Time Limit: 1000 mSec    Memory Limit : 32768 KB ...

  4. FZU 2150 Fire Game(点火游戏)

    FZU 2150 Fire Game(点火游戏) Time Limit: 1000 mSec    Memory Limit : 32768 KB Problem Description - 题目描述 ...

  5. fzu 2150 Fire Game 【身手BFS】

    称号:fzupid=2150"> 2150 Fire Game :给出一个m*n的图,'#'表示草坪,' . '表示空地,然后能够选择在随意的两个草坪格子点火.火每 1 s会向周围四个 ...

  6. UVA Problem B: Fire!

    Problem B: Fire! Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and t ...

  7. FZOJ Problem 2219 StarCraft

                                                                                                        ...

  8. BFS(两点搜索) FZOJ 2150 Fire Game

    题目传送门 题意:'#'表示草地,两个人在草地上点火,相邻的草地会烧起来,每烧一格等1秒,问最少要等几秒草地才烧完 分析:这题和UVA 11624 Fire!有点像,那题给定了两个点,这题两点不确定, ...

  9. FZU 2150 Fire Game

    Fire Game Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit St ...

随机推荐

  1. 01_5_Struts_ActionMethod_DMI_动态方法调用

    01_5_Struts_ActionMethod_DMI_动态方法调用 1. ActionMethod_DMI_动态方法调用 Action执行的时候并不一定要执行execute()方法 可以在配置文件 ...

  2. Sql Server 查询今天,昨天,近七天....数据

    今天数据: 昨天数据: 7天内数据: 30天内数据: 本月数据: 本年数据: 查询今天是今年的第几天: select datepart(dayofyear,getDate()) 查询今天是本月的第几天 ...

  3. CF-629 D - Babaei and Birthday Cake (离散化 + 线段树|树状数组)

    求上升子序列的最大和.O(n^2)会暴力,在查询的时候要用线段树维护 因为权值是浮点数,故先离散化一下,设第 i 个位置的权值,从小到大排名为 id.那么dp转移中 \[d[i] = max(d[i] ...

  4. Pycharm安装类库

    比如安装requests 打开settings,选择Project 下面的Project Interpreter,点击pip,在弹出窗口里输入requests然后点击install 即可!

  5. NodeJS基础入门-Event

    大多数Node.js核心API都采用惯用的异步事件驱动架构,其中某些类型的对象(触发器)会周期性地触发命名事件来调用函数对象(监听器). 例如,net.Server对象会在每次有新连接时触发事件;fs ...

  6. paper:synthesizable finite state machine design techniques using the new systemverilog 3.0 enhancements 之 standard verilog FSM conding styles(三段式)

    Three always block style with registered outputs(Good style)

  7. 一个炫酷的flash网站模板

    这是一个炫酷的flash欧美网站模板,它包括首页,公司简介,留言等五个页面,界面转换非常的炫酷!他还有时间.全屏.背景音乐开关的功能!有兴趣的朋友可以看看!贴几张网站图片给大家看看! 下载后直接找到s ...

  8. 第一课 项目的介绍 Thinkphp5第四季

    学习地址: https://study.163.com/course/courseLearn.htm?courseId=1004887012#/learn/video?lessonId=1050543 ...

  9. L2-006 树的遍历 RTA

    L2-006 树的遍历(25 分) 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列.这里假设键值都是互不相等的正整数. 输入格式: 输入第一行给出一个正整数N(<=30),是二叉树 ...

  10. Root CA certificate:ApacheJMeterTemporaryRootCA.crt created in JMeter bin directory

    今天学习jmeter录制,在点击start之后弹出: 且在jmeter安装目录里确实生成了ApacheJMeterTemporaryRootCA.crt文件 上网查询官方文档http://120.52 ...