洛谷 P2670
#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
int n, m, mp[110][110];
char temp;
cin >> n >> m;
//读取地图,*记为-1,?记为0
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> temp;
if (temp == '*') mp[i][j] = -1;
else mp[i][j] = 0;
}
}
//遍历整个地图,遇到-1时停下
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (mp[i][j] == -1) {
for (int y = -1; y <= 1; y++) {//给-1周围所有数+1
for (int x = -1; x <= 1; x++) {
if (mp[i + y][j + x] >= 0) {
mp[i + y][j + x]++;
}
}
}
}
}
}
for (int i = 0; i < n; i++) {//打印
for (int j = 0; j < m; j++) {
if (mp[i][j] == -1) cout << "*";
else cout << mp[i][j];
}
cout << "\n";
}
return 0;
}