http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82828#problem/I

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
 
就一个简单的广搜题, 害的我A了2两天, 后来才发现是a[]数组开小了, -_- (不开心)
 
 
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
using namespace std; #define INF 0x3f3f3f3f
#define N 20 struct node
{
int x, y, step;
}a[N*N]; int n, m, t;
char G[N][N];
int f[N][N];
int dir[][] = {{-,},{,-},{,},{,}}; int BFS(node s1, node s2)
{
node p, q; queue<node>Q;
Q.push(s1);
Q.push(s2); t = ;
f[s1.x][s1.y] = ;
f[s2.x][s2.y] = ; while(Q.size())
{
p = Q.front(), Q.pop(); for(int i=; i<; i++)
{
q.x = p.x + dir[i][];
q.y = p.y + dir[i][]; if(q.x>= && q.x<n && q.y>= && q.y<m && G[q.x][q.y]=='#' && !f[q.x][q.y])
{
f[q.x][q.y] = ;
q.step = p.step + ;
t = max(t, q.step);
Q.push(q);
}
}
} return t;
} int Judge()
{
int i, j; for(i=; i<n; i++)
for(j=; j<m; j++)
{
if(G[i][j]=='#' && !f[i][j])
return ;
}
return ;
} int main()
{
int T, iCase=;
scanf("%d", &T);
while(T--)
{
int i, j, k=;
scanf("%d%d", &n, &m); memset(G, , sizeof(G));
for(i=; i<n; i++)
{
scanf("%s", G[i]);
for(j=; j<m; j++)
{
if(G[i][j]=='#')
{
a[k].x=i, a[k].y=j, a[k].step=;
k++;
}
}
} int ans=INF, Step;
for(i=; i<k; i++)
for(j=i; j<k; j++)
{
memset(f, , sizeof(f));
Step = BFS(a[i], a[j]);
if(Step<ans && Judge())
ans = Step;
} printf("Case %d: ", iCase++);
if(ans==INF)
printf("-1\n");
else
printf("%d\n", ans);
}
return ;
}
 
 

(广搜)Fire Game -- FZU -- 2150的更多相关文章

  1. Fire Game FZU - 2150 (bfs)

    Problem 2150 Fire Game Accept: 3772    Submit: 12868Time Limit: 1000 mSec    Memory Limit : 32768 KB ...

  2. kuangbin专题 专题一 简单搜索 Fire Game FZU - 2150

    题目链接:https://vjudge.net/problem/FZU-2150 题意:’ . '代表火无法烧着的地方,‘ # ’表示草,火可以烧着.选择任意两个‘ # ’(可以两个都选同一个 ‘ # ...

  3. ACM: FZU 2150 Fire Game - DFS+BFS+枝剪 或者 纯BFS+枝剪

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

  4. FZU 2150 Fire Game

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

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

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

  6. FZU 2150 Fire Game (暴力BFS)

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

  7. HDU--杭电--1195--Open the Lock--深搜--都用双向广搜,弱爆了,看题了没?语文没过关吧?暴力深搜难道我会害羞?

    这个题我看了,都是推荐的神马双向广搜,难道这个深搜你们都木有发现?还是特意留个机会给我装逼? Open the Lock Time Limit: 2000/1000 MS (Java/Others)  ...

  8. HDU 5652(二分+广搜)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/128683#problem/E 题目大意:给定一只含有0和1的地图,0代表可以走的格子,1代表不能走的格 子.之 ...

  9. nyoj 613 免费馅饼 广搜

    免费馅饼 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy ...

随机推荐

  1. JAVA课堂练习-动手动脑--数组

    1.阅读并运行示例PassArray.java,观察并分析程序输出的结果,小结,然后与下页幻灯片所讲的内容进行对照. 源代码: public class PassArray { public stat ...

  2. Luogu2022 有趣的数-二分答案+数位DP

    Solution 我好像写了一个非常有趣的解法233, 我们可以用数位$DP$ 算出比$N$小的数中 字典序比 $X$ 小的数有多少个, 再和 $rank$进行比较. 由于具有单调性, 显然可以二分答 ...

  3. POJ 3621Sightseeing Cows 0/1 分数规划

    Description 作为对奶牛们辛勤工作的回报,Farmer John决定带她们去附近的大城市玩一天.旅行的前夜,奶牛们在兴奋地 讨论如何最好地享受这难得的闲暇. 很幸运地,奶牛们找到了一张详细的 ...

  4. 设计师们做UI设计和交互设计、界面设计等一般会去什么网站呢?

    明明可靠颜值吃饭,却偏偏要靠才华立身,UI设计师就是这样一群神奇的物种.面对“大的同时小一点”.“五彩斑斓黑”.“下班之前给我”……这些甲方大大刁钻的需求,设计师每天都在咬牙微笑讨生活.你可以批评我的 ...

  5. @1-4使用Xpath解析豆瓣短评

    使用Xpath解析豆瓣短评 Python爬虫(入门+进阶)     DC学院 本节课程主要介绍解析神器Xpath是什么.Xpath如何安装及使用,以及使用实际的例子讲解Xpath如何解析豆瓣短评的网页 ...

  6. [Robot Framework] 通过RemoteWhiteLibrary启动程序并登录初尝试

    启动remote whitelibrary之后,在robot framework中配置好library,就可以调用whitelibrary的关键字了 启动APP White.LAUNCHAPP    ...

  7. 论坛:一对一关联映射/单向关联/两个类间,可以有两个(多个)关联关系/content为大文本类型/

    >>单向:只写一端的映射属性,另一端不写(有一端用不着);双向:两端都写映射属性 >>一对一关联有两类:一类基于主键的(一般不使用),一类基于外键的(重点学习): 外键:是一个 ...

  8. https传输过程嗅探

    C1->浏览器告知服务器自身的信息 length = 165 a5 16 03 01 00 A0 01 00 00 9C 03 03 5E 1C 37 CD 40 [ ^ 7 @] B6 4A ...

  9. java基本例子

    文件结构 D:\jp\jarDemo\IAmMainClass.java import iAmPackage.*; public class IAmMainClass { public static ...

  10. IOS初级:SDWebImage

    简单用法 #import "ViewController.h" #import "SDWebImage/UIImageView+WebCache.h" @int ...