zoukankan      html  css  js  c++  java
  • FZU 2150 Fire Game (高姿势bfs--两个起点)(路径不重叠:一个队列同时跑)

    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

    给你块地,有空地,也有草堆,让你选两个草堆进行点火,燃烧的草堆会引燃上下左右的相邻草堆,每一次引燃花费1s时间,问你最少花多长时间把草堆都点着,如果做不到输出-1.
    这个题一开始姿势不对,想错了,先bfs下找连通块,如果连通块个数大于3直接GG,否则再在已知连通块内求个深度........283行代码直接挂掉了。
    然而正确思路是酱紫的:及时有只一个连通块,我们也可以选择两个点火点来减少时间。
    所以直接暴力枚举每两个草堆,把这两个点加入bfs队列,两起点bfs,看看此时能点多少点多少的bfs的时间(就是q中最后一个被pop出的元素的depth),再判断下选这两个点是否能把草堆全点着。
    PS:这题最后半小时不过是因为没有初始化,以后养成好习惯每次都在每个测例运行前加一行init()......
    代码如下:
    #include<stdio.h>
    #include<string.h>
    #include<queue>
    using namespace std;
    
    char a[15][15];
    int b[15][15];
    int x[105],y[105];
    int to[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
    
    struct Node{
        int x,y,s;
    }node;
    
    int main()
    {
        int t,tt,n,m,c1,c2,cc,ans,i,j;
        queue<Node> q;
        scanf("%d",&t);
        tt=t;
        while(t--){
            scanf("%d%d",&n,&m);
            c1=0;cc=0;ans=10000;
            memset(x,0,sizeof(x));
            memset(y,0,sizeof(y));
            for(i=0;i<n;i++){
                getchar();
                scanf("%s",a[i]);
                for(j=0;j<m;j++){
                    if(a[i][j]=='#'){
                        c1++;
                        x[++cc]=i;
                        y[cc]=j;
                    }
                }
            }
            for(int ii=1;ii<=cc;ii++){
                for(int jj=1;jj<=cc;jj++){
                    memset(b,0,sizeof(b));
                    node.x=x[ii];
                    node.y=y[ii];
                    b[node.x][node.y]=1;
                    node.s=0;
                    q.push(node);
                    node.x=x[jj];
                    node.y=y[jj];
                    b[node.x][node.y]=1;
                    node.s=0;
                    q.push(node);
                    if(ii==jj) c2=1;
                    else c2=2;
                    while(q.size()){
                        for(i=0;i<4;i++){
                            int tx=q.front().x+to[i][0];
                            int ty=q.front().y+to[i][1];
                            if(tx<0||ty<0||tx>=n||ty>=m) continue;
                            if(a[tx][ty]=='#'&&b[tx][ty]==0){
                                c2++;
                                b[tx][ty]=1;
                                node.x=tx;
                                node.y=ty;
                                node.s=q.front().s+1;
                                q.push(node);
                            }
                        }
                        q.pop();
                    }
                    if(c1==c2){
                        if(node.s<ans) ans=node.s;
                    }
                }
            }
            if(ans==10000) ans=-1;
            printf("Case %d: %d
    ",tt-t,ans);
        }
        return 0;
    }
  • 相关阅读:
    八皇后 c++
    筛法求素数
    3月13号周练——2015 Multi-University Training Contest 9
    Mac搭建Git服务器—开启SSH
    push自定义动画
    学习:二维码、QR码、J4L-QRCode、java
    Java注解Annotation详解
    IOS 基于APNS消息推送原理与实现(JAVA后台)
    IOS学习笔记—苹果推送机制APNs
    linux yum命令详解
  • 原文地址:https://www.cnblogs.com/yzm10/p/7244395.html
Copyright © 2011-2022 走看看