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

     Fire Game
    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    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.

     

    题意: 从两个任意点点着草地 火会向上下左右四个方向蔓延

             相邻的草地就会被点着

             问最短多长时间点燃所有草地

    思路:咋一看要同时完成两个点很麻烦

            其实就是枚举两个点同时进队列进行bfs

            其中要注意 草地小于等于二时 要特殊处理

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<queue>
    #include<cstring>
    #include<stdlib.h>
    #include<algorithm>
    using namespace std;
    char a[15][15];
    int vis[15][15];
    int wis[15][15][15][15];
    int ans,sum,starx,stary,starxx,staryy,n,m,mark,all;
    int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
    const int INF=0x3f3f3f3f;
    
    struct node
    {
        int x,y;
        int step;
    };
    node p,q;
    
    int panduan(int i,int j)
    {
        if(0<=i&&i<n && 0<=j&&j<m && a[i][j]=='#')
            return 1;
        return 0;
    }
    
    void BFS()
    {
        memset(vis,0,sizeof(vis));
        queue <node> Q;
        sum=-2;
        vis[starx][stary]=1;
        vis[starxx][staryy]=1;
    
        p.x=starx;
        p.y=stary;
        p.step=0;
    
        q.x=starxx;
        q.y=staryy;
        q.step=0;
    
        Q.push(p);
        Q.push(q);
    
        while(!Q.empty())
        {
            node now,next;
            now=Q.front();
            Q.pop();
    
            for(int i=0;i<4;i++)
            {
                next.x=now.x+dir[i][0];
                next.y=now.y+dir[i][1];
                if(panduan(next.x,next.y) && !vis[next.x][next.y])
                {
                    vis[next.x][next.y]=1;
                    next.step=now.step+1;
                    all++;
                    Q.push(next);
                }
            }
            sum=max(sum,now.step);
        }
    }
    int main()
    {    
        int kase,cnt=0;
        scanf("%d",&kase);
        while(kase--)
        {
            memset(wis,0,sizeof(wis));
            ans=INF,mark=0;
            scanf("%d %d",&n,&m);
            for(int i=0;i<n;i++)
            {
                scanf("%s",a[i]);
                for(int j=0;j<m;j++)
                    if(a[i][j]=='#')
                        mark++;
                getchar();
            }  
            if(mark<=2)
            {
                printf("Case %d: 0
    ",++cnt);
                continue;
            }
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<m;j++)
                {
    
                    if(a[i][j]!='#') continue;
                    for(int ii=0;ii<n;ii++)
                    {
                        for(int jj=0;jj<m;jj++)
                        {
                            if(panduan(i,j) && panduan(ii,jj) && (i!=ii || j!=jj) )
                            {
                                if(wis[i][j][ii][jj]==1) continue;
                                wis[i][j][ii][jj]=wis[ii][jj][i][j]=1;
                                starx=i;
                                stary=j;
                                starxx=ii;
                                staryy=jj;
                                all=2;
                                BFS();
                                if(ans>sum && mark==all)
                                    {
                                        ans=sum;
                                    }
                            }
                        }
                    }
                }
            }
            if(ans==INF)
                printf("Case %d: -1
    ",++cnt);
            else
                printf("Case %d: %d
    ",++cnt,ans);
        }
        return 0;
    }
    

      

     

  • 相关阅读:
    python基础
    python中自定义的栈
    python内置函数
    python函数之可迭代对象、迭代器的判断
    关系型数据库
    数据库基础知识
    进程间通信--管道
    共享内存应用范例
    Win7秘籍 如何用压缩卷调整不合理分区
    KL-divergence
  • 原文地址:https://www.cnblogs.com/sola1994/p/3932569.html
Copyright © 2011-2022 走看看