扫雷--蓝桥杯笔记

题目:

题目描述

在一个 n 行 m 列的方格图上有一些位置有地雷,另外一些位置为空。

请为每个空位置标一个整数,表示周围八个相邻的方格中有多少个地雷。

输入描述

输入的第一行包含两个整数 n, m。

第 2 行到第 n + 1 行每行包含 m 个整数,相邻整数之间用一个空格分隔。如果对应的整数为 0,表示这一格没有地雷。如果对应的整数为 1,表示这一格有地雷。

其中,≤n,m≤100 分钟后还是在当天。

输出描述

输出 n 行,每行 m 个整数,相邻整数之间用空格分隔。

对于没有地雷的方格,输出这格周围的地雷数量。对于有地雷的方格,输出 9。

输入输出样例

示例 1
输入
3 4
0 1 0 0
1 0 1 0
0 0 1 0
输出
2 9 2 1
9 4 9 2
1 3 9 2

运行限制

  • 最大运行时间:1s

  • 最大运行内存: 128M

题解:

  1. 定义两个数组,一个用于存储数据,一个用于输出;

  1. 遍历四周的雷

代码:

#include<iostream>


using namespace std;

const int N = 1e2 + 10;
int n, m;
int a[N][N], b[N][N];
int get(int x, int y)
{
    int res = 0;
    for (int i = x - 1; i <= x + 1; i++)
        for (int j = y - 1; j <= y + 1; j++)
            res += a[i][j];
    return res;
}
int main()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            cin >> a[i][j];
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            if (a[i][j])
                b[i][j] = 9;
            else
                b[i][j] = get(i, j);
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
            cout << b[i][j] << ' ';
        cout << endl;
    }
    return 0;
}

题目较易,发张图奖励自己

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的实现扫雷游戏的 Java 代码: ``` import java.util.Scanner; public class Minesweeper { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the number of rows: "); int rows = scanner.nextInt(); System.out.print("Enter the number of columns: "); int cols = scanner.nextInt(); System.out.print("Enter the number of mines: "); int mines = scanner.nextInt(); int[][] board = new int[rows][cols]; // Place mines randomly on the board for (int i = 0; i < mines; i++) { int row = (int) (Math.random() * rows); int col = (int) (Math.random() * cols); if (board[row][col] == -1) { i--; } else { board[row][col] = -1; } } // Calculate the number of mines around each cell for (int row = 0; row < rows; row++) { for (int col = 0; col < cols; col++) { if (board[row][col] != -1) { int count = 0; if (row > 0 && col > 0 && board[row - 1][col - 1] == -1) count++; if (row > 0 && board[row - 1][col] == -1) count++; if (row > 0 && col < cols - 1 && board[row - 1][col + 1] == -1) count++; if (col > 0 && board[row][col - 1] == -1) count++; if (col < cols - 1 && board[row][col + 1] == -1) count++; if (row < rows - 1 && col > 0 && board[row + 1][col - 1] == -1) count++; if (row < rows - 1 && board[row + 1][col] == -1) count++; if (row < rows - 1 && col < cols - 1 && board[row + 1][col + 1] == -1) count++; board[row][col] = count; } } } // Print the board for (int row = 0; row < rows; row++) { for (int col = 0; col < cols; col++) { if (board[row][col] == -1) { System.out.print("* "); } else { System.out.print(board[row][col] + " "); } } System.out.println(); } } } ``` 运行时,程序会提示用户输入行数、列数和雷数。然后程序会在二维数组中随机布置指定数量的地雷,并计算每个方格周围地雷的数量。最后,程序会打印出整个游戏板。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值