思路如下:
首先将地雷分布读入二维字符数组中,如果是地雷" * "就输出,否则在八个方向上遍历即可,可以用两个一维数组控制方向,详见代码sweep()函数
//#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <cctype>
#include <sstream>
#define inf 0x3f3f3f3f
#define eps 1e-6
using namespace std;
#define clr(x) memset(x,0,sizeof((x)))
const int maxn = 1e4+1;//2e6+1
#define MAX(a,b,c) ((a)>(b)?((a)>(c)?(a):(c)):((b)>(c)?(b):(c)))
#define _max(a,b) ((a) > (b) ? (a) : (b))
#define _min(a,b) ((a) < (b) ? (a) : (b))
#define _for(a,b,c) for(int a = b;a<c;a++)
char cb[105][105];
int n,m;
int readchar() {
int ch;
for(;;) {
ch = getchar();
if(ch!='\n'&&ch!='\r')return ch;
}
}
int a[] = {
-1,0,1};
int b[] = {
-1,0,1};
void sweep(int x,int y) {
int cnt = 0;
for(int i = 0;i<3;i++) {
for(int j = 0;j<3;j++) {
if(x+a[i]>=0&&y+b[j]>=0&&x+a[i]<n&&y+b[j]<m) {
if(cb[x+a[i]][y+b[j]]=='*')cnt++;
}
}
}
cout<<cnt;
}
int main()
{
#ifdef LOCAL
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
#endif
cin>>n>>m;
_for(i,0,n) {
_for(j,0,m) {
cb[i][j] = readchar();
}
}
for(int i = 0;i<n;i++) {
for(int j = 0;j<m;j++) {
if(cb[i][j]=='*') {
cout<<cb[i][j];
}
else sweep(i,j);
}
cout<<endl;
}
return 0;
}