Fire Game(不一样的bfs+暴力枚举)

本文介绍了一种在N*M棋盘上进行的特殊游戏,玩家需选择两个草地格子开始点燃,目标是使所有草地格子被火烧尽,以便进行更特殊的后续游戏。文章详细描述了游戏规则,包括火势蔓延的条件和过程,以及如何通过暴力枚举和广度优先搜索算法来找到最短的时间内完成游戏的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

代码:

///数据量比较小,所以暴力+枚举就行。。。。。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<queue>
#define INF 0x3f3f3f3f
using namespace std;
struct node
{
    int x;
    int y;
    int step;
} p[225];
int n,m;
char mp[15][15];
bool vis[15][15];
int walk[][2]= {{-1,0},{0,1},{1,0},{0,-1}};///走的方向 
int judge()
{
    int i,j;
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            if(mp[i][j]=='#'&&vis[i][j]==0)
                return 0;
        }
    }
    return 1;
}
int bfs(struct node a,struct node b)
{
    queue<struct node>q;
    q.push(a);///和普通的bfs不同的是这里上来就会压入2个数据。
    q.push(b);
    vis[a.x][a.y] = 1;
    vis[b.x][b.y] = 1;
    int mx = 0;
    while(!q.empty())
    {
        a = q.front();
        q.pop();
        for(int i=0; i<4; i++)
        {
            b.x = a.x + walk[i][0];
            b.y = a.y + walk[i][1];
            if(b.x>=0&&b.x<n&&b.y>=0&&b.y<m&&vis[b.x][b.y]==0&&mp[b.x][b.y]=='#')
            {
                vis[b.x][b.y] = 1;
                b.step = a.step + 1;
                if(mx<b.step)
                    mx = b.step;
                q.push(b);
            }
        }
    }
    return mx;///返回最大值。
}
int main()
{
    int T;
    int t = 0;
    scanf("%d",&T);
    while(T--)
    {
        t++;
        scanf("%d %d",&n,&m);
        int cnt = 0;
        for(int i=0; i<n; i++)
        {
            scanf("%s",mp[i]);
            for(int j=0; j<m; j++)
            {
                if(mp[i][j]=='#')///把每个点都初始化。用于后面的暴力。
                {
                    p[cnt].x = i;
                    p[cnt].y = j;
                    p[cnt].step = 0;
                    cnt++;///记录草坪的数量。可以对下面进行优化。
                }
            }
        }
        int step;
        int sum = INF;
        for(int i=0; i<cnt; i++)
        {
            for(int j=i; j<cnt; j++)
            {
                memset(vis,0,sizeof(vis));///因为是暴力,所以每一次都要重新更新。
                step = bfs(p[i],p[j]);///返回最大值。(两步的)
                int temp = judge();///判断一下。是否全部点燃草坪。没有的话就不会更新sum。(这里很关键)
                if(step<sum&&temp)///sum取最小值。
                    sum = step;///也就是说,如果更新的话,这个矩阵最多有两块草坪。(如果有两块草坪,并且分别在两块草坪上点火,那么就会燃烧完,只不过就是时间长短的问题,所以上面函数的mx就是返回每一次暴力的最大值(这里是最大值,不用说也知道,除非你没有读懂题。),所以把每一次记录的最大值进行比较,找到最小的,就是最优的。)
            }
        }
        if(sum==INF)
            printf("Case %d: -1\n",t);
        else
            printf("Case %d: %d\n",t,sum);
    }
    return 0;
}
 

### C++ 中使用 BFS 和 For 循环替代 DFS 的实现 在解决网格类问题时,广度优先搜索 (BFS) 可作为深度优先搜索 (DFS) 的有效替代方案。对于岛屿等问题而言,可以利用队列来管理待访问的位置,并通过 `for` 循环迭代处理这些位置。 #### 岛屿数量问题的 BFS 解决方案 下面是一个基于 BFS 来计算岛屿数量的例子: ```cpp #include <vector> #include <queue> using namespace std; class Solution { public: int numIslands(vector<vector<char>>& grid) { if (grid.empty() || grid[0].empty()) return 0; int rows = grid.size(); int cols = grid[0].size(); int islands = 0; vector<pair<int, int>> directions{{0,-1},{-1,0},{0,1},{1,0}}; for (int r = 0; r < rows; ++r){ for (int c = 0; c < cols; ++c){ if (grid[r][c] == '1'){ ++islands; queue<pair<int, int>> q; q.push({r,c}); while (!q.empty()){ auto [row, col] = q.front(); q.pop(); // 如果当前位置已经被标记,则跳过 if(grid[row][col]=='0') continue; // 将当前陆地标记为已访问 grid[row][col]='0'; // 对四个方向上的相邻节点进行探索 for(auto& dir : directions){ int newRow = row + dir.first; int newCol = col + dir.second; // 检查边界条件以及是否是未访问过的土地 if(newRow >= 0 && newRow < rows && newCol >= 0 && newCol < cols && grid[newRow][newCol] == '1') q.push({newRow,newCol}); } } } } } return islands; } }; ``` 此代码片段展示了如何使用 BFS 方法遍历整个地图并统计岛屿的数量。每当遇到一个新的岛屿部分(即值为 `'1'`),就启动一次新的 BFS 查找过程直到该岛完全被淹没为止。在此过程中,所有属于同一座岛屿的部分都会被设置成水 (`'0'`) 以防止重复计数[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值