注释可能不是很全,但我相信你们能看懂
#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
const int nans[8][2]={{1,0},{0,1},{-1,0},{0,-1},{1,-1},{-1,-1},{-1,1},{1,1}};//判断九宫格内是否有雷用的数组
const int awsd[4][2]={{1,0},{0,1},{-1,0},{0,-1}};//展开用的数组
int n,l;//规格
char qp[1010][1010];//用于展示当前的情况
bool un[1010][1010]={0};//标记雷的位置
void dfs(int x,int y){
int ans=0;//统计雷的个数
for(int i=0;i<8;i++){
int a=x+nans[i][0],b=y+nans[i][1];
if(un[a][b])ans++;
l--;
qp[x][y]=ans+'0';
if(ans>0)return;
for(int i=0;i<4;i++){
int p=x+awsd[i][0],q=y+awsd[i][1];
if(p>n||q>n)continue;
dfs(p,q);
}
return;
}
int main(){
system("color B");
int x,y;
cin>>n;
l=n*n;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
qp[i][j]='.';
srand((unsigned)time(NULL));
for(int i=1;i<n;i++){
int a=rand()%n+1,b=rand()%n+1;
if(!un[a][b])un[a][b]=1;
else i--;
}
while(l){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++)
cout<<qp[i][j]<<' ';
cout<<endl;
}
cin>>x>>y;
if(un[x][y]){
cout<<"Game Over!";
return 0;
}
dfs(x,y);
system("cls");
}
return 0;
}