跳到正文

题解

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

cyrel_awa约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年1月4日
篇幅
约 2 分钟
获赞
1
题目
P2670

版本

  1. v1当前

题意

对于每一个问号,统计它八个方向上的星号数量。

思路

输入。枚举雷区,如果此格是问号,搜索与它八联通的格子,如果是星号,计数器加 11,将结果放在原数组里,最后输出即可。

代码

C++
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <string>
#include <cstring>
#include <climits>
#include <cstdlib>
using namespace std;
typedef long long ll;
int n, m, dx[] = {0, 0, -1, 1, 1, 1, -1, -1}, dy[] = {-1, 1, 0, 0, -1, 1, -1, 1};//变化量数组,使用时把原位置加上数组值,得到要枚举的位置
char s[105][105];
int main(){
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++) cin >> s[i][j];
	for (int i = 1; i <= n; i++){
		for (int j = 1; j <= m; j++){
			if (s[i][j] == '?'){//如果此格是问号
				int cnt = 0;
				for (int k = 0; k < 8; k++){
					int nx = i+dx[k], ny = j+dy[k];//使用变化量数组枚举八联通格子
					if (nx < 1 || nx > n || ny < 1 || ny > m) continue;//防止统计到雷区外
					if (s[nx][ny] == '*') cnt++;
				}
				s[i][j] = cnt+'0';
			}
		}
	}
	for (int i = 1; i <= n; i++, puts(""))
		for (int j = 1; j <= m; j++) cout << s[i][j];
	return 0;
}
原文