Fire Game

来源:互联网 发布:域名商 编辑:程序博客网 时间:2024/05/17 04:04

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
可以再两个地方(可重合)放火,#可以烧,.不能烧。问最快的时间烧完所有的草。看了一下数据范围,还是可以乱搞的,把草标号,然后两个两个搜!如果烧的草数量对的话,就比较time。不一样就说明不能烧完。最后输出就可以了。

#include<cstdio>#include<cstring>#include<iostream>#include<queue>using namespace std;int n, m, num, dir[4][2] = { 1, 0, -1, 0, 0, 1, 0, -1 };int leap[12][12];char a[12][12];struct Node{    int x;    int y;    int ans;}node[105];queue<Node>p;int max(int a, int b){    return a > b ? a : b;}int min(int a, int b){    return a < b ? a : b;}int main(){    int i, j, ans, t, T, i1, j1, tempx, tempy, sum, time;    Node temp, now;    cin >> T; t = 0;    while (++t <= T)    {        cin >> n >> m;        getchar(); num = 0; time = 999;        for (i = 1; i <= n; i++)        {            for (j = 1; j <= m; j++)            {                scanf("%c", &a[i][j]);                if (a[i][j] == '#')                {                    num++;                    node[num].x = i; node[num].y = j;                }            }            getchar();        }        for (i = 1; i <= num; i++)        {            for (j = i; j <= num; j++)            {                memset(leap, 0, sizeof(leap));                leap[node[i].x][node[i].y] = 1;                leap[node[j].x][node[j].y] = 1;                node[i].ans = node[j].ans = 0;                p.push(node[i]);                p.push(node[j]);                sum = 0; ans = 0;                while (p.size())                {                    temp = p.front();                    for (i1 = 0; i1 <= 3; i1++)                    {                        tempx = temp.x + dir[i1][0];                        tempy = temp.y + dir[i1][1];                        now.x = tempx; now.y = tempy;                        if (tempx>=1&&tempx<=n&&tempy>=1&&tempy<=m){                            if (!leap[tempx][tempy] && a[tempx][tempy] == '#')                            {                                leap[tempx][tempy] = 1;                                now.ans = temp.ans + 1;                                ans = max(ans, now.ans);                                sum++;                                p.push(now);                            }                        }                    }                    p.pop();                }                if (sum == num-1&&i==j)                {                    time = min(time, ans);                }                else if (sum == num - 2 && i != j)                {                    time = min(time, ans);                }            }        }        cout << "Case " << t << ": ";        if (time == 999)            cout << "-1" << endl;        else            cout << time << endl;    }    return 0;}
0 0
原创粉丝点击