Fire Game

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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
 
题意:给出一个m*n的图,‘#’表示草坪,‘ . ’表示空地,然后可以选择在任意的两个草坪格子点火,火每 1 s会向周围四个格子扩散,问选择那两个点使得燃烧所有的草坪花费时间最小
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cstdlib>
#include <cmath>
#include <cctype>
#define N 210
#define MAXN 0xfffffff using namespace std;
int m, n, T;
char maps[N][N];
bool f[N][N];
int dir[][] = {,, ,, ,-, -,}; struct node
{
int x, y;
int step;
}a[]; bool Fire()
{
for(int i = ; i < m; i++)
for(int j = ; j < n; j++) {
if(maps[i][j] == '#' && !f[i][j]) {
return false;
}
}
return true;
} int BFS(node s, node e)
{
int i;
queue<node>q;
q.push(s);
q.push(e);
f[s.x][s.y] = f[e.x][e.y] = true; while(q.size()) {
s = q.front();
q.pop(); e = s;
for(i = ; i < ; i++) {
e.x = s.x + dir[i][];
e.y = s.y + dir[i][];
e.step = s.step + ;
if(e.x >= && e.x < m && e.y >= && e.y < n && maps[e.x][e.y] == '#' && !f[e.x][e.y]) {
f[e.x][e.y] = true;
q.push(e);
}
}
}
return s.step;
} int main()
{
scanf("%d", &T); for(int k = ; k <= T; k++) {
scanf("%d %d", &m, &n);
int b = , ans = MAXN;
for(int i = ; i < m; i++) { //处理图像, maps值为true为草坪
scanf(" ");
for(int j = ; j < n; j++) {
scanf("%c", &maps[i][j]);
if(maps[i][j] == '#') {
a[b].x = i;
a[b].y = j;
a[b++].step = ;
}
}
} for(int i = ; i < b; i++) {
for(int j = i; j < b; j++) {
memset(f, , sizeof(f));
int temp_ans = BFS(a[i], a[j]);
if(ans > temp_ans && Fire()) {
ans = temp_ans;
}
}
}
if(ans == MAXN)
printf("Case %d: -1\n", k);
else
printf("Case %d: %d\n", k, ans);
} return ;
}
 

FZU 2150 Fire Game的更多相关文章

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

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

  2. fzu 2150 Fire Game 【身手BFS】

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

  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 (暴力BFS)

    [题目链接]click here~~ [题目大意]: 两个熊孩子要把一个正方形上的草都给烧掉,他俩同一时候放火烧.烧第一块的时候是不花时间的.每一块着火的都能够在下一秒烧向上下左右四块#代表草地,.代 ...

  5. (FZU 2150) Fire Game (bfs)

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=2150 Problem Description Fat brother and Maze are playing ...

  6. FZU 2150 Fire Game 广度优先搜索,暴力 难度:0

    http://acm.fzu.edu.cn/problem.php?pid=2150 注意这道题可以任选两个点作为起点,但是时间仍足以穷举两个点的所有可能 #include <cstdio> ...

  7. (简单) FZU 2150 Fire Game ,Floyd。

    Problem Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M board ...

  8. FZU 2150 Fire Game (bfs+dfs)

    Problem Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M board ...

  9. FZU 2150 Fire Game 【两点BFS】

    Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns) ...

随机推荐

  1. css样式基础三

    css的定位: 其中css中被分为块级元素与行内元素.如块级元素div.hx标签.p元素.行内元素span和strong W3school给出的一切皆为框的定义. 而且可以使用display属性,强行 ...

  2. PetGenie

    大概六.七年前当我还在学 Asphyre 的时候,有看过一个以之编写的类似对对碰的“宠物对对碰”小游戏,虽然很简单,但我当时还是小小的沉溺过数个小时.而不久前,在闲逛论坛时无意看到了个以 FireMo ...

  3. UVALive 3971 组装电脑

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  4. 搞清css的单位 px,em,rem的区别

    前言:现在上大街一眼望去,基本上90%的人都拿着手机,走路,逛街,吃东西都不停着,所以对于我们这种前端开发的程序猿来说,让网页适应于移动端可以说是必须要满足的.所以最近也是一直在学习和实践.然后就接触 ...

  5. jxl读数据库数据生成xls 并下载

    1.所需jar jxl-2.6.10.jar jxls-core-1.0-RC-3.jar jxls-reader-1.0-RC-3.jar 2. excel修改行宽度封装 SheetColumn.j ...

  6. Struts2 简介

    回顾Struts2的使用过程,网上搜的教程多多少少都会有点问题,重新记录下创建过程,方便查阅. 1.下载Struts2的jar包 下载地址:http://archive.apache.org/dist ...

  7. HTML meta锚点跳转 小tips

    小tips meta锚点跳转 http://www.zhangxinxu.com/wordpress/2015/03/meta-http-equiv-refresh-content/

  8. monkey 自己总结

    adb shell monkey -p com.test.smstest --pct-touch 50 --pct-motion 30 --pct-nav 20 -s 10 -v --throttle ...

  9. Android文件存储

    文件存储是Android中最基本的一种数据存储方式,它不读存储的内容进行任何的格式化处理,所有数据原封不动的保存在文件之中.如果想用文件存储的方式保存一些较为复杂的数据,就需要定义一套自己的格式规范, ...

  10. SQL Server Reporting Service(SSRS) 第二篇 SSRS数据分组Parent Group

    SQL Server Reporting Service(SSRS) 第一篇 我的第一个SSRS例子默认使用Table进行简单的数据显示,有时为了进行更加直观的数据显示,我们需要按照某个字段对列表进行 ...