题意
对于每一个问号,统计它八个方向上的星号数量。
思路
输入。枚举雷区,如果此格是问号,搜索与它八联通的格子,如果是星号,计数器加 ,将结果放在原数组里,最后输出即可。
代码
#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;
}