首页 > 试题广场 >

小红的扫雷游戏

[编程题]小红的扫雷游戏
  • 热度指数:1422 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
小红最近爱上了扫雷游戏。
所谓扫雷游戏的规则是这样:给定一个n*m的矩阵,每个位置有可能是地雷。如果点击了一个地雷,那么直接被炸死,游戏结束。如果点击的一个位置不是地雷,那么将显示该格子周围 8 个格子中地雷数量的总和。
“周围 8 个格子”的定义:对于一个点 (x,y),其周围 8 个格子的坐标为 ,其中
现在小红拿到了一个 4*4 的矩阵,其中有一些位置已经被翻开。小红想知道,根据现有的信息,有哪些位置一定是雷,有哪些位置一定不是雷?小红希望你能把地图标记上。

输入描述:
输入4行,每行是一个长度为 4 的字符串。
其中字符 '.' 代表该位置的情况未知。
字符是数字代表该位置不是雷,且表示周围点的雷数量。
保证给定的地图一定合法。


输出描述:
对于一个确定的格子,如果是雷则标记为 'X',不是雷则标记为 'O'。
不确定的格子保留字符 '.',已经确定的数字也按数字输出。
示例1

输入

.121
....
....
....

输出

O121
OXOX
....
....

说明

判断依据如下:
根据(0,1)坐标的1,得知它周围4个只有一个雷。然后结合(0,2)坐标的2,得知(1,3)坐标一定是雷。
同理可得出(1,1)坐标一定是雷。根据两个已知的雷就可以判断出前两行其他均不是雷。
示例2

输入

....
..8.
....
....

输出

.XXX
.X8X
.XXX
....

说明

判断依据如下:
由于(1,2)坐标是8,那么该点周围有8个雷,且一共只有8个节点。所以这8个节点一定全部都是雷。
喵~
#define 喵 (
#define 呜 )
#define 喵喵 ;
#define 呜喵 i
#define 喵呜 j
#define 呜呜 [
#define 喵喵喵 ]
#define 呜喵喵 0
#define 喵呜喵 =
#define 呜呜喵 int
#define 喵喵呜 <
#define 呜喵呜 ,
#define 喵呜呜 v
#define 呜呜呜 4
#define 喵喵喵喵 b
#define 呜喵喵喵 for
#define 喵呜喵喵 {
#define 呜呜喵喵 }
#define 喵喵呜喵 ++
#define 呜喵呜喵 1
#define 喵呜呜喵 &&
#define 呜呜呜喵 if
#define 喵喵喵呜 k
#define 呜喵喵呜 :
#define 喵呜喵呜 <=
#define 呜呜喵呜 >=
#define 喵喵呜呜 check
#define 呜喵呜呜 di
#define 喵呜呜呜 dj
#define 呜呜呜呜 is1
#define 喵喵喵喵喵 ||
#define 呜喵喵喵喵 '0'
#define 喵呜喵喵喵 '9'
#define 呜呜喵喵喵 <<
#define 喵喵呜喵喵 >
#define 呜喵呜喵喵 ?
#define 喵呜呜喵喵 bool
#define 呜呜呜喵喵 cnt
#define 喵喵喵呜喵 is0
#define 呜喵喵呜喵 putchar
#define 喵呜喵呜喵 st
#define 呜呜喵呜喵 vector
#define 喵喵呜呜喵 !=
#define 呜喵呜呜喵 '.'
#define 喵呜呜呜喵 +
#define 呜呜呜呜喵 =-
#define 喵喵喵喵呜 break
#define 呜喵喵喵呜 char
#define 喵呜喵喵呜 return
#define 呜呜喵喵呜 &
#define 喵喵呜喵呜 'O'
#define 呜喵呜喵呜 'X'
#define 喵呜呜喵呜 '\n'
#define 呜呜呜喵呜 +=
#define 喵喵喵呜呜 .
#define 呜喵喵呜呜 16
#define 喵呜喵呜呜 2
#define 呜呜喵呜呜 48
#define 喵喵呜呜呜 >>
#define 呜喵呜呜呜 ^
#define 喵呜呜呜呜 auto
#define 呜呜呜呜呜 continue
#define 喵喵喵喵喵喵 else
#define 呜喵喵喵喵喵 getchar
#define 喵呜喵喵喵喵 insert
#define 呜呜喵喵喵喵 main
#define 喵喵呜喵喵喵 namespace
#define 呜喵呜喵喵喵 set
#define 喵呜呜喵喵喵 std
#define 呜呜呜喵喵喵 using
#define 喵喵喵呜喵喵 while
#define 呜喵喵呜喵喵 |
#include<bits/stdc++.h>
呜呜呜喵喵喵 喵喵呜喵喵喵 喵呜呜喵喵喵 喵喵
喵呜呜喵喵 喵喵呜呜 喵 呜呜喵 喵喵喵喵 呜喵呜 呜呜喵 呜喵 呜喵呜 呜呜喵 喵呜 呜 喵呜喵喵
    喵呜喵喵呜 喵 呜喵 喵喵呜 呜喵喵 喵喵喵喵喵 呜喵 呜呜喵呜 呜呜呜 喵喵喵喵喵 喵呜 喵喵呜 呜喵喵 喵喵喵喵喵 喵呜 呜呜喵呜 呜呜呜 呜 呜喵呜喵喵
            呜喵喵 呜喵喵呜
            喵 喵喵喵喵 呜呜喵喵呜 喵 呜喵呜喵 呜呜喵喵喵 喵 喵 呜喵 呜呜喵喵喵 喵呜喵呜呜 呜 呜喵喵呜喵喵 喵呜 呜 呜 呜 喵喵
