题解
2024-01-31 10:07:22
发布于:广东
#include<iostream>
#include<cstring>
using namespace std;
char a[105][105],b[105][105];
int main(){
int n,m,g;
cin >> n >> m;
for(int i = 0;i < n;i ++){
for(int j = 0;j < m;j ++){//输入大地图
cin >> a[i][j];
}
}
for(int i =0;i < n;i ++){
for(int j = 0;j < m;j ++){
g = 0;
if(a[i][j] == ''){
b[i][j] = '';
} else {
if(i - 1 >= 0 and a[i - 1][j] == ''){//对于每一个方位进行判断
g += 1;
}
if(i + 1 < n and a[i + 1][j] == ''){
g += 1;
}
if(j - 1 >= 0 and a[i][j - 1] == ''){
g += 1;
}
if(j + 1 < m and a[i][j + 1] == ''){
g += 1;
}
if(i + 1 < n and j + 1 < m and a[i + 1][j + 1] == ''){
g += 1;
}
if(i - 1 >= 0 and j - 1 >= 0 and a[i - 1][j - 1] == ''){
g += 1;
}
if(i + 1 < n and j - 1 >= 0 and a[i + 1][j - 1] == ''){
g += 1;
}
if(i - 1 >= 0 and j + 1 < m and a[i - 1][j + 1] == ''){
g += 1;
}
b[i][j] = 48 + g;
}
}
}
for(int i = 0;i < n;i ++){
for(int j = 0;j < m;j ++){//将带有数据的数组输出
cout << b[i][j];
}
cout << endl;
}
return 0;
}
这里空空如也
有帮助,赞一个