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

    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
     
    题意:有一块草地,两个人做游戏,选两个点进行燃草,问最少需要多久才能将所有的草点燃,点燃草时是每一步蔓延到上下左右,选的那个点时间为0(两个点可以相同)。
    #include<stdio.h>
    #include<string.h>
    #include<queue>
    #include<algorithm>
    using namespace std;
    
    const int N=20;
    const int INF=0x3f3f3f3f;
    
    struct node
    {
        int x, y, s;
    }no[110];
    char Map[N][N];
    int m, n, vis[N][N];
    int dir[4][2] = { {1,0}, {-1,0}, {0,1}, {0,-1} };
    
    int BFS(node no1, node no2) ///计算从这两点开始点燃草的时间
    {
        int i, x, y;
        queue<node>Q;
        node now, next;
    
        Q.push(no1);
        Q.push(no2);
        vis[no1.x][no1.y] = vis[no2.x][no2.y] = 1;
    
        while (!Q.empty())
        {
            now = Q.front(); Q.pop();
    
            for (i = 0; i < 4; i++)
            {
                next.x = x = now.x + dir[i][0];
                next.y = y = now.y + dir[i][1];
    
                if (x >= 0 && x < m && y >= 0 && y < n && Map[x][y] == '#' && !vis[x][y])
                {
                    next.s = now.s + 1;
                    vis[x][y] = 1;
                    Q.push(next);
                }
            }
        }
    
        return now.s;
    }
    
    int Judge() ///判断是否点燃了所有的草
    {
        int i, j;
    
        for (i = 0; i < m; i++)
        {
            for (j = 0; j < n; j++)
            {
                if (!vis[i][j] && Map[i][j] == '#')
                    return 0;
            }
        }
    
        return 1;
    }
    
    int main ()
    {
        int T, i, j, ans, cnt, kk = 0, k;
    
        scanf("%d", &T);
    
        while (T--)
        {
            scanf("%d%d", &m, &n);
            for (i = 0; i < m; i++)
                scanf("%s", Map[i]);
    
            k = 0;
            kk++;
            ans = INF;
    
            for (i = 0; i < m; i++)
            {
                for (j = 0; j < n; j++)
                {
                    if (Map[i][j] == '#') ///先将草地保存下来
                    {
                        no[k].x = i;
                        no[k].y = j;
                        no[k++].s = 0;
                    }
                }
            }
    
            for (i = 0; i < k; i++) ///在草地中任选两点进行BFS查找点燃所有的草需要多久
            {
                for (j = i; j < k; j++) ///这里j从i开始,这样不会重复查找,避免超时
                {
                    memset(vis, 0, sizeof(vis)); ///注意每次查找前都需要初始化
                    cnt = BFS(no[i], no[j]);
                    if (cnt < ans && Judge()) ///如果能点燃所有的草并且时间更短,更新结果
                        ans = cnt;
                }
            }
    
            if (ans == INF) printf("Case %d: -1
    ", kk);
            else printf("Case %d: %d
    ", kk, ans);
        }
    
        return 0;
    }
  • 相关阅读:
    maven 的依赖包的版本更改之后,项目启动报错,相关联的资源没有在tomcat里面加载
    Maven更新后本地仓库jar后缀带有 lastUpdated
    spring+mybatis之声明式事务管理初识(小实例)
    java 详解类加载器的双亲委派及打破双亲委派
    jvm内存溢出区域和排查方法
    JVM中的新生代和老年代(Eden空间、两个Survior空间)
    什么情况下会发生堆内存溢出,栈内存溢出,结合实例说明
    投而死面试
    检查性异常
    多线程系列课程
  • 原文地址:https://www.cnblogs.com/syhandll/p/4842062.html
Copyright © 2011-2022 走看看