题目描述
见 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;
    }
}



















