专栏文章

题解:P2670 [NOIP2015 普及组] 扫雷游戏

P2670题解参与者 8已保存评论 8

文章操作

快速查看文章及其快照的属性,并进行相关操作。

当前评论
8 条
当前快照
1 份
快照标识符
@miqlx4jo
此快照首次捕获于
2025/12/04 06:57
3 个月前
此快照最后确认于
2025/12/04 06:57
3 个月前
查看原文

题解:P2670 [NOIP2015 普及组] 扫雷游戏

吐槽一句,这么弱的题,居然这么少人发题解?

1. 解题思路

我的思路比较中规中矩,不容易错。其实就是定义两个方向数组,分别记录一个点周围 88 个格子横纵坐标减去这个格子横纵坐标的数值,这样可以少写许多判断语句。
注意注意!扫描到它周围的每一个格子时都要判断是否下标越界!
梳理一遍,先读入,随后扫描整个数组,如果当前的字符是 * 就直接输出,否则扫描它周围的 88 个格子,有 * 就计数器加一。

2. 代码实现

代码非常简单呀~
CPP
#include <bits/stdc++.h>
using namespace std;
const int maxn = 110;
const int dx[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int dy[] = {1, 1, 0, -1, -1, -1, 0, 1};
int n, m;
char a[maxn][maxn];
inline bool inb(int x, int y) {
    return x > 0 && y > 0 && x <= n && y <= m;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    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] == '*') { cout << '*'; continue; }
            int cnt = 0;
            for (int k = 0; k < 8; k++) {
                int nx = i + dx[k], ny = j + dy[k];
                if (inb(nx, ny) && a[nx][ny] == '*') cnt++;
            } cout << cnt;
        }
        cout << '\n';
    }
    return 0;
}

评论

8 条评论,欢迎与作者交流。

正在加载评论...