#include<bits/stdc++.h>
using namespace std;
int a[2000][2000];
int main(){
int n,m;
int Bomb;
char x;
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>x;
if(x=='*'){
a[i][j]=-1;//Bomb!
}else{
a[i][j]=1;//No Bomb
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(a[i][j]==1){
Bomb=0;
for(int k=-1;k<=1;k++){
for(int l=-1;l<=1;l++){
if(a[i+k][j+l]==-1){
Bomb+=1;
}
}
}
a[i][j]=Bomb;
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(a[i][j]!=-1){
cout<<a[i][j];
}else{
cout<<"*";
}
}
cout<<endl;
}
return 0;
}
不要把x定义为string,必须是char。因为用string会一直读取到换行: x="*.*."

京公网安备 11010502036488号