扫雷扫蓓蕾:
C++#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int N=505;
int n,m,tot,rest;
int a[N][N];
char c[N][N];
bool bomb[N][N],flag[N][N];
int dx[]={0,0,-1,-1,-1,1,1,1},dy[]={-1,1,-1,0,1,-1,0,1};
void init(){
int temp=0;
while(temp<tot){
int x=rand()%n+1,y=rand()%m+1;
if(bomb[x][y]) continue;
bomb[x][y]=1;
temp++;
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
c[i][j]='#';
if(bomb[i][j]){
a[i][j]=419;
continue;
}
for(int k=0;k<8;k++){
int tx=i+dx[k],ty=j+dy[k];
if(tx<1||n<tx||ty<1||m<ty) continue;
if(bomb[tx][ty]) a[i][j]++;
}
}
}
}
void show(){
cout<<"\033[2J\033[H";
cout<<"\033[4m";
for(int i=0;i<=n;i++){
for(int j=0;j<=m;j++){
if(i&&j){
if('0'<=c[i][j]&&c[i][j]<='9'){
cout<<"\033[36m";
}else if(c[i][j]=='@'){
cout<<"\033[31m";
}else if(c[i][j]=='#'){
cout<<"\033[32m";
}
cout<<c[i][j]<<" ";
cout<<"\033[37m";
if(c[i][j]=='#'||c[i][j]=='@') rest++;
}else{
cout<<"\033[4m";
if(i==0&&j==0) cout<<'\\';
else if(i==0) printf("%2d",j);
else if(j==0) printf("%2d",i);
}
cout<<"\033[0m";
cout<<" ";
}
cout<<"\n\n";
}
}
void show_end(){
cout<<"\033[4m";
for(int i=0;i<=n;i++){
for(int j=0;j<=m;j++){
if(i&&j){
if(a[i][j]==419) cout<<"\033[31m* ";
else if(!a[i][j]) cout<<" ";
else{
cout<<"\033[36m";
cout<<a[i][j]<<" ";
}
cout<<"\033[37m";
}else{
cout<<"\033[4m";
if(i==0&&j==0) cout<<'\\';
else if(i==0) printf("%2d",j);
else if(j==0) printf("%2d",i);
}
cout<<"\033[0m";
cout<<" ";
}
cout<<"\n\n";
}
}
int stat(){
rest=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(c[i][j]=='#'||c[i][j]=='@') rest++;
}
}
}
bool open(int x,int y){
if(flag[x][y]) return true;
if(bomb[x][y]) return false;
if(c[x][y]!='#'){
bool may_lose=0;
int flag_num=0;
for(int k=0;k<8;k++){
int tx=x+dx[k],ty=y+dy[k];
if(tx<1||n<tx||ty<1||m<ty) continue;
if(flag[tx][ty]) flag_num++;
if(bomb[tx][ty]&&!flag[tx][ty]) may_lose=1;
}
if(flag_num!=a[x][y]) return true;
if(may_lose) return false;
for(int k=0;k<8;k++){
int tx=x+dx[k],ty=y+dy[k];
if(tx<1||n<tx||ty<1||m<ty) continue;
if(c[tx][ty]=='#'){
open(tx,ty);
}
}
return true;
}
queue<pair<int,int> > q;
q.push(make_pair(x,y));
if(a[x][y]) c[x][y]='0'+a[x][y];
else c[x][y]=' ';
while(!q.empty()){
int ux=q.front().first, uy=q.front().second;
q.pop();
for(int k=0;k<8;k++){
int tx=ux+dx[k],ty=uy+dy[k];
if(tx<1||n<tx||ty<1||m<ty||c[tx][ty]!='#') continue;
if(!a[tx][ty]){
c[tx][ty]=' ';
q.push(make_pair(tx,ty));
}else if(c[ux][uy]==' '){
c[tx][ty]='0'+a[tx][ty];
}
}
}
return true;
}
int main(){
system("cls");
cout<<"To start a game with a map of [n]-row, [m]-col and [num]'s bombs\n";
cout<<"You need input [n] [m] [num]\n";
cout<<"(No need to input the symbol [] )\n";
cin>>n>>m>>tot;
srand(time(0));
init();
bool result=0;
while(1){
show();
cout<<"Input 1 [x] [y] to open a block. Or if you've marked the blocks around, this can open them quickly\n";
cout<<"Input 2 [x] [y] to mark a bomb. The block you've marked won't be open by mistake\n";
cout<<"(No need to input the symbol [] )\n";
int opt,x,y;
cin>>opt>>x>>y;
if(opt==1){
if(!open(x,y)){
break;
}
stat();
if(rest==tot){
result=1;
break;
}
}else{
if(c[x][y]!='#'&&c[x][y]!='@') continue;
flag[x][y]=!flag[x][y];
if(flag[x][y]) c[x][y]='@';
else c[x][y]='#';
}
}
if(result) cout<<"\033[32m------YOU WIN------";
else cout<<"\033[31m------YOU LOSE------";
cout<<"\033[37m";
cout<<"\n\nMAP: \n";
show_end();
return 0;
}