FZU2150BFS

来源:互联网 发布:node js 内网代理 编辑:程序博客网 时间:2024/06/01 08:19
Problem 2150 Fire Game

Accept: 2485    Submit: 8611
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

        题意:给出一个图,#表示草堆.表示不能烧的。选两个格点点燃草堆,火每次都要向上下左右有草的四个方向移动。求最短烧完草的时间。
        解题思路:开始的时候我以为是DFS+BFS题,就是用DFS扫一遍,超过两个草堆就直接挂了,然后再用BFS扫一遍找最少时间。但是我写完DFS发现这样并不能节省时间。然后去网上看了大牛的博客,普遍性的思路就是双起点BFS。因为这个题目的数据很小,所以可以枚举出所有的两个#来进行BFS。这样最后再看烧没烧完,和最短时间,就算只有一个连通块也能解决。以下是我的AC代码
#include <iostream>#include <cstdio>#include <queue>#include <memory.h>using  namespace std;#define maxn 0x3f3f3fstruct trip{    int x;    int y;};int n, m;char pic[15][15];int flag[15][15];int mm[4][2]= {{1,0},{0,1},{-1,0},{0,-1}};int bfs(int x1, int y1, int x2, int y2){    queue<trip> q;    int xx, yy;    trip p1, p2, now, next;    memset(flag, maxn, sizeof(flag));    p1.x = x1;    p1.y = y1;    p2.x = x2;    p2.y = y2;    flag[x1][y1] = 0;    flag[x2][y2] = 0;    q.push(p1);    q.push(p2);    while (!q.empty())    {        now = q.front();        q.pop();        for (int i = 0; i<4; i++)        {            xx = now.x + mm[i][0];            yy = now.y + mm[i][1];            if (xx >= 0 && xx<n&&yy>=0 && yy<m&&pic[xx][yy] == '#'&&flag[xx][yy]>flag[now.x][now.y] + 1)            {                flag[xx][yy] = flag[now.x][now.y] + 1;                next.x = xx;                next.y = yy;                q.push(next);            }        }    }    int maxx = 0;    for (int i = 0; i < n; i++)        for (int j = 0 ; j < m; j++)            if (pic[i][j] == '#')                maxx = max(maxx, flag[i][j]);    return maxx;}int main(){    int t;    cin >> t;    for (int z = 1; z <= t; z++)    {        cin >> n >> m;        for (int i = 0; i < n; i++)            cin >> pic[i];        int temp;        int ans = maxn;        for (int i = 0; i < n; i++)            for (int j = 0; j < m; j++)                if (pic[i][j] == '#')                    for (int ii = 0; ii < n; ii++)                        for (int jj = 0; jj < m; jj++)                            if (pic[ii][jj] == '#')                            {                                temp = bfs(i, j, ii, jj);                                ans = min(ans, temp);                            }        if (ans == maxn) ans = -1;        cout << "Case " << z << ": " << ans << endl;    }}

原创粉丝点击