问题遇到的现象和发生背景
扫雷游戏是一款十分经典的单机小游戏。它的精髓在于,通过已翻开格子所提示的周围格地雷数,来判断未翻开格子里是否是地雷。
现在给出 nn 行 mm 列的雷区中的地雷分布,要求计算出每个非地雷格的周围格地雷数。
注:每个格子周围格有八个:上、下、左、右、左上、右上、左下、右下。
输入格式
第一行包含两个整数 nn 和 mm,分别表示雷区的行数和列数。1 \le n \le 100, 1 \le m \le 1001≤n≤100,1≤m≤100。
接下来 nn 行,每行 mm 个字符,'*'表示相应格子中是地雷,'?'表示相应格子中无地雷。字符之间无任何分隔符。
输出格式
nn 行,每行 mm 个字符,描述整个雷区。若相应格中是地雷,则用'*'表示,否则用相应的周围格地雷数表示。字符之间无任何分隔符。
Sample Input
3 3
??
???
??
Sample Output
10
221
11
蒟蒻代码写的很菜,望见谅
问题相关代码,请勿粘贴截图
#include<bits/stdc++.h>
using namespace std;
int main (){
int x,y;cin>>x>>y;
char **a=new (std::nothrow)char*[x+2];
for(int i=0;i<y+2;i++){
a[i]=new (std::nothrow)char [y+2];
}//a[x+1][y+1];
for(int i=1;i<=x;i++){
for(int j=1;j<=y;j++){
cin>>a[i][j];
// cout<<a[i][j];
}
// cout<<'\n';
}
int **b=new (std::nothrow)int*[x+2];
for(int i=0;i<y+2;i++){
b[i]=new (std::nothrow)int [y+2];
}
for(int i=1;i<=x;i++){
for(int j=1;j<=y;j++){
b[i][j]=0;
// cout<<b[i][j];
}
// cout<<'\n';
}
for(int i=1;i<=x;i++){
for(int j=1;j<=y;j++){
if(a[i][j]=='*')continue;
// cout<<a[i][j];
if(a[i-1][j-1]=='*') b[i][j]+=1;
if(a[i-1][j]=='*') b[i][j]+=1;
if(a[i][j-1]=='*') b[i][j]+=1;
if(a[i+1][j]=='*') b[i][j]+=1;
if(a[i][j+1]=='*') b[i][j]+=1;
if(a[i-1][j+1]=='*') b[i][j]+=1;
if(a[i+1][j-1]=='*') b[i][j]+=1;
if(a[i+1][j+1]=='*') b[i][j]+=1;
}/*cout<<'\n';*/
}
for(int i=1;i<=x;i++){
for(int j=1;j<=y;j++){
if(a[i][j]=='*')cout<<a[i][j];
else cout<<b[i][j];
}
cout<<'\n';
}
for(int i=0;i<y+1;i++)
delete []a[i];
delete []a;
for(int i=0;i<y+1;i++)
delete []b[i];
delete []b;
return 0;
}