呜呜喵喵
呜呜喵 呜呜喵喵喵喵 喵 呜 喵呜喵喵
    呜呜喵呜喵 喵喵呜 呜呜喵呜喵 喵喵呜 呜喵喵喵呜 喵喵呜呜呜 喵呜呜 喵 呜呜呜 呜喵呜 呜呜喵呜喵 喵喵呜 呜喵喵喵呜 喵喵呜喵喵 喵 呜呜呜 呜喵呜 呜喵喵 呜 呜 喵喵
    呜喵喵喵 喵 呜呜喵 呜喵 喵呜喵 呜喵喵 喵喵 呜喵 喵喵呜 呜呜呜 喵喵 呜喵 喵喵呜喵 呜
        呜喵喵喵 喵 呜呜喵 喵呜 喵呜喵 呜喵喵 喵喵 喵呜 喵喵呜 呜呜呜 喵喵 喵呜 喵喵呜喵 呜
            喵喵喵呜喵喵 喵 喵 喵呜呜 呜呜 呜喵 喵喵喵 呜呜 喵呜 喵喵喵 喵喵呜 呜喵喵喵喵 喵喵喵喵喵 喵呜呜 呜呜 呜喵 喵喵喵 呜呜 喵呜 喵喵喵 喵喵呜喵喵 喵呜喵喵喵 呜 喵呜呜喵 喵呜呜 呜呜 呜喵 喵喵喵 呜呜 喵呜 喵喵喵 喵喵呜呜喵 呜喵呜呜喵 呜
                喵呜呜 呜呜 呜喵 喵喵喵 呜呜 喵呜 喵喵喵 喵呜喵 呜喵喵喵喵喵 喵 呜 喵喵
    呜喵呜喵喵喵 喵喵呜 呜呜喵 喵喵呜喵喵 喵呜喵呜喵 喵喵
    呜喵喵喵 喵 呜呜喵 喵喵喵喵 喵呜喵 呜喵喵 喵喵 喵喵喵喵 喵喵呜 喵 呜喵呜喵 呜呜喵喵喵 呜喵喵呜呜 呜 喵喵 喵喵喵喵 喵喵呜喵 呜 喵呜喵喵
        喵呜呜喵喵 喵喵喵呜 喵呜喵 呜喵呜喵 喵喵
        呜喵喵喵 喵 呜呜喵 呜喵 喵呜喵 呜喵喵 喵喵 呜喵 喵喵呜 呜呜呜 喵呜呜喵 喵喵喵呜 喵喵 呜喵 喵喵呜喵 呜
            呜喵喵喵 喵 呜呜喵 喵呜 喵呜喵 呜喵喵 喵喵 喵呜 喵喵呜 呜呜呜 喵呜呜喵 喵喵喵呜 喵喵 喵呜 喵喵呜喵 呜
                呜呜呜喵 喵 喵呜呜 呜呜 呜喵 喵喵喵 呜呜 喵呜 喵喵喵 呜呜喵呜 呜喵喵喵喵 喵呜呜喵 喵呜呜 呜呜 呜喵 喵喵喵 呜呜 喵呜 喵喵喵 喵呜喵呜 喵呜喵喵喵 呜 喵呜喵喵
                    呜呜呜喵 喵 喵喵呜呜 喵 喵喵喵喵 呜喵呜 呜喵 呜喵呜 喵呜 呜 呜 喵呜喵喵
                        喵喵喵呜 喵呜喵 呜喵喵 喵喵 喵喵喵喵呜 喵喵
                    呜呜喵喵
                    呜呜喵 呜呜呜喵喵 喵呜喵 呜喵喵 喵喵
                    呜喵喵喵 喵 呜呜喵 呜喵呜呜 呜呜呜呜喵 呜喵呜喵 喵喵 呜喵呜呜 喵呜喵呜 呜喵呜喵 喵喵 呜喵呜呜 喵喵呜喵 呜
                        呜喵喵喵 喵 呜呜喵 喵呜呜呜 呜呜呜呜喵 呜喵呜喵 喵喵 喵呜呜呜 喵呜喵呜 呜喵呜喵 喵喵 喵呜呜呜 喵喵呜喵 呜
                            呜呜呜喵喵 呜呜呜喵呜 喵喵呜呜 喵 喵喵喵喵 呜喵呜 呜喵 喵呜呜呜喵 呜喵呜呜 呜喵呜 喵呜 喵呜呜呜喵 喵呜呜呜 呜 喵喵
                    呜呜呜喵 喵 呜呜呜喵喵 喵喵呜呜喵 喵 呜呜喵 呜 喵 喵呜呜 呜呜 呜喵 喵喵喵 呜呜 喵呜 喵喵喵 呜喵呜呜呜 呜呜喵呜呜 呜 呜 喵呜喵喵
                        喵喵喵呜 喵呜喵 呜喵喵 喵喵 喵喵喵喵呜 喵喵
                    呜呜喵喵
                呜呜喵喵
        呜呜呜喵 喵 喵喵喵呜 呜 喵呜喵呜喵 喵喵喵呜呜 喵呜喵喵喵喵 喵 喵喵喵喵 呜 喵喵
    呜呜喵喵
    呜喵喵喵 喵 呜呜喵 呜喵 喵呜喵 呜喵喵 喵喵 呜喵 喵喵呜 呜呜呜 喵喵 呜喵 喵喵呜喵 呜 喵呜喵喵
        呜喵喵喵 喵 呜呜喵 喵呜 喵呜喵 呜喵喵 喵喵 喵呜 喵喵呜 呜呜呜 喵喵 喵呜 喵喵呜喵 呜 喵呜喵喵
            呜呜呜喵 喵 喵呜呜 呜呜 呜喵 喵喵喵 呜呜 喵呜 喵喵喵 呜呜喵呜 呜喵喵喵喵 喵呜呜喵 喵呜呜 呜呜 呜喵 喵喵喵 呜呜 喵呜 喵喵喵 喵呜喵呜 喵呜喵喵喵 呜 喵呜喵喵
                呜喵喵呜喵 喵 喵呜呜 呜呜 呜喵 喵喵喵 呜呜 喵呜 喵喵喵 呜 喵喵
                呜呜呜呜呜 喵喵
            呜呜喵喵
            喵呜呜喵喵 喵喵喵呜喵 喵呜喵 呜喵喵 呜喵呜 呜呜呜呜 喵呜喵 呜喵喵 喵喵
            呜喵喵喵 喵 喵呜呜呜呜 喵喵喵喵 呜喵喵呜 喵呜喵呜喵 呜 喵呜喵喵
                呜呜呜喵 喵 喵喵呜呜 喵 喵喵喵喵 呜喵呜 呜喵 呜喵呜 喵呜 呜 呜 呜呜呜呜 喵呜喵 呜喵呜喵 喵喵
                喵喵喵喵喵喵 喵喵喵呜喵 喵呜喵 呜喵呜喵 喵喵
            呜呜喵喵
            呜喵喵呜喵 喵 喵 喵喵喵呜喵 喵呜呜喵 呜呜呜呜 呜 呜喵呜喵喵 呜喵呜呜喵 呜喵喵呜 呜呜呜呜 呜喵呜喵喵 呜喵呜喵呜 呜喵喵呜 喵喵呜喵呜 呜 喵喵
        呜呜喵喵
        呜喵喵呜喵 喵 喵呜呜喵呜 呜 喵喵
    呜呜喵喵
    喵呜喵喵呜 呜喵喵 喵喵
