【题解】[NOIP2015]扫雷游戏(Java & C++)

本文介绍了一道NOIP2015普及组的扫雷题目,重点讲解了如何在编程中统计每个格子周围地雷数量,特别是针对边界情况的特殊处理,并提供了Java和C++的代码示例。

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

题目描述

P2670 [NOIP2015 普及组] 扫雷游戏 - 洛谷

前置知识

无。

题目分析

只需要统计每一个格子周围 8 个格子的地雷数量即可。

重点是处理边界情况,比如左上角,有五个格子不在数组范围内,需要特殊处理。
示意图

为了避免思路混乱,可以考虑单独开写一个函数来判断某个格子是否有地雷。

参考代码

Java:

import java.util.*;

public class Main {
    private static boolean[][] mines;
    private static int n, m; // n=行,m=列
    
    public static void main(String[] args) {
        // 输入
        Scanner s = new Scanner(System.in);
        n = s.nextInt();
        m = s.nextInt();
        s.nextLine();
        mines = new boolean[n][m];
        for (int i = 0; i < n; i++) {
            String line = s.nextLine();
            char[] tokens = line.toCharArray();
            for (int j = 0; j < m; j++) {
                mines[i][j] = (tokens[j] == '*');
            }
        }
        
        // 计算
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                int count = getCount(i ,j);
                if (count == -1)
                	System.out.print("*");
                else
                	System.out.print(count);
            }
            System.out.println();
        }
        
    }
    
    private static int getCount(int row, int col) {
        // 是地雷
        if (mines[row][col])
            return -1;
        
        // 不是地雷
        int count = 0;
        count += hasMine(row-1, col-1);
        count += hasMine(row-1, col);
        count += hasMine(row-1, col+1);
        
        count += hasMine(row, col-1);
        count += hasMine(row, col);
        count += hasMine(row, col+1);
        
        count += hasMine(row+1, col-1);
        count += hasMine(row+1, col);
        count += hasMine(row+1, col+1);
        
        return count;
    }
    
    private static int hasMine(int row, int col) {
    	// 重点:越界判断
    	if (row >= n || col >= m || row < 0 || col < 0) {
    		return 0;
    	}
    	else {
			return mines[row][col] ? 1: 0;
		}
    }
}

C++:
(由 ChatGPT 转换)

#include <iostream>
#include <vector>

using namespace std;

vector<vector<bool>> mines;
int n, m; // n=行,m=列

int getCount(int row, int col);
int hasMine(int row, int col);

int main() {
    // 输入
    cin >> n >> m;
    mines.resize(n, vector<bool>(m, false));
    cin.ignore(); // Consume the newline character

    for (int i = 0; i < n; i++) {
        string line;
        cin >> line;
        for (int j = 0; j < m; j++) {
            mines[i][j] = (line[j] == '*');
        }
    }

    // 计算
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            int count = getCount(i, j);
            if (count == -1)
                cout << '*';
            else
                cout << count;
        }
        cout << endl;
    }

    return 0;
}

int getCount(int row, int col) {
    // 是地雷
    if (mines[row][col])
        return -1;

    // 不是地雷
    int count = 0;
    count += hasMine(row - 1, col - 1);
    count += hasMine(row - 1, col);
    count += hasMine(row - 1, col + 1);

    count += hasMine(row, col - 1);
    count += hasMine(row, col);
    count += hasMine(row, col + 1);

    count += hasMine(row + 1, col - 1);
    count += hasMine(row + 1, col);
    count += hasMine(row + 1, col + 1);

    return count;
}

int hasMine(int row, int col) {
    // 重点:越界判断
    if (row >= n || col >= m || row < 0 || col < 0) {
        return 0;
    } else {
        return mines[row][col] ? 1 : 0;
    }
}

你好!对于扫雷游戏题解,我可以给你一些思路和代码示例。首先,你需要了解扫雷游戏的规则和要求。接下来,你可以使用C++语言来实现游戏逻辑和界面。 下面是一个简单的扫雷游戏C++代码示例: ```cpp #include &lt;iostream&gt; #include &lt;vector&gt; #include &lt;random&gt; using namespace std; class MinesweeperGame { private: int rows; int cols; vector&lt;vector&lt;char&gt;&gt; board; vector&lt;vector&lt;bool&gt;&gt; revealed; vector&lt;pair&lt;int, int&gt;&gt; directions = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}}; public: MinesweeperGame(int m, int n, int mineCount) { rows = m; cols = n; board.resize(rows, vector&lt;char&gt;(cols, &#39; &#39;)); revealed.resize(rows, vector&lt;bool&gt;(cols, false)); placeMines(mineCount); calculateNumbers(); } void printBoard() { cout &lt;&lt; &quot; &quot;; for (int j = 0; j &lt; cols; j++) { cout &lt;&lt; j &lt;&lt; &quot; &quot;; } cout &lt;&lt; endl; for (int i = 0; i &lt; rows; i++) { cout &lt;&lt; i &lt;&lt; &quot; |&quot;; for (int j = 0; j &lt; cols; j++) { cout &lt;&lt; board[i][j] &lt;&lt; &quot;|&quot;; } cout &lt;&lt; endl; } } bool isGameOver() { for (int i = 0; i &lt; rows; i++) { for (int j = 0; j &lt; cols; j++) { if (board[i][j] == &#39;M&#39; &amp;&amp; revealed[i][j]) { return true; } } } return false; } void reveal(int row, int col) { if (row &lt; 0 || row &gt;= rows || col &lt; 0 || col &gt;= cols || revealed[row][col]) { return; } revealed[row][col] = true; if (board[row][col] == &#39;M&#39;) { return; } if (board[row][col] == &#39;0&#39;) { for (auto dir : directions) { reveal(row + dir.first, col + dir.second); } } } private: void placeMines(int mineCount) { random_device rd; mt1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值