FZU 1250 BFS(两起点)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Tianweidadada/article/details/81976374

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

扫描二维码关注公众号,回复: 3317100 查看本文章
Case 1: 1
Case 2: -1
Case 3: 0
Case 4: 2

题目大意:从两个点放火,火可以上下左右蔓延,问最小需要的时间(蔓延一个需要一个时间),最少有一个地方是grass

除了grass外为空地。

1 A  happy

#include<iostream>
#include<stdio.h>
#include<vector>
#include<map>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<set>
#include<cmath>
#include<queue>
using namespace std;
typedef pair<int,int> P;

int dir[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};
int n,m;
bool vis[13][13];
int step[13][13];
char a[11][11];

bool isOk(){
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < m; ++j){
            if(a[i][j] == '#' && !vis[i][j])
                return false;
        }
    }
    return true;
}

bool check(int x, int y){
    if(x < 0 || x >= n || y < 0 || y >= m)
        return false;
    if(vis[x][y])
        return false;
    return a[x][y] == '#';
}


int bfs(P a, P b){
    memset(vis,false,sizeof(vis));
    memset(step,0,sizeof(step));
    queue<P> q;
    q.push(a);
    vis[a.first][a.second] = true;
    q.push(b);
    vis[b.first][b.second] = true;
    step[a.first][a.second] = step[b.first][b.second] = 0;
    while(!q.empty()){
        P cur = q.front();
        q.pop();
        if(isOk()){
            // 取 grass fired 最长的那个时间
            int ans = -2;
            for(int i = 0; i < n; ++i){
                for(int j = 0; j < m; ++j){
                    ans = max(ans,step[i][j]);
                }
            }
            return ans;
        }
        for(int i = 0; i < 4; ++i){
            int nx = cur.first + dir[i][0];
            int ny = cur.second + dir[i][1];
            if(check(nx,ny)){
                step[nx][ny] = step[cur.first][cur.second] + 1;
                q.push(P(nx,ny));
                vis[nx][ny] = true;
            }
        }

    }
    return -1;
}

int main()
    {
        ios::sync_with_stdio(false);
        int t;
        int mmin;
        int kcase = 0;
        cin >> t;
        while(t--){
            cin >> n >> m;
            for(int i = 0; i < n; ++i)
                cin >> a[i];
            vector<int> p;
            int cnt = 0;
            // 这里 对坐标进行了转换 以单个数字表示 (x,y) 其实也可以 四层循环 
            for(int i = 0; i < n; ++i){
                for(int j = 0; j < m; ++j){
                    if(a[i][j] == '#'){
                          p.push_back(cnt);
                    }
                    ++cnt;
                }
            }
            // 一颗草时候 需要单独判断
            if(p.size() == 1){
                cout << "Case " << ++kcase << ": " << 0 << endl;
                continue;
            }
            // 记录是否 成功
            bool flag = false;
            int mmin = 1 << 30;
            for(int i = 0; i < p.size(); ++i){
                for(int j = i+1; j < p.size(); ++j){
                    //得到真实坐标
                    P p1 = P(p[i]/m,p[i]%m);
                    P p2 = P(p[j]/m,p[j]%m);
                    // 以两个点作为初始点 进行 搜索
                    int ans = bfs(p1,p2);
                    if(ans >= 0 && mmin > ans){
                        mmin = ans;
                        flag = true;
                    }
                }
            }
            if(flag){
                cout << "Case " << ++kcase << ": " << mmin << endl;
            }
            else{
                cout << "Case " << ++kcase << ": " << -1 << endl;
            }
        }


        return 0;
    }

猜你喜欢

转载自blog.csdn.net/Tianweidadada/article/details/81976374