zoukankan      html  css  js  c++  java
  • Fire Game 双向bfs

    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

    题意,从两个地方点火,看看烧完最短的时间是多少,火不能烧'.',只能烧'#',也就是遇到了句点,火焰停止蔓延;
    一开始想看看有没有啥规律,想判断如果连通分支超过两个就不行,然后分开讨论一个连通分支与两个连通分支的情况,可是规律不通用,于是选择了排着遍历,mint给一个很大的值,每次选两个地方进行点火,然后都入队,bfs,如果能把草全烧完,更新最小时间,最后如果最小时间还是原来很大的值,输出-1.
    但是忘记了还有只有一块草的情况,那就另外加了一下就过了。


    代码:
    #include <iostream>
    #include <cstdlib>
    #include <queue>
    #include <cstring>
    #include <cstdio>
    using namespace std;
    
    int dir[4][2]={0,1,1,0,0,-1,-1,0};
    int  T,n,m,x,y,t,vis[10][10],c;
    char mp[10][10];
    class que
    {
        public:
        int x,y,time;
    }temp;
    int xx[100],yy[100],xy,mint;
    int main()
    {
        cin>>T;
        for(int l=1;l<=T;l++)
        {
            mint=99999;//updated
            cin>>n>>m;
            queue<que>q;
            xy=0;
            for(int i=0;i<n;i++)
                for(int j=0;j<m;j++)
                {
                    cin>>mp[i][j];
                    if(mp[i][j]=='#')
                        xx[xy]=i,yy[xy]=j,xy++;
                }
            for(int i=0;i<xy;i++)
            {
                for(int j=i+1;j<xy;j++)
                {
                    memset(vis,0,sizeof(vis));//initialized
                    temp.x=xx[i],temp.y=yy[i],temp.time=0;
                    q.push(temp);
                    vis[temp.x][temp.y]=1;
                    temp.x=xx[j],temp.y=yy[j],temp.time=0;
                    q.push(temp);
                    vis[temp.x][temp.y]=1;
                    c=2;//updated
                    t=0;//updated
                    while(!q.empty())
                    {
                        if(t<q.front().time)t=q.front().time;
                        for(int k=0;k<4;k++)
                        {
                            x=q.front().x+dir[k][0];
                            y=q.front().y+dir[k][1];
                            if(x<0||y<0||x>=n||y>=m||mp[x][y]=='.'||vis[x][y])continue;
                            temp.x=x,temp.y=y,temp.time=q.front().time+1;
                            q.push(temp);
                            vis[x][y]=1;
                            c++;
                        }
                        q.pop();
                    }
                    if(c==xy){if(t<mint)mint=t;}//judged
                }
    
            }
            printf("Case %d: ",l);
            if(mint<99999)cout<<mint<<endl;
            else if(xy==1)cout<<0<<endl;//special
            else cout<<-1<<endl;
    
        }
    }
  • 相关阅读:
    关于 self 和 super 在oc 中 的疑惑 与 分析
    面向对象总结
    OC面向对象的三大特征
    Web jsp开发学习——Servlet提交表单时用法
    Web jsp开发学习——Servlet提交表单时用法
    Web jsp开发学习——点击菜单页面切换
    Web jsp开发学习——点击菜单页面切换
    珍藏的数据库SQL基础练习题答案
    珍藏的数据库SQL基础练习题答案
    数据库SQL语言学习--上机练习3(插入 更新 删除)
  • 原文地址:https://www.cnblogs.com/8023spz/p/7250994.html
Copyright © 2011-2022 走看看