【原创】扫雷纯享版

· · 算法·理论

作者温馨提示(务必要看!):

由于作者究极无敌蒟蒻,连随机数都不会,所以只能拜托好心的你们手输雷的位置了=(

还有,作者注释掉的部分用于在点到空白格子时自动探查周围,但编写的是理想情况,现实中无法运行(TLE),作者在自家电脑上试了最多留一行,目前的效果是翻开周围的8格

以上两个问题如果有什么好办法解决(别忘了我是个蒟蒻)请回复

!是雷(当然你能看到就说明你无了),#是小旗子标记

以下是代码

#include<bits/stdc++.h>
using namespace std;
int lst[101][101],cover[101][101];
void safe(int a,int b) {
    cover[a-1][b-1]=0;
    cover[a-1][b]=0;
    cover[a-1][b+1]=0;
    cover[a][b-1]=0;
    cover[a][b+1]=0;
    cover[a+1][b-1]=0;
    cover[a+1][b]=0;
    cover[a+1][b+1]=0;
//  if(lst[a-1][b-1]==0)safe(a-1,b-1);
//  if(lst[a-1][b]==0)safe(a-1,b);
//  if(lst[a-1][b+1]==0)safe(a-1,b+1);
//  if(lst[a][b-1]==0)safe(a,b-1);
//  if(lst[a][b-1]==0)safe(a,b+1);
//  if(lst[a+1][b-1]==0)safe(a+1,b-1);
//  if(lst[a+1][b]==0)safe(a+1,b);
//  if(lst[a+1][b+1]==0)safe(a+1,b+1);
}
int main() {
    int n,m,x,a,b,cz,lose=0;
    cout<<"请输入棋盘的长宽和雷的数量(长宽均小于100)"<<endl;
    cin>>n>>m>>x;
    int cnt=n*m;
    if(m*n<=x) {
        cout<<"Error:雷数大于等于总方格数"<<endl;
        return 0;
    }
    for(int i=0; i<=n; i++) {
        for(int j=0; j<=m; j++) {
            lst[i][j]=0;
            cover[i][j]=1;
        }
    }
    for(int i=0; i<x; i++) {
        for(int j=0; j<1; j++) {
            cout<<"请输入第"<<i+1<<"颗雷的位置(行/列)"<<endl;
            cin>>a>>b;
            if(lst[a][b]==-1) {
                cout<<"Error:此处已有雷,请换个位置"<<endl;
                i--;
            }
        }
        lst[a][b]=-1;
    }
    for(int i=1; i<=n; i++) {
        for(int j=1; j<=m; j++) {
            if(lst[i][j]!=-1) {
                if(lst[i-1][j-1]==-1)lst[i][j]++;
                if(lst[i-1][j]==-1)lst[i][j]++;
                if(lst[i-1][j+1]==-1)lst[i][j]++;
                if(lst[i][j-1]==-1)lst[i][j]++;
                if(lst[i][j+1]==-1)lst[i][j]++;
                if(lst[i+1][j-1]==-1)lst[i][j]++;
                if(lst[i+1][j]==-1)lst[i][j]++;
                if(lst[i+1][j+1]==-1)lst[i][j]++;
            }
        }
    }
    cout<<"   ";
    for(int i=1; i<=n; i++) {
        cout<<i;
        if(i<10)cout<<" ";
    }
    cout<<endl<<endl;
    for(int i=1; i<=n; i++) {
        cout<<i;
        if(i<10)cout<<"  ";
        else cout<<" ";
        for(int j=1; j<=m; j++) {
            cout<<"? ";
        }
        cout<<endl;
    }
    while(lose==0) {
        cout<<"请输入操作(输入数字):"<<endl<<"1.翻看"<<endl<<"2.标记(标记为#)"<<endl<<"3.撤销标记"<<endl<<"输入其它数字均作为3处理"<<endl;
        cin>>cz;
        if(cz==1) {
            cout<<"请输入要翻看的格子(行/列)"<<endl;
            cin>>a>>b;
            if(cover[a][b]==0)cout<<"此处已翻开"<<endl;
            else if(cover[a][b]==2)cout<<"此处被标记"<<endl;
            else{
                cover[a][b]=0;
                if(lst[a][b]==0)safe(a,b);
                cnt--;
            }
        }
        else if(cz==2) {
            cout<<"请输入要标记的格子(行/列)"<<endl;
            cin>>a>>b;
            if(cover[a][b]==0)cout<<"此处已翻开"<<endl;
            if(cover[a][b]==2)cout<<"此处已标记"<<endl;
            else{
                cover[a][b]=2;
                cnt--;
            }
        }
        else {
            cout<<"请输入要撤销标记的格子(行/列)"<<endl;
            cin>>a>>b;
            if(cover[a][b]==0){
                cout<<"此处已翻开"<<endl; 
            }
            else if(cover[a][b]==1){
                cout<<"此处没有标记"<<endl;
            }
            else{
                cover[a][b]=1;
                cnt++;
            }
        }
        cout<<"   ";
        for(int i=1; i<=n; i++) {
            cout<<i;
            if(i<10)cout<<" ";
        }
        cout<<endl<<endl;
        for(int i=1; i<=n; i++) {
            cout<<i;
            if(i<10)cout<<"  ";
            else cout<<" ";
            for(int j=1; j<=m; j++) {
                if(cover[i][j]==0) {
                    if(lst[i][j]==-1) {
                        cout<<"! ";
                        lose=1;
                    } else if(lst[i][j]==0)cout<<"  ";
                    else cout<<lst[i][j]<<" ";
                } else if(cover[i][j]==2)cout<<"# ";
                else cout<<"? ";
            }
            cout<<endl;
        }
        if(lose==1) {
            cout<<"你 失 败 了";
            return 0;
        }
        if(cnt==0) {
            cout<<"你 赢 了";
            return 0;
        }
    }
}