zoukankan      html  css  js  c++  java
  • FZU 2150 Fire Game (bfs+dfs)

    Problem Description

    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
     
    题意:你可以从2个地方点火,问能否把所有的草烧光,在一个联通块里的草火会传递,如果可以烧掉所有的草输出最少的时间
    思路:我是先dfs确定连通块 然后再bfs判断是否可以全部烧完(其实这个方法很慢 需要600ms 其实直接bfs更快)
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    #include<string>
    #include<vector>
    #include<stack>
    #include<bitset>
    #include<cstdlib>
    #include<cmath>
    #include<set>
    #include<list>
    #include<deque>
    #include<map>
    #include<queue>
    #define ll long long int
    using namespace std;
    inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
    int moth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    int dir[4][2]={1,0 ,0,1 ,-1,0 ,0,-1};
    int dirs[8][2]={1,0 ,0,1 ,-1,0 ,0,-1, -1,-1 ,-1,1 ,1,-1 ,1,1};
    const int inf=0x3f3f3f3f;
    const ll mod=1e9+7;
    int n,m;
    char G[15][15];
    bool vis[15][15];
    int ans;
    int num;
    struct node{
        int x,y,step;
    };
    bool jug(){
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++){
                if(G[i][j]=='#'&&!vis[i][j])
                return false;
            }
        return true;
    }
    void bfs(int x,int y,int x2,int y2){
        queue<node > q;
        node t; t.step=0; t.x=x; t.y=y;
        node tt; tt.step=0; tt.x=x2; tt.y=y2;
        if(!vis[x][y])
        vis[x][y]=1,q.push(t);
        if(!vis[x2][y2])
        vis[x2][y2]=1,q.push(tt);
        while(!q.empty()){
            node temp=q.front();
            q.pop();
            ans=max(ans,temp.step);
            for(int i=0;i<4;i++){
                int xx=temp.x+dir[i][0];
                int yy=temp.y+dir[i][1];
                if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&G[xx][yy]=='#'&&!vis[xx][yy]){
                    vis[xx][yy]=1;
                    node te; te.x=xx; te.y=yy; te.step=temp.step+1;
                    q.push(te);
                }
            }
        }
    }
    void dfs(int x,int y){
        for(int i=0;i<4;i++){
            int xx=x+dir[i][0];
            int yy=y+dir[i][1];
            if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&!vis[xx][yy]&&G[xx][yy]=='#'){
                vis[xx][yy]=1;
                dfs(xx,yy);
            }
        }
    }
    int main(){
        ios::sync_with_stdio(false);
        int t;
        cin>>t;
        int w=1;
        while(t--){
            cin>>n>>m;
            num=0;
            pair<int,int> p[107];
            int sz=0;
            for(int i=1;i<=n;i++)
                for(int j=1;j<=m;j++){
                    cin>>G[i][j];
                    if(G[i][j]=='#'){
                        sz++;
                        p[sz].first=i;
                        p[sz].second=j;
                    }
                }
            memset(vis,0,sizeof(vis));
            for(int i=1;i<=n;i++)
                for(int j=1;j<=m;j++)
                    if(!vis[i][j]&&G[i][j]=='#'){
                        num++;
                        vis[i][j]=1;
                        dfs(i,j);
                    }
            if(num<=2){
                int a=inf;
                for(int i=1;i<=sz;i++)
                    for(int j=1;j<=sz;j++){
                        memset(vis,0,sizeof(vis));
                        ans=0;
                        bfs(p[i].first,p[i].second,p[j].first,p[j].second);
                        if(jug())
                        a=min(a,ans);
                    }
                cout<<"Case "<<w++<<": "<<a<<endl;
            }else cout<<"Case "<<w++<<": -1"<<endl;
        }
        return 0;
    }
  • 相关阅读:
    大杂烩 -- 查找单向链表倒数第m个元素
    大杂烩 -- 单向链表是否存在环或是否相交
    大杂烩 -- 四种生成和解析XML文档的方法详解
    延伸 -- 泛型 -- 通配符的使用
    延伸 -- 泛型 -- 泛型的内部原理:类型擦除以及类型擦除带来的问题
    延伸 -- 泛型 -- 泛型的基本介绍和使用
    大杂烩 -- HashMap、HashTable、ConCurrentHashMap 联系与区别
    大杂烩 -- ArrayList的动态增长 源码分析
    Java -- 异常的捕获及处理 -- 自定义异常类
    Java字符串占位符(commons-text)替换(转载)
  • 原文地址:https://www.cnblogs.com/wmj6/p/10461982.html
Copyright © 2011-2022 走看看