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;
    }
  • 相关阅读:
    若依(基于SpringBoot的权限管理系统)的快速搭建
    Android中怎样在项目中引入别的项目中正使用的library
    AndroidStudio中提示:uses-sdk:minSdkVersion 16 cannot be smaller than version 19 declared in libr
    AndroidStudio中提示:Failed to resolve: com.github.CymChad:BaseRecyclerViewAdapterHelper:
    mysql与mongoDB常用命令
    github、gitlab等常用api接口
    常用编译网址
    vue/react: 父组件中请求数据好?还是子组件中请求数据好?
    进阶《Python高性能编程》中文PDF+英文PDF+源代码
    【2021-03-19】人生十三信条
  • 原文地址:https://www.cnblogs.com/syhandll/p/4842062.html
Copyright © 2011-2022 走看看