用二维字符数组获取
#include<bits/stdc++.h>
using namespace std;
#define N 105
#define f(i,a,b) for(int i=a;i<=b;i++)
char a[N][N];
int r[]={-1,0,1,0,-1,1,-1,1},c[]={0,-1,0,1,-1,-1,1,1};
int main(){
freopen("mine.in","r",stdin);
freopen("mine.out","w",stdout);
int n,m;
cin>>n>>m;
getchar();
f(i,1,n){
f(j,1,m){
cin>>a[i][j];
if(a[i][j]!='*') a[i][j]='0';
}
getchar();
}
f(i,1,n)
f(j,1,m)
if(a[i][j]=='*')
f(k,0,7)
if(a[i+r[k]][j+c[k]]!='*')
a[i+r[k]][j+c[k]]=a[i+r[k]][j+c[k]]+1;
f(i,1,n){
f(j,1,m)
cout<<a[i][j];
cout<<'\n';
}
return 0;
}
用string和int二维数组获取
#include<bits/stdc++.h>
using namespace std;
#define N 105
#define f(i,a,b) for(int i=a;i<=b;i++)
int a[N][N];
int r[]={-1,0,1,0,-1,1,-1,1},c[]={0,-1,0,1,-1,-1,1,1};
int main(){
freopen("mine.in","r",stdin);
freopen("mine.out","w",stdout);
int n,m;
cin>>n>>m;
string s;
f(i,1,n){
cin>>s;
f(j,1,m){
if(s[j-1]=='*') a[i][j]=-1;
else a[i][j]=0;
}
}
f(i,1,n)
f(j,1,m)
f(k,0,7)
if(a[i][j]==-1&&a[i+r[k]][j+c[k]]!=-1)
a[i+r[k]][j+c[k]]=a[i+r[k]][j+c[k]]+1;
f(i,1,n){
f(j,1,m)
if(a[i][j]==-1) cout<<'*';
else cout<<a[i][j];
cout<<'\n';
}
return 0;
}