FZU2150(KB1-I)

Fire Game

Accept: 1955    Submit: 6880
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

 1 //2017-02-28
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <queue>
 6
 7 using namespace std;
 8
 9 char grid[15][15], tmp[15][15];
10 int dx[4] = {0, 0, 1, -1};
11 int dy[4] = {1, -1, 0, 0};
12 int book[15][15], answer = 0x3f3f3f3f;
13 struct node
14 {
15     int x, y, step;
16 };
17
18 void bfs(int n, int m, int x1, int y1, int x2, int y2)
19 {
20     for(int i = 0; i < n; i++)
21       for(int j = 0; j < m; j++)
22         tmp[i][j] = grid[i][j];
23     int x, y, step, ans = 0;
24     tmp[x1][y1] = ‘*‘;
25     tmp[x2][y2] = ‘*‘;
26     queue<node> q;
27     node tmpnode;
28     tmpnode.x = x1;
29     tmpnode.y = y1;
30     tmpnode.step = 0;
31     q.push(tmpnode);
32     tmpnode.x = x2;
33     tmpnode.y = y2;
34     q.push(tmpnode);
35     while(!q.empty())
36     {
37         x = q.front().x;
38         y = q.front().y;
39         step = q.front().step;
40         for(int i = 0; i < 4; i++)
41         {
42             int nx = x + dx[i];
43             int ny = y + dy[i];
44             if(nx>=0&&nx<n&&ny>=0&&ny<m&&tmp[nx][ny]==‘#‘)
45             {
46                 tmpnode.x = nx;
47                 tmpnode.y = ny;
48                 tmpnode.step = step+1;
49                 ans = step+1;
50                 tmp[nx][ny] = ‘*‘;
51                 q.push(tmpnode);
52             }
53         }
54         q.pop();
55     }
56     for(int i = 0; i < n; i++)
57       for(int j = 0; j < m; j++)
58         if(tmp[i][j] == ‘#‘){
59             return;
60         }
61     if(ans < answer)answer = ans;
62 }
63
64 int main()
65 {
66     int T, n, m, cnt;
67     cin>>T;
68     for(int kase = 1; kase <= T; kase++)
69     {
70         cin>>n>>m;
71         cnt = 0;
72         for(int i = 0; i < n; i++)
73           for(int j = 0; j < m; j++)
74           {
75               cin>>grid[i][j];
76               if(grid[i][j] == ‘#‘)cnt++;
77           }
78         answer = 0x3f3f3f3f;
79         for(int i = 0; i < n; i++)
80               for(int j = 0; j < m; j++)
81             {
82                 if(grid[i][j] == ‘#‘){
83                     for(int i2 = i; i2 < n; i2++)
84                       for(int j2 = 0; j2 < m; j2++){
85                           if(i==i2 && j2 <= j)continue;
86                           if(grid[i2][j2] == ‘#‘)bfs(n, m, i, j, i2, j2);
87                       }
88                 }
89             }
90         if(answer == 0x3f3f3f3f)answer = -1;
91         if(cnt==1)answer=0;
92         cout<<"Case "<<kase<<": "<<answer<<endl;
93     }
94     return 0;
95 }
时间: 2024-12-20 23:12:25

FZU2150(KB1-I)的相关文章

KB-1

一:修改App的头像: 点击进入即: 在这里修改App的头像:步骤为: 29pt  2x:在图片打开后工具里像素修改为:58*58 3x:在图片打开后工具里像素修改为:87*87 40pt   2x:在图片打开后工具里像素修改为:80*80 3x:在图片打开后工具里像素修改为:120*120 60pt   2x:在图片打开后工具里像素修改为:120*120 3x:在图片打开后工具里像素修改为:180*180 二:修改App名称: Bundle name修改自己需要的即可. 三:修改引导页(两张引

FZU2150 Fire Game BFS搜索

题意:就是选两个点出发,只能走草坪,看能不能走完所有的草坪 分析:由于数据范围很小,所有枚举这两个点,事先将所有的草坪点存起来,然后任选两个点走,(两个点可以是同一个点) 然后BFS就行了 注:无解的情况很好做,事先深搜判连通块的个数就好,大于2就无解(代码比较烂) #include<cstdio> #include<algorithm> #include<iostream> #include<cstring> #include<cmath> #

FZU--2150 Fire Game(一道出乎意料的搜索)

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 c

fzu2150(bfs)

题目链接:http://acm.fzu.edu.cn/problem.php?pid=2150 题意:在任意两处点火,求最短时间烧光所有草堆. 分析:由于n,m比较小,将所有草堆坐标记录下来,然后暴力枚举所有可能的两处草堆为起点燃烧.最后取最短时间.求每两处燃烧需要用的时间一次bfs即可. #include <cstdio> #include <cstring> #include <cmath> #include <iostream> #include &l

拿出来分享了!VIP珍藏!!!全网最齐全的 DEDECMS模板 网盘地址!没有你找不到的!【第二篇】

拿出来分享了!VIP珍藏!!!全网最齐全的 DEDECMS模板 网盘地址!没有你找不到的! 模板类型最齐全: -------------优美的走起!---------- 五:DEDECMS模板--服装类模板列表: 织梦模板爱搭配服装行业dedecms门户模板下载.zip 35.25 MB昨天16:43VIP永久 服装行业dedecms模板.zip 138.09 KB昨天16:43VIP永久 织梦模板爱搭配服装行业dedecms门户模板下载.zip 35.25 MB昨天16:43VIP永久 201

吓磊郧颇芍l5f2n3lp

这枚戒指霍雨浩见过,平时王言都待在手上,是他的储物魂导器.只不过此时却已经完全破损了,以霍雨浩魂导师的眼光自然看得出,这枚戒指的核心法阵已经被破坏.至于那柄散发着淡淡森寒气息的刻刀,可不正是那柄排名九十九位的列榜刻刀噬灵么?腰间挂着三枚史莱克监察者专用的信号弹.贝贝不屑的看了他一眼,道:"学习个屁.人家那泡妞手段是建立在相貌的基础上.你有相貌这种东西么?你要是有我一半英俊,也不至于混成现在这样."霍雨浩摇了摇头,微笑道:"算了,我估计王老师以后还会再找我的.咱们获得了全胜的好

转:软件版本的定义

from: http://m.blog.csdn.net/article/details?id=8086731 软件版本RC,M,GA…… 发表于2012/10/18 17:25:45  3469人阅读 分类: 数据库 杂&扯淡 RC版本 RC:(Release Candidate) Candidate是候选人的意思,用在软件上就是候选版本.Release是发行.发布的意思.Release.Candidate.就是发行候选版本.和Beta版最大的差别在于Beta阶段会一直加入新的功能,但是到了R

侍嗜烟首闪rb2695235f271119

是的,霍雨浩早已达到了极限,玄天功带来的那股暖意固然能够滋润他的身体,一定程度的减缓疲劳.但却不能让他真的变强啊!"给我觉醒武魂啊!你是说,你要处置冰帝?"霍雨浩终于反应过来了,只不过他实在是没力气,想要坐起来.力量用到一半,又倒了回去.真的是魂帝?这是所有观战者共同的想法.这也是本届大赛第一次出现的魂帝.如果您觉得还不错就请收藏本站,以便下次方便看书. 如有章节错误请与管理员联系.本月为您推荐唐家三少最新巨著<绝世唐门>这个时候,戴钥衡和陈子锋更加意识到了精神探测的妙用,

android 项目实战——超级课程表课表一键提取功能

如果你是在校大学生,或许你用多了各种课程表,比如课程格子,超级课程表.它们都有一个共同点就是可以一键导入教务处的课程.那么一直都是用户的我们,没有考虑过它是如何实现的.那么现在就来模仿一款"超级课程表". PS:由于超级课程表是商用软件,原本提取了一些图片,但是为了避免涉及侵权问题,所有图片均已使用一张绿色圆圈代替,背景图片也以颜色代替,缺乏美观,如果你觉得太丑,可以自己寻找图片代替. 那么说了这么久,先来看看这款高仿的软件长什么样子.本文的代码做过精简,所以界面可能有出入. 好了,界