FZU 2150 Fire Game(双向BFS)

来源:互联网 发布:求数组最大最小值 编辑:程序博客网 时间:2024/05/16 15:59

J - Fire Game

 FZU - 2150 

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


// 训练赛时在这道题卡了好久, 看到时有预感是暴力搜, 但实在没想到居然这么暴力!
// 大体题意就是给你一张图,'#' 代表草, 让你放两把火能让火顺着草烧。 如果能烧完输出烧完最短时间, 否则输出-1
// 思路很简单粗暴, 就是任意枚举两点进行bfs(放火), 如果能烧完返回时间, 烧不完返回一个无穷大。 这里用
到的一个是同时在两点bfs(放火), 算是新学了一招。
#include <cstdio>#include <cstring>#include <queue>#include <cmath>#include <algorithm>#define MAX 9999using namespace std;typedef struct node{    int x;    int y;//存坐标    int time;//存烧到这点的耗时} Node;Node Grid[110];char Map[12][12];//存图int n, m;int l;// 存草地数, 以后会用到, 算是为暴力减枝int vis[12][12];//标记该点是否被访问过, 由于某种不可描述原因这里 -1 代表未访问,0代表已访问, 可能有点别扭int bfs(int i, int y);// 同时bfs Grid[i] , Grid[j]两点int i_way[] = {-1, 1, 0, 0};int j_way[]= {0, 0, -1, 1};int main(){    int t;    int Case = 0;    scanf("%d", &t);    while(t--)    {        memset(Map, 0, sizeof(Map));        scanf("%d%d", &n, &m);        getchar();        l =  0;        for(int i = 1; i <= n; i++)        {            for(int j = 1; j <= m; j++)            {                scanf("%c", &Map[i][j]);                if(Map[i][j] == '#')                {                    Grid[l].x = i;                    Grid[l++].y = j;//用结构体记下所有草地数目, 减少暴力时重复                }            }            getchar();        }        int Min = MAX;//初始为无限大        for(int i = 0; i < l; i++)        {            for(int j = i; j < l; j++)            {                memset(vis, -1, sizeof(vis));                Min = min(bfs(i, j), Min);//暴力(放火)开始!            }        }        if(Min == MAX)//两把火烧不完草地            Min = -1;        printf("Case %d: %d\n", ++Case, Min);    }    return 0;}int bfs(int u, int v){    queue <Node> q;    vis[Grid[u].x][Grid[u].y] = 0;// 标记已被访问    Grid[u].time = 0;//放火那一刻时间为0    q.push(Grid[u]);//入队    if(u != v)//减重    {        vis[Grid[v].x][Grid[v].y] = 0;        Grid[v].time = 0;        q.push(Grid[v]);    }    int ans = -1;    int cou = 0;//记录已烧草地数量    while(!q.empty())    {        Node tmp = q.front();//出队        q.pop();        cou++;        int flag = 1;//标记是否烧到尽头        for(int i = 0; i < 4; i++)// 上下左右走        {            int nx = tmp.x + i_way[i];            int ny = tmp.y + j_way[i];            if(Map[nx][ny] == '#' && vis[nx][ny] == -1)//很巧妙的一份地方,详见输入部分,可以减少很多判断            {                flag = 0;                Node next = {nx, ny, tmp.time + 1};                vis[nx][ny] = 0;                q.push(next);            }        }        if(flag)//一条路走到尽头        {            ans = max(ans, tmp.time);//完全烧完时间为所有路烧完最长时间        }    }    if(cou < l)//判断是否把所有草地烧完        ans = MAX;    return ans;}// 虽然是暴力搜索, 但是时间在340ms左右, 如果手动建一个队列的话会快好几倍。 祺神的代码能到90+ms, 可是说很快了
原创粉丝点击