ghjhkniob 2020-04-21 16:45 采纳率: 93.3%
浏览 1125
已采纳

在9*9的矩阵中随机生成10个“雷”,输出“雷”的分布矩阵(扫雷游戏的初始状态)。C语言怎么写代码?

在9*9的矩阵中随机生成10个“雷”,输出“雷”的分布矩阵(扫雷游戏的初始状态)。要求“雷”的位置输出字符(ASCII码15),非“雷”的位置输出附近有多少颗“雷”,附近无雷不输出。
提示:
① 声明一个9×9的二维数组,并将每个元素初始化为0;
② 随机生成10个“雷”:可使用随机函数生成10对不重复的行列下标,将其值置为-1,之后使用循环累计每个非-1元素周围-1的个数。
③ 计算非“雷”位置附近有多少“雷”,即当数组元素不等于-1时,计算它上下左右8个数组元素中值为-1的个数。可以累加这几个数组元素是否为-1的关系表达式和逻辑表达式之和来完成。例如计算数组元素a[i][j]附近上一行的“雷”个数可以使用如下表达式:
“(i>0&&j>0&&a[i-1][j-1]==-1)+(i>0&&a[i-1][j]==-1)+(i>0&&j<8&&a[i-1][j+1]==-1)”。

  • 写回答

2条回答 默认 最新

  • Huoon 2020-04-21 23:31
    关注

    方法1

    #include <iostream>
    #include <random>
    #include <time.h>
    void main()
    {
        char matrix[9][9];
        for (int i = 0; i < 9; i++)
        {
            for (int j = 0; j < 9; j++)
            {
                matrix[i][j] = 0;
            }
        }
    
        srand(time(NULL));
    
        int n = 10;
        while (n--)
        {
            int line = rand() % 9;
            int row = rand() % 9;
            if (matrix[line][row] == 0)
            {
                matrix[line][row] = -1;
            }
            else
            {
                n++;
                continue;
            }
        }
    
        for (int i = 0; i < 9; i++)
        {
            for (int j = 0; j < 9; j++)
            {
                if (matrix[i][j] != -1)
                {
                    int leftIndex = j - 1;
                    int rightIndex = j + 1;
                    int topIndex = i - 1;
                    int bottomIndex = i + 1;
    
                    int left = leftIndex;
                    int top = topIndex;
    
                    int leftMoveStep = 0;
                    int topMoveStep = 0;
                    int count = 0;
                    while (true)
                    {
                        if (left >= 0 && left < 9 && top >= 0 && top < 9)
                        {
                            if (matrix[top][left] == -1)
                            { 
                                count++;
                            }
                        }
    
                        if (left == leftIndex && top == i)
                        {
                            break;
                        }
    
                        if (left - leftIndex < 2 && top == topIndex)
                        {
                            leftMoveStep = 1;
                            topMoveStep = 0;
                        }
                        else if (left == rightIndex && top == topIndex)
                        {
                            topMoveStep = 1;
                            leftMoveStep = 0;
                        }
                        else if (left == rightIndex && top == bottomIndex)
                        {
                            topMoveStep = 0;
                            leftMoveStep = -1;
                        }
                        else if (left == leftIndex && top == bottomIndex)
                        {
                            leftMoveStep = 0;
                            topMoveStep = -1;
                        }
                        else if (left - leftIndex < 2 && top == bottomIndex)
                        {
                            leftMoveStep = -1;
                            topMoveStep = 0;
                        }
                        left += leftMoveStep;
                        top += topMoveStep;
                    }
    
                    matrix[i][j] = count;
    
                    count > 0 ? printf("%d ", count) : printf("  ");
                }
                else
                {
                    printf("%c ", 15);
                }
            }
            printf("\n");
        }
    
        getchar();
    }
    

    方法2(性能更好)

    
    #include <iostream>
    #include <random>
    #include <time.h>
    void main()
    {
        char matrix[9][9];
        for (int i = 0; i < 9; i++)
        {
            for (int j = 0; j < 9; j++)
            {
                matrix[i][j] = 0;
            }
        }
    
        srand(time(NULL));
    
        int n = 10;
        while (n--)
        {
            int line = rand() % 9;
            int row = rand() % 9;
            if (matrix[line][row] != -1)
            {
                matrix[line][row] = -1;
    
                int leftIndex = row - 1;
                int rightIndex = row + 1;
                int topIndex = line - 1;
                int bottomIndex = line + 1;
    
                int left = leftIndex;
                int top = topIndex;
    
                int leftMoveStep = 0;
                int topMoveStep = 0;
                int count = 0;
                while (true)
                {
                    if (left >= 0 && left < 9 && top >= 0 && top < 9)
                    {
                        if (matrix[top][left] != -1)
                        {
                            matrix[top][left] += 1;
                        }
                    }
    
                    if (left == leftIndex && top == line)
                    {
                        break;
                    }
    
                    if (left - leftIndex < 2 && top == topIndex)
                    {
                        leftMoveStep = 1;
                        topMoveStep = 0;
                    }
                    else if (left == rightIndex && top == topIndex)
                    {
                        topMoveStep = 1;
                        leftMoveStep = 0;
                    }
                    else if (left == rightIndex && top == bottomIndex)
                    {
                        topMoveStep = 0;
                        leftMoveStep = -1;
                    }
                    else if (left == leftIndex && top == bottomIndex)
                    {
                        leftMoveStep = 0;
                        topMoveStep = -1;
                    }
                    else if (left - leftIndex < 2 && top == bottomIndex)
                    {
                        leftMoveStep = -1;
                        topMoveStep = 0;
                    }
                    left += leftMoveStep;
                    top += topMoveStep;
                }
            }
            else
            {
                n++;
                continue;
            }
        }
    
        for (int i = 0; i < 9; i++)
        {
            for (int j = 0; j < 9; j++)
            {
                if (matrix[i][j] != -1)
                {
    
                    matrix[i][j] > 0 ? printf("%d ", matrix[i][j]) : printf("  ");
                }
                else
                {
                    printf("%c ", 15);
                }
            }
            printf("\n");
        }
    
        getchar();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)
  • ¥15 如何解决MIPS计算是否溢出
  • ¥15 vue中我代理了iframe,iframe却走的是路由,没有显示该显示的网站,这个该如何处理
  • ¥15 操作系统相关算法中while();的含义
  • ¥15 CNVcaller安装后无法找到文件
  • ¥15 visual studio2022中文乱码无法解决
  • ¥15 关于华为5g模块mh5000-31接线问题
  • ¥15 keil L6007U报错
  • ¥15 webapi 发布到iis后无法访问