王容宇 • 12天前
扫雷:
#include <bits/stdc++.h>
using namespace std;
int sum[101][101], b[101][101], now=0, lei=0, zhlei=0;
int n;
void v(int x, int y)
{
for(int i = -1; i<=1; i++)
{
for(int j = -1; j<=1; j++)
{
if(!(x+i==0 || x+i==n+1 || y+j==0 || y+j==n+1 || (i==0 && j==0)))
{
if(sum[x+i][y+j]>=0 && !b[x+i][y+j])
{
now++;
b[x+i][y+j]=1;
if(sum[x+i][y+j]==0)
{
v(x+i, y+j);
}
}
}
}
}
}
int main()
{
srand(time(0));
cin >> n;
for(int i = 1; i<=n;)
{
int x = rand()%n+1, y=rand()%n+1;
if(sum[x][y]>=0)
{
i++;
sum[x][y]=-1e9;
for(int j = -1; j<=1; j++)
{
for(int k = -1; k<=1; k++)
{
sum[x+j][y+k]++;
}
}
}
}
while(now!=n*(n-1) && zhlei!=n)
{
for(int i = 1; i<=n; i++)
{
for(int j = 1; j<=n; j++)
{
if(b[i][j]>0)
{
cout << sum[i][j];
}
else if(b[i][j]==0)
{
cout << "?";
}
else
{
cout << "*";
}
}
cout << endl;
}
cout << "还有" << n*(n-1)-now << "个格子" << endl;
cout << "还有" << n-lei << "个雷" << endl;
int id, x, y;
cin >> id >> x >> y;
if(x<1 || x>n || y<1 || y>n)
{
cout << "你干嘛~~~" << endl;
}
else if(id==1 && !b[x][y])
{
now++;
if(sum[x][y]<0)
{
cout << "你输了!" << endl;
for(int i = 1; i<=n; i++)
{
for(int j = 1; j<=n; j++)
{
if(sum[i][j]>=0)
{
cout << sum[i][j];
}
else
{
cout << "*";
}
}
cout << endl;
}
return 0;
}
b[x][y]=1;
if(sum[x][y]==0)
{
v(x, y);
}
}
else if(id==2 && !b[x][y])
{
b[x][y]=-1;
lei++;
if(sum[x][y]<0)
{
zhlei++;
}
}
else if(id==3 && b[x][y]==-1)
{
b[x][y]=0;
lei--;
if(sum[x][y]<0)
{
zhlei--;
}
}
else
{
cout << "你干嘛~~~" << endl;
}
}
for(int i = 1; i<=n; i++)
{
for(int j = 1; j<=n; j++)
{
if(sum[i][j]>=0)
{
cout << sum[i][j];
}
else
{
cout << "*";
}
}
cout << endl;
}
cout << "你赢了!" << endl;
return 0;
}
评论: