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. js原生语法实现表格操作

    HTML页面: <!doctype html> <html lang="en"> <head> <meta charset="U ...

  2. 织梦文章里面的图片alt和title属性,用文章标题自动替换

    把{dede:field.body/}改成{dede:field.body runphp=yes}global $dsql,$id,$aid;$myid = isset($id) ? $id : $a ...

  3. (转)system.badimageformatexception 未能加载文件或程序集

    “/xxxxx”应用程序中的服务器错误. ------------------------------------------------------------------------------- ...

  4. Numpy copy & deep copy

    1. '='的赋值方式会带有关联性 >>> import numpy as np >>> a = np.arange(4) >>> b = a & ...

  5. instanceof 和 typeof

    instanceof 运算符用来检测 constructor.prototype 是否存在于参数 object 的原型链 function Person(){ Person.prototype.dan ...

  6. Codeforces 757B. Bash's Big Day GCD

    B. Bash's Big Day time limit per test:2 seconds memory limit per test:512 megabytes input:standard i ...

  7. Eclipse设置jre版本 或者 jdk

    设置Eclipse默认的 JRE 版本 Eclipse 配置 JDK 的方法和配置 JRE 相同 windows --> Preferences --> Java --> 完成后查看 ...

  8. inet_pton和inet_ntop inet_ntoa

    Linux下地址转换函数 inet_pton ==> Ox inet_ntop ==> xxx.xxx.xxx #include <sys/types.h> #include ...

  9. C# 单例模式的五种写法

    1.简单实现           C#   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 public sealed c ...

  10. 2019.02.06 bzoj4503: 两个串(fft)

    传送门 题意简述:给两个字符串s,ts,ts,t,ttt中可能有通配符,问ttt在sss出现的次数和所有位置. 思路:一道很熟悉的题,跟bzoj4259bzoj4259bzoj4259差不多的. 然后 ...