呜呜喵喵


发表于 2025-11-16 17:09:15 回复(4)
50年内没人能看懂
#include <iostream>
#include <string>
#include <vector>

const int N = 4;

const int dis[8][2] = { {-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1} };

bool check(std::vector<std::string>& base, std::vector<std::string>& mine)
{
    for (int i = 0; i < N; ++i)
    {
        for (int j = 0; j < N; ++j)
        {
            if (mine[i][j] >= '0' && mine[i][j] <= '9')
            {
                int tar_mine_num = mine[i][j] - '0';
                int real_min_num = 0;
                for (int k = 0; k < 8; ++k)
                {
                    int x = i + dis[k][0];
                    int y = j + dis[k][1];
                    if (x >= 0 && x < N && y >= 0 && y < N && '.' == mine[x][y] && 'X' == base[x][y])
                    {
                        real_min_num += 1;
                    }
                }

                if (real_min_num != tar_mine_num)
                {
                    return false;
                }
            }
        }
    }
    return true;
}


int main()
{
    std::vector<std::string> mine(N);
    for (int i = 0; i < N; ++i)
    {
        std::cin >> mine[i];
    }

    std::vector<std::string> answer(mine);
    std::vector<std::vector<bool>> used(N, std::vector<bool>(N, false));

    for (int i = 0; i < N; ++i)
    {
        for (int j = 0; j < N; ++j)
        {
            if (mine[i][j] != '.')
            {
                used[i][j] = true;
                for (int k = 0; k < 8; ++k)
                {
                    int x = i + dis[k][0];
                    int y = j + dis[k][1];
                    if (x >= 0 && x < N && y >= 0 && y < N)
                    {
                        used[x][y] = true;
                    }
                }
            }
        }
    }

    for (int mask = 0; mask < (1 << 16); ++mask)
    {
        std::vector<std::string> base(N, std::string(N, '.'));
        for (int i = 0; i < 16; ++i)
        {
            int row = i / N;
            int col = i % N;
            if ((mask >> i) & 1)
            {
                base[row][col] = 'X';
            }
            else
            {
                base[row][col] = 'O';
            }
        }
        if (check(base, mine))
        {
            for (int i = 0; i < N; ++i)
            {
                for (int j = 0; j < N; ++j)
                {
                    if (mine[i][j] != '.')
                    {
                        for (int k = 0; k < 8; ++k)
                        {
                            int x = i + dis[k][0];
                            int y = j + dis[k][1];
                            if (x >= 0 && x < N && y >= 0 && y < N)
                            {
                                if ('.' == answer[x][y])
                                {
                                    if ('X' == base[x][y])
                                    {
                                        answer[x][y] = 'X';
                                    }
                                    else if ('O' == base[x][y])
                                    {
                                        answer[x][y] = 'O';
                                    }
                                }
                                else if ('U' == answer[x][y])
                                {
                                    ;
                                }
                                else
                                {
                                    if (answer[x][y] != base[x][y])
                                    {
                                        answer[x][y] = 'U';
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    for (int i = 0; i < N; ++i)
    {
        for (int j = 0; j < N; ++j)
        {
            if (used[i][j])
            {
                if (mine[i][j] >= '0' && mine[i][j] <= '9')
                {
                    std::cout << mine[i][j];
                }
                else
                {
                    std::cout << ((answer[i][j] == 'U') ? '.' : answer[i][j]);
                }
            }
            else
            {
                std::cout << '.';
            }

        }
        std::cout << '\n';
    }

}


发表于 2025-11-16 13:26:38 回复(0)
为甚吗我的代码失败了?AI都觉的我是对的?求助各位大佬!
#include <stdio.h>  
int main() {
    char grid[4][4];
    char res[4][4];

    for (int i = 0; i < 4; ++i) {
        char line[5];  
        scanf("%s", line);  
        for (int j = 0; j < 4; ++j) {
            grid[i][j] = line[j];
            res[i][j] = grid[i][j];
        }
    }
    int dirs[8][2] = {
        {-1, -1}, {-1, 0}, {-1, 1},
        {0, -1},          {0, 1},
        {1, -1},  {1, 0}, {1, 1}
    };

   
    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < 4; ++j) {
            if (grid[i][j] >= '0' && grid[i][j] <= '8') {
                int k = grid[i][j] - '0';
                int cnt_x = 0, cnt_dot = 0;
                int dot_pos[8][2];
                int dot_idx = 0;

                for (int d = 0; d < 8; ++d) {
                    int ni = i + dirs[d][0];
                    int nj = j + dirs[d][1];
                    if (ni >= 0 && ni < 4 && nj >= 0 && nj < 4) {
                        if (res[ni][nj] == 'X') {
                            cnt_x++;
                        } else if (res[ni][nj] == '.') {
                            cnt_dot++;
                            dot_pos[dot_idx][0] = ni;
                            dot_pos[dot_idx][1] = nj;
                            dot_idx++;
                        }
                    }
                }

                if (cnt_x == k) {
                    for (int d = 0; d < dot_idx; ++d) {
                        res[dot_pos[d][0]][dot_pos[d][1]] = 'O';
                    }
                } else if (cnt_x + cnt_dot == k) {
                    for (int d = 0; d < dot_idx; ++d) {
                        res[dot_pos[d][0]][dot_pos[d][1]] = 'X';
                    }
                }
            }
        }
    }

    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < 4; ++j) {
            printf("%c", res[i][j]);
        }
        printf("\n");
    }

    return 0;
}
发表于 2025-11-17 00:11:56 回复(1)