FZU 2150 Fire Game(多起点bfs)

来源:互联网 发布:加长棍刀在淘宝叫什么 编辑:程序博客网 时间:2024/05/22 09:40

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
43 3.#.###.#.3 3.#.#.#.#.3 3...#.#...3 3###..##.#
Sample Output
Case 1: 1Case 2: -1Case 3: 0Case 4: 2
兄弟两个熊孩子烧草丛玩,火在一秒内可以向四个方向扩展一格,空地不能燃烧,点两把火,求问最少需要几秒才能烧完草丛
数据量不算大,直接暴力枚举加两个起点广搜就好,本来还想开一个数组来记录每一格的时间,觉得有点多余,广搜的话最
后一个节点本来就是耗时最长的,所以只需用一个timet变量来记录就好了,省时又省心。
#include<cstdio>#include<queue>#include<vector>#include<cstring>#include<algorithm>using namespace std;const int M = 15;const int INF = 99999999;char mapp[M][M];bool bo[M][M];struct Node{    int x, y, s;};vector<Node> gras;int ans, n, m, timet, len;//len用来存放gras.size()减少多次调用int mv[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};void bfs(int x1, int y1, int x2, int y2){    memset(bo, false, sizeof(bo));    queue<Node> q;    Node tmp, next;    int xt, yt;    tmp.x = x1;    tmp.y = y1;    tmp.s = 0;    bo[x1][y1] = bo[x2][y2] = true;    q.push(tmp);    tmp.x = x2;    tmp.y = y2;    tmp.s = 0;    q.push(tmp);    while(!q.empty())    {        tmp = q.front();        q.pop();        timet = tmp.s;        for(int i=0;i<4;i++)        {            xt = tmp.x + mv[i][0];            yt = tmp.y + mv[i][1];            if(xt<1||xt>n||yt<1||yt>m)                continue;            next.s = tmp.s + 1;            if(mapp[xt][yt]=='#'&&!bo[xt][yt])            {                next.x = xt;                next.y = yt;                bo[xt][yt] = true;                q.push(next);            }        }    }}bool check(){    for(int i=0;i<len;i++)        if(!bo[gras[i].x][gras[i].y])            return false;    return true;}int main(){    int t, cnt=1;    Node tmm;    scanf("%d", &t);    while(t--)    {        gras.clear();        ans = INF;        scanf("%d%d", &n, &m);        for(int i=1;i<=n;i++)            for(int j=1;j<=m;j++)            {                scanf(" %c", &mapp[i][j]);                if(mapp[i][j]=='#')                {                    tmm.x = i;                    tmm.y = j;                    gras.push_back(tmm);                }            }        len = gras.size();        for(int i=0;i<len;i++)        {            for(int j=i;j<len;j++)            {                timet = 0;                bfs(gras[i].x, gras[i].y, gras[j].x, gras[j].y);                if(check())                    ans = min(ans, timet);            }        }        printf("Case %d: ", cnt++);        if(ans==INF)            printf("-1\n");        else            printf("%d\n", ans);    }    return 0;}


原创粉丝点击