首页 > 其他分享 >Fire Game (FZU 2150)(BFS)

Fire Game (FZU 2150)(BFS)

时间:2023-02-14 13:06:48浏览次数:41  
标签:grass tx Fire BFS int Game grid now dis

题解:一开始想错了,以为只要烧完就是那个答案,但是这不是最优的结果,需要每两个点都bfs一遍,找到如果能够全部烧完,找到花费时间最小的,如果不能return -1。在bfs的时候,记录答案的方法参考了一下其他大神的思路,把能烧到地方都需要能够用个二维数组dis[ ]来标记烧到这个地方时所用的时间是多少。在经过一次两点的bfs后就需要找dis[ ]中最大的那个点,因为这一定是烧完的最后一个点。最后找能烧完的答案中最小的那个就可以了。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
#include<cstdio>
#include<queue>
using  namespace std;
const int INF = 0x3f3f3f3f;
char gra[20][20];
int dis[20][20];
int dx[4] = {1,-1,0,0};
int dy[4] = {0,0,1,-1};
struct node
{
    int x,y;
};
int T,n,m;
int bfs(int x1, int y1, int x2, int y2)
{
    struct node now, next;
    queue<node>q;
    int tx, ty;
    memset(dis,INF,sizeof(dis));
    now.x = x1;  now.y = y1;
    q.push(now);
    now.x = x2;  now.y = y2;
    q.push(now);
    dis[x1][y1] = dis[x2][y2] = 0;
    while(!q.empty())
    {
        now = q.front();
        q.pop();
        for(int i =0; i < 4; i ++)
        {
            tx = now.x + dx[i];
            ty = now.y + dy[i];
 if(tx >= 0 && tx < n && ty >= 0 && ty < m && gra[tx][ty] == '#' && dis[tx][ty] > dis[now.x][now.y] + 1)
            {
                dis[tx][ty] = dis[now.x][now.y] + 1;
                next.x = tx;
                next.y = ty;
                q.push(next);
            }
        }
    }
    int maxn = 0;
    for(int i = 0; i < n; i ++)
    {
        for(int j = 0; j < m; j ++)
        {
            if(gra[i][j] == '#')
            {
                maxn = max(maxn,dis[i][j]);
            }
        }
    }
    return maxn;
}
int main()
{
    cin >> T;
    for (int cas = 1; cas <= T; cas++)
    {

        cin >> n >> m;
        for (int i = 0; i < n; i++)
        {
            cin >> gra[i];
        }
        int ans = INF;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                if (gra[i][j] == '#')
                    for (int k = 0; k < n; k++)
                    {
                        for (int p = 0; p < m; p++)
                        {
                            if (gra[k][p] == '#')
                            {
                                int temp = bfs(i, j, k, p);
                                ans = min(ans, temp);
                            }
                        }
                    }
            }
        }
        if (ans == INF)
            ans = -1;
        cout << "Case " << cas << ": " << ans << endl;
    }
    return 0;
}

Problem

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

标签:grass,tx,Fire,BFS,int,Game,grid,now,dis
From: https://blog.51cto.com/u_15965659/6056604

相关文章

  • 使用精灵组对精灵成员编队 pygame 230213
    定义精灵成员定义了两个精灵成员说明:Background类是精灵类的子类定义精灵组精灵组添加精灵语法:精灵组.add(精灵成员)批量更新数据语法:精灵组.update()说明:目的是让所有的精灵......
  • C++ 修改防火墙firewall设置(Linux、Ubuntu、CentOS)
    1、简介1.1Ubuntuhttps://ubuntu.com/download/desktopUbuntu是一个以桌面应用为主的Linux操作系统,其名称来自非洲南部祖鲁语或豪萨语的“ubuntu"一词,意思是“人性”“......
  • Firewall常用命令
    1、firewalld的基本使用启动:systemctlstartfirewalld查看状态:systemctlstatusfirewalld停止:systemctldisablefirewalld禁用:systemctlstopfirewalld2.syst......
  • 多源bfs
    输入第一行两个整数n,m。接下来一个N行M列的01矩阵,数字之间没有空格。数据范围1≤N,M≤1000 输出一个N行M列的矩阵B,相邻两个整数之间用一个空格隔开。每个整数表示......
  • 2023Hgame
    2023HgameSharedDiary源代码先放一下constexpress=require('express');constbodyParser=require('body-parser');constsession=require('express-session')......
  • Hgame-2023-week3-Re
    Hgame2023week3Reverse1.kmusic首先点开.exe文件运行(如果没有安装.netruntime,那么他会提醒你先下载,也可以在这里手动下载)。打开是一个如下界面:点击会有对应......
  • ABC 272 D (BFS)
    ABC272D题意给定一个N*N的棋盘,棋子初始位置在(1,1),给定一个数M,棋子每步操作可以走到距离不超过M的位置,假设棋子在(i,j),则下一步(x,y)应满足(x-i)×(x-i)+(y-j)×(y-j)<=M思路这是加......
  • 按下空格发射子弹 pygame 230210
    逻辑捕捉用户按下空格的事件创建一个子弹对象在游戏循环中让子弹往上飞行定义子弹模板按下空格拷备子弹让子弹显示并飞......
  • linux 防火墙 firewalld
    1、查看firewalld服务状态systemctlstatusfirewalld出现Active:active(running)高亮显示则表示是启动状态。出现Active:inactive(dead)灰色表示停止,看单词也行。2、......
  • delphi FireDAC使用ApplyUpdates批量提交数据
    简单的设计一下界面,大致如下:要求:审核或者反审核用户选择的数据1.当记录已审核时,再点审核会提示出错,反审核也一样2.正常点审核时,审核=true,审核人=当前用......