zoukankan      html  css  js  c++  java
  • Fzu 2150 Fire Game

    Problem 2150 Fire Game

    Accept: 864    Submit: 3207
    Time Limit: 1000 mSec    Memory Limit : 32768 KB

     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
     
    DFS+BFS做的。代码写的有点挫。思路:先dfs分块,然后分为2种情况讨论 块数为1时枚举两个起火点求最少时间,块数为2时分别求各块的最少时间,在这两个最少时间里取最大的。
    /* ***********************************************
    Author        :pk29
    Created Time  :2015/8/18 16:50:09
    File Name     :4.cpp
    ************************************************ */
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <stdio.h>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    #include <iomanip>
    #include <list>
    #include <deque>
    #include <stack>
    #define ull unsigned long long
    #define ll long long
    #define mod 90001
    #define INF 0x3f3f3f3f
    #define maxn 10000+10
    #define cle(a) memset(a,0,sizeof(a))
    const ull inf = 1LL << 61;
    const double eps=1e-5;
    using namespace std;
    struct node{
        int x,y,dist;
    };
    
    bool cmp(int a,int b){
        return a>b;
    }
    char mp[20][20];
    int n,m,vis[20][20];
    int  dir[4][2]={1,0,0,1,0,-1,-1,0};
    int cnt;
    void dfs(int x,int y){
        if(cnt>2)return ;
        vis[x][y]=1;
        mp[x][y]=char(cnt+'0');
        for(int i=0;i<4;i++){
            int nx=x+dir[i][0];
            int ny=y+dir[i][1];
            if(!vis[nx][ny]&&nx<n&&ny<m&&nx>=0&&ny>=0&&mp[nx][ny]=='#'){
                vis[nx][ny]=1;
                dfs(nx,ny);
            }
        }
    }
    int bfs(node s,char c){
        int vist[15][15]={0};
        vist[s.x][s.y]=1;
        queue<node>q;
        q.push(s);
        node u,v;
        while(!q.empty()){
            u=q.front(),q.pop();
            for(int i=0;i<4;i++){
                v.x=u.x+dir[i][0];
                v.y=u.y+dir[i][1];
                if(v.x<n&&v.x>=0&&v.y<m&&v.y>=0&&mp[v.x][v.y]==c&&!vist[v.x][v.y]){
                    v.dist=u.dist+1;
                    q.push(v);
                    vist[v.x][v.y]=1;
                }
            }
            if(q.empty())return u.dist;
        }
    }
    int Bfs(node s1,node s2,char c){
        int vist[15][15]={0};
        vist[s1.x][s1.y]=1;
        vist[s2.x][s2.y]=1;
        queue<node>q;
        q.push(s1);
        q.push(s2);
        node u,v;
        while(!q.empty()){
            u=q.front(),q.pop();
            for(int i=0;i<4;i++){
                v.x=u.x+dir[i][0];
                v.y=u.y+dir[i][1];
                if(v.x<n&&v.x>=0&&v.y<m&&v.y>=0&&mp[v.x][v.y]==c&&!vist[v.x][v.y]){
                    v.dist=u.dist+1;
                    q.push(v);
                    vist[v.x][v.y]=1;
                }
            }
            if(q.empty())return u.dist;
        }
    }
    int main()
    {
        #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        #endif
        //freopen("out.txt","w",stdout);
        int t;
        cin>>t;
        int cas=0;
        while(t--){
            scanf("%d %d",&n,&m);
            for(int i=0;i<n;i++)
                scanf("%s",mp[i]);
            cnt=0;
            cle(vis);
            for(int i=0;i<n;i++)
                for(int j=0;j<m;j++){
                    if(!vis[i][j]&&mp[i][j]=='#'){
                        cnt++;
                        dfs(i,j);
                    }
                }
            if(cnt>2){
                    printf("Case %d: %d
    ",++cas,-1);continue;
            }
            else if(cnt==0){
                printf("Case %d: %d
    ",++cas,0);continue;
            }
            else if(cnt==1){
                cle(vis); int Min=INF;
                for(int i=0;i<n;i++)
                    for(int j=0;j<m;j++)
                        for(int k=0;k<n;k++)
                            for(int p=0;p<m;p++){
                                if(mp[i][j]=='1'&&mp[k][p]=='1'){
                                    node s1,s2;
                                    s1.x=i,s1.y=j,s1.dist=0;
                                    s2.x=k,s2.y=p,s2.dist=0;
                                    Min=min(Bfs(s1,s2,'1'),Min);
                                }
                            }
                printf("Case %d: %d
    ",++cas,Min);continue;
            }
            else{
                cle(vis);
                int Min1=INF,Min2=INF;
                for(int i=0;i<n;i++)
                    for(int j=0;j<m;j++){
                        if(mp[i][j]=='1'&&!vis[i][j]){
                            vis[i][j]=1;
                            node s;s.x=i,s.y=j,s.dist=0;
                            Min1=min(bfs(s,'1'),Min1);
                        }
                        else if(mp[i][j]=='2'&&!vis[i][j]){
                            vis[i][j]=1;
                            node s;s.x=i,s.y=j,s.dist=0;
                            Min2=min(bfs(s,'2'),Min2);
                        }
                    }
                printf("Case %d: %d
    ",++cas,max(Min1,Min2));continue;
            }
        }
        return 0;
    }
     
  • 相关阅读:
    一文搞定String、StringBuffer、StringBulider(含使用方法,三者对比)
    Linux C/C++编程之(十一)gdb调试
    Linux C/C++编程之(十)动态库的制作和使用
    《java入门第一季》之面向对象接口面试题
    《java入门第一季》之面向对象(谈谈接口)
    《java入门第一季》之面向对象(抽象类其实不抽象)
    《java入门第一季》之面向对象(抽象类到底多抽象)
    《java入门第一季》之面向对象多态面试题(多态收尾)
    《java入门第一季》之面向对象(多态练习)
    《java入门第一季》之面向对象(多态向下转型)
  • 原文地址:https://www.cnblogs.com/pk28/p/4741098.html
Copyright © 2011-2022 走看看