Fire Game(双起点BFS)

传送门FZU2150

描述

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.

输入

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

输出

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.

样例

  • Input

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    
    4
    3 3
    .#.
    ###
    .#.
    3 3
    .#.
    #.#
    .#.
    3 3
    ...
    #.#
    ...
    3 3
    ###
    ..#
    #.#
    
  • Output

    1
    2
    3
    4
    5
    
    6
    Case 1: 1
    Case 2: -1
    Case 3: 0
    Case 4: 2
    

题解

  • 题意:为了将草丛变成空地,现在需要找两个点放火,火会向上下左右蔓延,空地不会被点燃,每向四周蔓延一格需要1单位的时间,求最少需要多久可以将所有草烧完,无法烧完则输出-1
  • 双起点bfs,n方枚举两个起点放入队列,然后bfs即可
    记录路径

    Code

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    
    #include<iostream>
    #include<cstring>
    #include<queue>
    #define INIT(a,b) memset(a,b,sizeof(a))
    #define LL long long
    using namespace std;
    const int inf=0x3f3f3f3f;
    const int N=17;
    const int mod=1e9+7;
    int gra[N*N][2],tot=0,vis[N][N];
    int rec[4][2]={1,0,-1,0,0,1,0,-1};
    char mp[N][N];
    int n,m;
    struct node{
        int x,y,time;
    };
    queue<node> que;
    bool judge(int i,int j){
        if(i<0||i>=n||j<0||j>=m||mp[i][j]=='.'||vis[i][j])return false;
        return true;
    }
    bool can(){
        for(int i=0;i<tot;i++)
            if(!vis[gra[i][0]][gra[i][1]])
                return false;
        return true;
    }
    int bfs(){
        int res=-1;
        while(!que.empty()){
            node q=que.front();que.pop();
            res=max(res,q.time);
            for(int i=0;i<4;i++){
                int nx=q.x+rec[i][0],ny=q.y+rec[i][1];
                if(judge(nx,ny)){
                    que.push((node){nx,ny,q.time+1});
                    vis[nx][ny]=1;
                }
            }
        }
        if(can()) return res;
        else return -1;
    }
    int main(){
        ios::sync_with_stdio(false);
        cin.tie(0);
        int t;cin>>t;
        for(int k=1;k<=t;k++){
            cout<<"Case "<<k<<": ";
            tot=0;
            cin>>n>>m;
            for(int i=0;i<n;i++){
                for(int j=0;j<m;j++){
                    cin>>mp[i][j];
                    if(mp[i][j]=='#'){
                        gra[tot][0]=i;
                        gra[tot++][1]=j;
                    }
                }
            }
            if(tot<=2) {
                cout<<"0"<<endl;
                continue;
            }
            int ans=inf,tem;
            for(int i=0;i<tot;i++){
                for(int j=0;j<tot;j++){
                    INIT(vis,0);
                    vis[gra[i][0]][gra[i][1]]=1;
                    vis[gra[j][0]][gra[j][1]]=1;
                    que.push((node){gra[i][0],gra[i][1],0});
                    que.push((node){gra[j][0],gra[j][1],0});
                    tem=bfs();
                    if(tem!=-1) ans=min(tem,ans);
                }
            }
            if(ans==inf)cout<<"-1"<<endl;
            else cout<<ans<<endl;
        }
    
    	return 0;
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值