C语言控制台实现自动扫雷程序

本文学习来自B站UP主まっぷたつ的个人空间-まっぷたつ个人主页-哔哩哔哩视频 (bilibili.com)icon-default.png?t=N7T8https://space.bilibili.com/620228教程链接

【从入门到精通】扫雷游戏的规则、定式、公式及复杂判雷技巧详解_哔哩哔哩_bilibiliicon-default.png?t=N7T8https://www.bilibili.com/video/BV1vz411z7t6/?spm_id_from=333.337.search-card.all.click

硬解代码,不建议装逼使用

#define ROW 30 // 雷区行列
#define COL 16
#define NUM 99 // 雷的个数

#define COUNT 100 // 程序运行多少次
#define NP_P 0 // 是否逐过程打印

这些参数可以自行调整

在Windows控制台运行需要做一些修改,可自行查阅解决

#Linux#

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <math.h>

#define ROW 30
#define COL 16
#define NUM 99

#define COUNT 100
#define NP_P 0

#define HID printf("\33[?25l");
#define WHI printf("\033[37m");
#define BLA printf("\033[30m");
#define CYA printf("\033[36m");
#define YEL printf("\033[33m");

int FAI = 0;
int SUC = 0;
int MinFlag = NUM;
int EmpFlag = ROW * COL;
int map[ROW + 2][COL + 2];
int IndexF[ROW + 2][COL + 2];
//
void Initmap();
int RandCho();
void CleZer(int, int);
int OneSet();
int TwoSet();
int ThrSet();
int IntersectionN(int[], int[], int, int);
int *IntersectionSet(int *, int[], int[], int, int, int);
bool isInArray(int, int[], int);
int *difference(int *, int[], int, int[], int, int);
void Printmap(int, int);
//
void Initmap()
{
    // 初始化
    for (int i = 0; i < ROW + 2; i++)
    {
        for (int j = 0; j < COL + 2; j++)
        {
            map[i][j] = 0;
            IndexF[i][j] = 1;
        }
    }
    // 埋雷
    int num = NUM;
    while (num)
    {
        int r = rand() % ROW + 1;
        int c = rand() % COL + 1;
        if (map[r][c] != -1)
        {
            map[r][c] = -1;
            num--;
        }
    }
    // 处理
    for (int i = 1; i <= ROW; i++)
    {
        for (int j = 1; j <= COL; j++)
        {
            if (map[i][j] != -1)
            {
                for (int x = i - 1; x <= i + 1; x++)
                {
                    for (int y = j - 1; y <= j + 1; y++)
                    {
                        if (map[x][y] == -1)
                        {
                            map[i][j]++;
                        }
                    }
                }
            }
        }
    }
    // 掩盖
    for (int i = 1; i <= ROW; i++)
    {
        for (int j = 1; j <= COL; j++)
        {
            map[i][j] += 10;
        }
    }
}

// 随机函数
int RandCho()
{
    int r = 1, c = 1, r_o = 1, c_o = 1;
    double Rate = (double)MinFlag / (double)EmpFlag;
    double Rate_N = (double)MinFlag / (double)EmpFlag;
    double Rate_O = (double)MinFlag / (double)EmpFlag;
    if (MinFlag == EmpFlag)
    {
        for (int i = 1; i <= ROW; i++)
        {
            for (int j = 1; j <= COL; j++)
            {
                if ((IndexF[i][j]) && (map[i][j] < 19 && map[i][j] > 8))
                {
                    MinFlag--;
                    EmpFlag--;
                    map[i][j] = 19;
                }
            }
        }
        Printmap(0, 0);
        printf("[清空]-游戏结束!\n");
        return 1;
    }
    for (int i = 1; i <= ROW; i++)
    {
        for (int j = 1; j <= COL; j++)
        {
            if ((IndexF[i][j]) && (map[i][j] < 9 && map[i][j] > 0))
            {
                int Mnum = 0, Nnum = 0, NnumM = 0;
                for (int x = i - 1; x <= i + 1; x++)
                {
                    for (int y = j - 1; y <= j + 1; y++)
                    {
                        if ((IndexF[x][y]) && (x > 0 && x <= ROW && y > 0 && y <= COL) && (map[x][y] < 19 && map[x][y] > 8))
                        {
                            Nnum++;
                            r_o = x;
                            c_o = y;
                        }
                        if (map[x][y] == 19)
                        {
                            Mnum++;
                        }
                    }
                }
                NnumM = map[i][j] - Mnum;
                if (Nnum != 0)
                {
                    Rate_O = (double)NnumM / (double)Nnum;
                    if (Rate_O < Rate_N)
                    {
                        Rate_N = Rate_O;
                        r = r_o;
                        c = c_o;
                    }
                }
            }
        }
    }
    if (Rate_N == Rate)
    {
        r = rand() % ROW + 1;
        c = rand() % COL + 1;
        while ((map[r][c] == 19 || map[r][c] < 9))
        {
            r = rand() % ROW + 1;
            c = rand() % COL + 1;
        }
    }
    map[r][c] -= 10;
    if (map[r][c] == 0)
    {
        CleZer(r, c);
        return 0;
    }
    else if (map[r][c] == -1)
    {
        MinFlag--;
        EmpFlag--;
        Printmap(r, c);
        printf("[爆炸]-游戏结束!\n");
        return 1;
    }
    else
    {
        EmpFlag--;
        return 0;
    }
}

// 清空连锁函数
void CleZer(int r, int c)
{
    EmpFlag--;
    IndexF[r][c] = 0;
    for (int x = r - 1; x <= r + 1; x++)
    {
        for (int y = c - 1; y <= c + 1; y++)
        {
            if ((x > 0 && x <= ROW && y > 0 && y <= COL) && (map[x][y] < 19 && map[x][y] > 8))
            {
                map[x][y] -= 10;
                if (map[x][y] == 0)
                {
                    CleZer(x, y);
                }
                else
                {
                    EmpFlag--;
                }
            }
        }
    }
}

// 单集合判雷
int OneSet()
{
    int flag = 0;
whi_A:
    for (int i = 1; i <= ROW; i++)
    {
        for (int j = 1; j <= COL; j++)
        {
            if ((IndexF[i][j]) && (map[i][j] < 9 && map[i][j] > 0))
            {
                int Mnum = 0, Nnum = 0;
                for (int x = i - 1; x <= i + 1; x++)
                {
                    for (int y = j - 1; y <= j + 1; y++)
                    {
                        if ((IndexF[x][y]) && (x > 0 && x <= ROW && y > 0 && y <= COL) && (map[x][y] < 19 && map[x][y] > 8))
                        {
                            Nnum++;
                        }
                        if (map[x][y] == 19)
                        {
                            Mnum++;
                        }
                    }
                }
                if (Nnum == 0)
                {
                    IndexF[i][j] = 0;
                }
                if (Mnum + Nnum == map[i][j] && Mnum != map[i][j])
                {
                    IndexF[i][j] = 0;
                    for (int x = i - 1; x <= i + 1; x++)
                    {
                        for (int y = j - 1; y <= j + 1; y++)
                        {
                            if ((IndexF[x][y]) && (x > 0 && x <= ROW && y > 0 && y <= COL) && (map[x][y] < 19 && map[x][y] > 8))
                            {
                                if (NP_P == 1)
                                {
                                    Printmap(x, y);
                                    system("clear");
                                }
                                IndexF[x][y] = 0;
                                MinFlag--;
                                EmpFlag--;
                                map[x][y] = 19;
                                if (NP_P == 1)
                                {
                                    Printmap(x, y);
                                    system("clear");
                                }
                            }
                        }
                    }
                    flag++;
                    goto whi_A;
                }
                else if (Mnum == map[i][j])
                {
                    IndexF[i][j] = 0;
                    for (int x = i - 1; x <= i + 1; x++)
                    {
                        for (int y = j - 1; y <= j + 1; y++)
                        {
                            if ((IndexF[x][y]) && (x > 0 && x <= ROW && y > 0 && y <= COL) && (map[x][y] < 19 && map[x][y] > 8))
                            {
                                if (NP_P == 1)
                                {
                                    Printmap(x, y);
                                    system("clear");
                                }
                                map[x][y] -= 10;
                                if (map[x][y] == 0)
                                {
                                    CleZer(x, y);
                                }
                                else
                                {
                                    EmpFlag--;
                                }
                                if (NP_P == 1)
                                {
                                    Printmap(x, y);
                                    system("clear");
                                }
                            }
                        }
                    }
                    flag++;
                    goto whi_A;
                }
            }
        }
    }
    return flag;
}

// 双集合判雷
int TwoSet()
{
    int flag = 0;
whi_B:
    for (int i = 1; i <= ROW; i++)
    {
        for (int j = 1; j <= COL; j++)
        {
            if ((IndexF[i][j]) && (map[i][j] < 9 && map[i][j] > 0))
            {
                for (int x = i - 2; x <= i + 2; x++)
                {
                    for (int y = j - 2; y <= j + 2; y++)
                    {
                        if ((IndexF[x][y]) && (x > 0 && x <= ROW && y > 0 && y <= COL) && (!(i == x && j == y)) && (map[x][y] < 9 && map[x][y] > 0))
                        {
                            int S_1_Mnum = 0, S_1_Nnum = 0, S_1_NnumM = 0;
                            int S_2_Mnum = 0, S_2_Nnum = 0, S_2_NnumM = 0;
                            int S_1 = 0, S_2 = 0;
                            int Set_1[8], Set_2[8];
                            for (int _i = i - 1; _i <= i + 1; _i++)
                            {
                                for (int _j = j - 1; _j <= j + 1; _j++)
                                {
                                    if ((IndexF[_i][_j]) && (_i > 0 && _i <= ROW && _j > 0 && _j <= COL) && (map[_i][_j] < 19 && map[_i][_j] > 8))
                                    {
                                        if (ROW > COL)
                                        {
                                            Set_1[S_1++] = _i * (ROW + 2) + _j;
                                        }
                                        else
                                        {
                                            Set_1[S_1++] = _j * (COL + 2) + _i;
                                        }
                                        S_1_Nnum++;
                                    }
                                    if ((_i > 0 && _i <= ROW && _j > 0 && _j <= COL) && (map[_i][_j] == 19))
                                    {
                                        S_1_Mnum++;
                                    }
                                }
                            }
                            if (S_1_Nnum == 0)
                            {
                                IndexF[i][j] = 0;
                            }
                            S_1_NnumM = map[i][j] - S_1_Mnum;
                            for (int _i = x - 1; _i <= x + 1; _i++)
                            {
                                for (int _j = y - 1; _j <= y + 1; _j++)
                                {
                                    if ((IndexF[_i][_j]) && (_i > 0 && _i <= ROW && _j > 0 && _j <= COL) && (map[_i][_j] < 19 && map[_i][_j] > 8))
                                    {
                                        if (ROW > COL)
                                        {
                                            Set_2[S_2++] = _i * (ROW + 2) + _j;
                                        }
                                        else
                                        {
                                            Set_2[S_2++] = _j * (COL + 2) + _i;
                                        }
                                        S_2_Nnum++;
                                    }
                                    if ((_i > 0 && _i <= ROW && _j > 0 && _j <= COL) && (map[_i][_j] == 19))
                                    {
                                        S_2_Mnum++;
                                    }
                                }
                            }
                            if (S_2_Nnum == 0)
                            {
                                IndexF[x][y] = 0;
                            }
                            S_2_NnumM = map[x][y] - S_2_Mnum;
                            int INnum = IntersectionN(Set_2, Set_1, S_2, S_1);
                            if (INnum)
                            {
                                int a_I = S_1_NnumM, b_I = S_2_NnumM, x_I = S_1_Nnum - INnum, y_I = INnum, z_I = S_2_Nnum - INnum;
                                int S_difN = x_I + z_I;
                                int *result = NULL;
                                result = difference(result, Set_1, S_1, Set_2, S_2, S_difN);
                                if ((((a_I >= b_I && b_I > 0) && (a_I - b_I == x_I)) || ((b_I >= a_I && a_I > 0) && (b_I - a_I == z_I))) && ((x_I == 0 && z_I != 0) || (x_I != 0 && z_I == 0)))
                                {
                                    for (int SD_i = 0; SD_i < S_difN; SD_i++)
                                    {
                                        int SD_R = 0, SD_C = 0;
                                        if (ROW > COL)
                                        {
                                            SD_R = result[SD_i] / (ROW + 2);
                                            SD_C = result[SD_i] % (ROW + 2);
                                        }
                                        else
                                        {
                                            SD_R = result[SD_i] % (COL + 2);
                                            SD_C = result[SD_i] / (COL + 2);
                                        }
                                        int dif_r = 0, dif_c = 0;
                                        if (a_I > b_I)
                                        {
                                            dif_r = i;
                                            dif_c = j;
                                        }
                                        if (a_I < b_I)
                                        {
                                            dif_r = x;
                                            dif_c = y;
                                        }
                                        if (a_I == b_I)
                                        {
                                            if (x_I > z_I)
                                            {
                                                dif_r = x;
                                                dif_c = y;
                                            }
                                            else
                                            {
                                                dif_r = i;
                                                dif_c = j;
                                            }
                                        }
                                        if ((abs(SD_R - dif_r) >= 0 && abs(SD_R - dif_r) <= 1) && (abs(SD_C - dif_c) >= 0 && abs(SD_C - dif_c) <= 1))
                                        {
                                            if (map[SD_R][SD_C] < 19 && map[SD_R][SD_C] > 8)
                                            {
                                                if (NP_P == 1)
                                                {
                                                    Printmap(SD_R, SD_C);
                                                    system("clear");
                                                }
                                                IndexF[SD_R][SD_C] = 0;
                                                MinFlag--;
                                                EmpFlag--;
                                                map[SD_R][SD_C] = 19;
                                                if (NP_P == 1)
                                                {
                                                    Printmap(SD_R, SD_C);
                                                    system("clear");
                                                }
                                            }
                                        }
                                        else
                                        {
                                            if (map[SD_R][SD_C] < 19 && map[SD_R][SD_C] > 8)
                                            {
                                                if (NP_P == 1)
                                                {
                                                    Printmap(SD_R, SD_C);
                                                    system("clear");
                                                }
                                                map[SD_R][SD_C] -= 10;
                                                if (map[SD_R][SD_C] == 0)
                                                {
                                                    CleZer(SD_R, SD_C);
                                                }
                                                else
                                                {
                                                    EmpFlag--;
                                                }
                                                if (NP_P == 1)
                                                {
                                                    Printmap(SD_R, SD_C);
                                                    system("clear");
                                                }
                                            }
                                        }
                                    }
                                    flag++;
                                    goto whi_B;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return flag;
}

// 三集合判雷
int ThrSet()
{
    int flag = 0;
whi_C:
    for (int i = 1; i <= ROW; i++)
    {
        for (int j = 1; j <= COL; j++)
        {
            if ((IndexF[i][j]) && (map[i][j] < 9 && map[i][j] > 0))
            {
                for (int x = i - 2; x <= i + 2; x++)
                {
                    for (int y = j - 2; y <= j + 2; y++)
                    {
                        if ((IndexF[x][y]) && (x > 0 && x <= ROW && y > 0 && y <= COL) && (!(i == x && j == y)) && (map[x][y] < 9 && map[x][y] > 0))
                        {
                            for (int r = i - 2; r <= i + 2; r++)
                            {
                                for (int c = j - 2; c <= j + 2; c++)
                                {
                                    if ((IndexF[r][c]) && (r > 0 && c <= ROW && r > 0 && c <= COL) && (!(r == x && c == y)) && (!(r == i && c == j)) && (map[r][c] < 9 && map[r][c] > 0))
                                    {
                                        int S_1_Mnum = 0, S_1_Nnum = 0, S_1_NnumM = 0;
                                        int S_2_Mnum = 0, S_2_Nnum = 0, S_2_NnumM = 0;
                                        int S_3_Mnum = 0, S_3_Nnum = 0, S_3_NnumM = 0;
                                        int S_1 = 0, S_2 = 0, S_3 = 0;
                                        int Set_1[8], Set_2[8], Set_3[8];
                                        for (int _i = 0; _i < 8; _i++)
                                        {
                                            Set_1[_i] = 0;
                                            Set_2[_i] = 0;
                                            Set_3[_i] = 0;
                                        }
                                        for (int i_i = i - 1; i_i <= i + 1; i_i++)
                                        {
                                            for (int j_i = j - 1; j_i <= j + 1; j_i++)
                                            {
                                                if ((IndexF[i_i][j_i]) && (map[i_i][j_i] < 19 && map[i_i][j_i] > 8))
                                                {
                                                    if (ROW > COL)
                                                    {
                                                        Set_1[S_1++] = i_i * (ROW + 2) + j_i;
                                                    }
                                                    else
                                                    {
                                                        Set_1[S_1++] = j_i * (COL + 2) + i_i;
                                                    }
                                                    S_1_Nnum++;
                                                }
                                                if (map[i_i][j_i] == 19)
                                                {
                                                    S_1_Mnum++;
                                                }
                                            }
                                        }
                                        S_1_NnumM = map[i][j] - S_1_Mnum;
                                        for (int x_i = x - 1; x_i <= x + 1; x_i++)
                                        {
                                            for (int y_i = y - 1; y_i <= y + 1; y_i++)
                                            {
                                                if ((IndexF[x_i][y_i]) && (map[x_i][y_i] < 19 && map[x_i][y_i] > 8))
                                                {
                                                    if (ROW > COL)
                                                    {
                                                        Set_2[S_2++] = x_i * (ROW + 2) + y_i;
                                                    }
                                                    else
                                                    {
                                                        Set_2[S_2++] = y_i * (COL + 2) + x_i;
                                                    }
                                                    S_2_Nnum++;
                                                }
                                                if (map[x_i][y_i] == 19)
                                                {
                                                    S_2_Mnum++;
                                                }
                                            }
                                        }
                                        S_2_NnumM = map[x][y] - S_2_Mnum;
                                        for (int r_i = r - 1; r_i <= r + 1; r_i++)
                                        {
                                            for (int c_i = c - 1; c_i <= c + 1; c_i++)
                                            {
                                                if ((IndexF[r_i][c_i]) && (map[r_i][c_i] < 19 && map[r_i][c_i] > 8))
                                                {
                                                    if (ROW > COL)
                                                    {
                                                        Set_3[S_3++] = r_i * (ROW + 2) + c_i;
                                                    }
                                                    else
                                                    {
                                                        Set_3[S_3++] = c_i * (COL + 2) + r_i;
                                                    }
                                                    S_3_Nnum++;
                                                }
                                                if (map[r_i][c_i] == 19)
                                                {
                                                    S_3_Mnum++;
                                                }
                                            }
                                        }
                                        S_3_NnumM = map[r][c] - S_3_Mnum;
                                        int INnum_2_1 = IntersectionN(Set_2, Set_1, S_2, S_1);
                                        int INnum_3_1 = IntersectionN(Set_3, Set_1, S_3, S_1);
                                        int INnum_3_2 = IntersectionN(Set_3, Set_2, S_3, S_2);
                                        if (INnum_2_1 != 0 && INnum_3_1 != 0 && INnum_3_2 == 0) // A
                                        {
                                            int a_I = S_2_NnumM, b_I = S_1_NnumM, c_I = S_3_NnumM, v_I = S_2_Nnum - INnum_2_1, w_I = INnum_2_1, x_I = S_1_Nnum - INnum_2_1 - INnum_3_1, y_I = INnum_3_1, z_I = S_3_Nnum - INnum_3_1;
                                            if (((b_I >= a_I + c_I && a_I + c_I > 0) && (b_I - a_I - c_I == x_I) || ((a_I + c_I >= b_I && b_I > 0) && (a_I + c_I - b_I == v_I + z_I))) && (v_I + z_I != 0))
                                            {
                                                int S_2_1_difN = v_I + x_I + y_I;
                                                int S_3_1_difN = w_I + x_I + z_I;
                                                int *result_2_1 = NULL;
                                                int *result_3_1 = NULL;
                                                int result_2_1_x_i = 0;
                                                int result_3_1_r_i = 0;
                                                int result_2_1_i_i = 0;
                                                int result_3_1_i_i = 0;
                                                int result_2_1_x[8];
                                                int result_3_1_r[8];
                                                int result_2_1_i[8];
                                                int result_3_1_i[8];
                                                int *result_x = NULL;
                                                for (int _i = 0; _i < 8; _i++)
                                                {
                                                    result_2_1_x[_i] = 0;
                                                    result_3_1_r[_i] = 0;
                                                    result_2_1_i[_i] = 0;
                                                    result_3_1_i[_i] = 0;
                                                }
                                                result_2_1 = difference(result_2_1, Set_1, S_1, Set_2, S_2, S_2_1_difN);
                                                for (int res = 0; res < S_2_1_difN; res++)
                                                {
                                                    int res_i = 0, res_j = 0;
                                                    if (ROW > COL)
                                                    {
                                                        res_i = result_2_1[res] / (ROW + 2);
                                                        res_j = result_2_1[res] % (ROW + 2);
                                                    }
                                                    else
                                                    {
                                                        res_i = result_2_1[res] % (COL + 2);
                                                        res_j = result_2_1[res] / (COL + 2);
                                                    }
                                                    if ((abs(res_i - x) >= 0 && abs(res_i - x) <= 1) && (abs(res_j - y) >= 0 && abs(res_j - y) <= 1))
                                                    {
                                                        result_2_1_x[result_2_1_x_i++] = result_2_1[res];
                                                    }
                                                    if ((abs(res_i - i) >= 0 && abs(res_i - i) <= 1) && (abs(res_j - j) >= 0 && abs(res_j - j) <= 1))
                                                    {
                                                        result_2_1_i[result_2_1_i_i++] = result_2_1[res];
                                                    }
                                                }
                                                result_3_1 = difference(result_3_1, Set_1, S_1, Set_3, S_3, S_3_1_difN);
                                                for (int res = 0; res < S_3_1_difN; res++)
                                                {
                                                    int res_i = 0, res_j = 0;
                                                    if (ROW > COL)
                                                    {
                                                        res_i = result_3_1[res] / (ROW + 2);
                                                        res_j = result_3_1[res] % (ROW + 2);
                                                    }
                                                    else
                                                    {
                                                        res_i = result_3_1[res] % (COL + 2);
                                                        res_j = result_3_1[res] / (COL + 2);
                                                    }
                                                    if ((abs(res_i - r) >= 0 && abs(res_i - r) <= 1) && (abs(res_j - c) >= 0 && abs(res_j - c) <= 1))
                                                    {
                                                        result_3_1_r[result_3_1_r_i++] = result_3_1[res];
                                                    }
                                                    if ((abs(res_i - i) >= 0 && abs(res_i - i) <= 1) && (abs(res_j - j) >= 0 && abs(res_j - j) <= 1))
                                                    {
                                                        result_3_1_i[result_3_1_i_i++] = result_3_1[res];
                                                    }
                                                }
                                                result_x = IntersectionSet(result_x, result_2_1_i, result_3_1_i, result_2_1_i_i, result_3_1_i_i, x_I);
                                                for (int res = 0; res < x_I; res++)
                                                {
                                                    int res_i = 0, res_j = 0;
                                                    if (ROW > COL)
                                                    {
                                                        res_i = result_x[res] / (ROW + 2);
                                                        res_j = result_x[res] % (ROW + 2);
                                                    }
                                                    else
                                                    {
                                                        res_i = result_x[res] % (COL + 2);
                                                        res_j = result_x[res] / (COL + 2);
                                                    }
                                                    if (b_I > a_I + c_I)
                                                    {
                                                        if (map[res_i][res_j] < 19 && map[res_i][res_j] > 8)
                                                        {
                                                            if (NP_P == 1)
                                                            {
                                                                Printmap(res_i, res_j);
                                                                system("clear");
                                                            }
                                                            IndexF[res_i][res_j] = 0;
                                                            MinFlag--;
                                                            EmpFlag--;
                                                            map[res_i][res_j] = 19;
                                                            if (NP_P == 1)
                                                            {
                                                                Printmap(res_i, res_j);
                                                                system("clear");
                                                            }
                                                        }
                                                    }
                                                    if (b_I < a_I + c_I)
                                                    {
                                                        if (map[res_i][res_j] < 19 && map[res_i][res_j] > 8)
                                                        {
                                                            if (NP_P == 1)
                                                            {
                                                                Printmap(res_i, res_j);
                                                                system("clear");
                                                            }
                                                            map[res_i][res_j] -= 10;
                                                            if (map[res_i][res_j] == 0)
                                                            {
                                                                CleZer(res_i, res_j);
                                                            }
                                                            else
                                                            {
                                                                EmpFlag--;
                                                            }
                                                            if (NP_P == 1)
                                                            {
                                                                Printmap(res_i, res_j);
                                                                system("clear");
                                                            }
                                                        }
                                                    }
                                                    flag++;
                                                }
                                                for (int res = 0; res < result_2_1_x_i; res++)
                                                {
                                                    int res_i = 0, res_j = 0;
                                                    if (ROW > COL)
                                                    {
                                                        res_i = result_2_1_x[res] / (ROW + 2);
                                                        res_j = result_2_1_x[res] % (ROW + 2);
                                                    }
                                                    else
                                                    {
                                                        res_i = result_2_1_x[res] % (COL + 2);
                                                        res_j = result_2_1_x[res] / (COL + 2);
                                                    }
                                                    if (b_I >= a_I + c_I)
                                                    {
                                                        if (map[res_i][res_j] < 19 && map[res_i][res_j] > 8)
                                                        {
                                                            if (NP_P == 1)
                                                            {
                                                                Printmap(res_i, res_j);
                                                                system("clear");
                                                            }
                                                            map[res_i][res_j] -= 10;
                                                            if (map[res_i][res_j] == 0)
                                                            {
                                                                CleZer(res_i, res_j);
                                                            }
                                                            else
                                                            {
                                                                EmpFlag--;
                                                            }
                                                            if (NP_P == 1)
                                                            {
                                                                Printmap(res_i, res_j);
                                                                system("clear");
                                                            }
                                                        }
                                                    }
                                                    if (b_I < a_I + c_I)
                                                    {
                                                        if (map[res_i][res_j] < 19 && map[res_i][res_j] > 8)
                                                        {
                                                            if (NP_P == 1)
                                                            {
                                                                Printmap(res_i, res_j);
                                                                system("clear");
                                                            }
                                                            IndexF[res_i][res_j] = 0;
                                                            MinFlag--;
                                                            EmpFlag--;
                                                            map[res_i][res_j] = 19;
                                                            if (NP_P == 1)
                                                            {
                                                                Printmap(res_i, res_j);
                                                                system("clear");
                                                            }
                                                        }
                                                    }
                                                    flag++;
                                                }
                                                for (int res = 0; res < result_3_1_r_i; res++)
                                                {
                                                    int res_i = 0, res_j = 0;
                                                    if (ROW > COL)
                                                    {
                                                        res_i = result_3_1_r[res] / (ROW + 2);
                                                        res_j = result_3_1_r[res] % (ROW + 2);
                                                    }
                                                    else
                                                    {
                                                        res_i = result_3_1_r[res] % (COL + 2);
                                                        res_j = result_3_1_r[res] / (COL + 2);
                                                    }
                                                    if (b_I >= a_I + c_I)
                                                    {
                                                        if (map[res_i][res_j] < 19 && map[res_i][res_j] > 8)
                                                        {
                                                            if (NP_P == 1)
                                                            {
                                                                Printmap(res_i, res_j);
                                                                system("clear");
                                                            }
                                                            map[res_i][res_j] -= 10;
                                                            if (map[res_i][res_j] == 0)
                                                            {
                                                                CleZer(res_i, res_j);
                                                            }
                                                            else
                                                            {
                                                                EmpFlag--;
                                                            }
                                                            if (NP_P == 1)
                                                            {
                                                                Printmap(res_i, res_j);
                                                                system("clear");
                                                            }
                                                        }
                                                    }
                                                    if (b_I < a_I + c_I)
                                                    {
                                                        if (map[res_i][res_j] < 19 && map[res_i][res_j] > 8)
                                                        {
                                                            if (NP_P == 1)
                                                            {
                                                                Printmap(res_i, res_j);
                                                                system("clear");
                                                            }
                                                            IndexF[res_i][res_j] = 0;
                                                            MinFlag--;
                                                            EmpFlag--;
                                                            map[res_i][res_j] = 19;
                                                            if (NP_P == 1)
                                                            {
                                                                Printmap(res_i, res_j);
                                                                system("clear");
                                                            }
                                                        }
                                                    }
                                                    flag++;
                                                }
                                                goto whi_C;
                                            }
                                        }
                                        else if (INnum_2_1 != 0 && INnum_3_1 != 0 && INnum_3_2 != 0) // B
                                        {
                                            int *result_3_2 = NULL;
                                            result_3_2 = IntersectionSet(result_3_2, Set_3, Set_2, S_3, S_2, INnum_3_2);
                                            int IS_B = IntersectionN(result_3_2, Set_1, INnum_3_2, S_1);
                                            if (IS_B == INnum_3_2 && IS_B < S_1_Nnum && INnum_2_1 + INnum_3_1 > S_1_Nnum)
                                            {
                                                int a_I = S_2_NnumM, b_I = S_1_NnumM, c_I = S_3_NnumM, v_I = S_2_Nnum - INnum_2_1, w_I = INnum_2_1 - INnum_3_2, x_I = INnum_3_2, y_I = INnum_3_1 - INnum_3_2, z_I = S_3_Nnum - INnum_3_1;
                                                if (w_I != 0 && y_I != 0 && v_I != 0 && z_I != 0)
                                                {
                                                    int *result_2_1 = NULL;
                                                    int *result_3_1 = NULL;
                                                    int result_2_1_x[8];
                                                    int result_3_1_r[8];
                                                    int result_2_1_x_i = 0;
                                                    int result_3_1_r_i = 0;
                                                    for (int _i = 0; _i < 8; _i++)
                                                    {
                                                        result_2_1_x[_i] = 0;
                                                        result_3_1_r[_i] = 0;
                                                    }
                                                    result_2_1 = difference(result_2_1, Set_1, S_1, Set_2, S_2, INnum_2_1);
                                                    for (int res = 0; res < INnum_2_1; res++)
                                                    {
                                                        int res_i = 0, res_j = 0;
                                                        if (ROW > COL)
                                                        {
                                                            res_i = result_2_1[res] / (ROW + 2);
                                                            res_j = result_2_1[res] % (ROW + 2);
                                                        }
                                                        else
                                                        {
                                                            res_i = result_2_1[res] % (COL + 2);
                                                            res_j = result_2_1[res] / (COL + 2);
                                                        }
                                                        if ((abs(res_i - x) >= 0 && abs(res_i - x) <= 1) && (abs(res_j - y) >= 0 && abs(res_j - y) <= 1))
                                                        {
                                                            result_2_1_x[result_2_1_x_i++] = result_2_1[res];
                                                        }
                                                    }
                                                    result_3_1 = difference(result_3_1, Set_1, S_1, Set_3, S_3, INnum_3_1);
                                                    for (int res = 0; res < INnum_3_1; res++)
                                                    {
                                                        int res_i = 0, res_j = 0;
                                                        if (ROW > COL)
                                                        {
                                                            res_i = result_3_1[res] / (ROW + 2);
                                                            res_j = result_3_1[res] % (ROW + 2);
                                                        }
                                                        else
                                                        {
                                                            res_i = result_3_1[res] % (COL + 2);
                                                            res_j = result_3_1[res] / (COL + 2);
                                                        }
                                                        if ((abs(res_i - r) >= 0 && abs(res_i - r) <= 1) && (abs(res_j - c) >= 0 && abs(res_j - c) <= 1))
                                                        {
                                                            result_3_1_r[result_3_1_r_i++] = result_3_1[res];
                                                        }
                                                    }
                                                    if (b_I == a_I + c_I)
                                                    {
                                                        for (int res = 0; res < result_2_1_x_i; res++)
                                                        {
                                                            int res_i = 0, res_j = 0;
                                                            if (ROW > COL)
                                                            {
                                                                res_i = result_2_1_x[res] / (ROW + 2);
                                                                res_j = result_2_1_x[res] % (ROW + 2);
                                                            }
                                                            else
                                                            {
                                                                res_i = result_2_1_x[res] % (COL + 2);
                                                                res_j = result_2_1_x[res] / (COL + 2);
                                                            }
                                                            if (map[res_i][res_j] < 19 && map[res_i][res_j] > 8)
                                                            {
                                                                if (NP_P == 1)
                                                                {
                                                                    Printmap(res_i, res_j);
                                                                    system("clear");
                                                                }
                                                                map[res_i][res_j] -= 10;
                                                                if (map[res_i][res_j] == 0)
                                                                {
                                                                    CleZer(res_i, res_j);
                                                                }
                                                                else
                                                                {
                                                                    EmpFlag--;
                                                                }
                                                                if (NP_P == 1)
                                                                {
                                                                    Printmap(res_i, res_j);
                                                                    system("clear");
                                                                }
                                                            }
                                                            flag++;
                                                        }
                                                        for (int res = 0; res < result_3_1_r_i; res++)
                                                        {
                                                            int res_i = 0, res_j = 0;
                                                            if (ROW > COL)
                                                            {
                                                                res_i = result_3_1_r[res] / (ROW + 2);
                                                                res_j = result_3_1_r[res] % (ROW + 2);
                                                            }
                                                            else
                                                            {
                                                                res_i = result_3_1_r[res] % (COL + 2);
                                                                res_j = result_3_1_r[res] / (COL + 2);
                                                            }
                                                            if (map[res_i][res_j] < 19 && map[res_i][res_j] > 8)
                                                            {
                                                                if (NP_P == 1)
                                                                {
                                                                    Printmap(res_i, res_j);
                                                                    system("clear");
                                                                }
                                                                map[res_i][res_j] -= 10;
                                                                if (map[res_i][res_j] == 0)
                                                                {
                                                                    CleZer(res_i, res_j);
                                                                }
                                                                else
                                                                {
                                                                    EmpFlag--;
                                                                }
                                                                if (NP_P == 1)
                                                                {
                                                                    Printmap(res_i, res_j);
                                                                    system("clear");
                                                                }
                                                            }
                                                            flag++;
                                                        }
                                                        for (int res = 0; res < INnum_3_2; res++)
                                                        {
                                                            int res_i = 0, res_j = 0;
                                                            if (ROW > COL)
                                                            {
                                                                res_i = result_3_2[res] / (ROW + 2);
                                                                res_j = result_3_2[res] % (ROW + 2);
                                                            }
                                                            else
                                                            {
                                                                res_i = result_3_2[res] % (COL + 2);
                                                                res_j = result_3_2[res] / (COL + 2);
                                                            }
                                                            if (map[res_i][res_j] < 19 && map[res_i][res_j] > 8)
                                                            {
                                                                if (NP_P == 1)
                                                                {
                                                                    Printmap(res_i, res_j);
                                                                    system("clear");
                                                                }
                                                                map[res_i][res_j] -= 10;
                                                                if (map[res_i][res_j] == 0)
                                                                {
                                                                    CleZer(res_i, res_j);
                                                                }
                                                                else
                                                                {
                                                                    EmpFlag--;
                                                                }
                                                                if (NP_P == 1)
                                                                {
                                                                    Printmap(res_i, res_j);
                                                                    system("clear");
                                                                }
                                                            }
                                                            flag++;
                                                        }
                                                        goto whi_C;
                                                    }
                                                    if (a_I + c_I - b_I == v_I + x_I + z_I)
                                                    {
                                                        for (int res = 0; res < result_2_1_x_i; res++)
                                                        {
                                                            int res_i = 0, res_j = 0;
                                                            if (ROW > COL)
                                                            {
                                                                res_i = result_2_1_x[res] / (ROW + 2);
                                                                res_j = result_2_1_x[res] % (ROW + 2);
                                                            }
                                                            else
                                                            {
                                                                res_i = result_2_1_x[res] % (COL + 2);
                                                                res_j = result_2_1_x[res] / (COL + 2);
                                                            }
                                                            if (map[res_i][res_j] < 19 && map[res_i][res_j] > 8)
                                                            {
                                                                if (NP_P == 1)
                                                                {
                                                                    Printmap(res_i, res_j);
                                                                    system("clear");
                                                                }
                                                                IndexF[res_i][res_j] = 0;
                                                                MinFlag--;
                                                                EmpFlag--;
                                                                map[res_i][res_j] = 19;
                                                                if (NP_P == 1)
                                                                {
                                                                    Printmap(res_i, res_j);
                                                                    system("clear");
                                                                }
                                                            }
                                                            flag++;
                                                        }
                                                        for (int res = 0; res < result_3_1_r_i; res++)
                                                        {
                                                            int res_i = 0, res_j = 0;
                                                            if (ROW > COL)
                                                            {
                                                                res_i = result_3_1_r[res] / (ROW + 2);
                                                                res_j = result_3_1_r[res] % (ROW + 2);
                                                            }
                                                            else
                                                            {
                                                                res_i = result_3_1_r[res] % (COL + 2);
                                                                res_j = result_3_1_r[res] / (COL + 2);
                                                            }
                                                            if (map[res_i][res_j] < 19 && map[res_i][res_j] > 8)
                                                            {
                                                                if (NP_P == 1)
                                                                {
                                                                    Printmap(res_i, res_j);
                                                                    system("clear");
                                                                }
                                                                IndexF[res_i][res_j] = 0;
                                                                MinFlag--;
                                                                EmpFlag--;
                                                                map[res_i][res_j] = 19;
                                                                if (NP_P == 1)
                                                                {
                                                                    Printmap(res_i, res_j);
                                                                    system("clear");
                                                                }
                                                            }
                                                            flag++;
                                                        }
                                                        for (int res = 0; res < INnum_3_2; res++)
                                                        {
                                                            int res_i = 0, res_j = 0;
                                                            if (ROW > COL)
                                                            {
                                                                res_i = result_3_2[res] / (ROW + 2);
                                                                res_j = result_3_2[res] % (ROW + 2);
                                                            }
                                                            else
                                                            {
                                                                res_i = result_3_2[res] % (COL + 2);
                                                                res_j = result_3_2[res] / (COL + 2);
                                                            }
                                                            if (map[res_i][res_j] < 19 && map[res_i][res_j] > 8)
                                                            {
                                                                if (NP_P == 1)
                                                                {
                                                                    Printmap(res_i, res_j);
                                                                    system("clear");
                                                                }
                                                                IndexF[res_i][res_j] = 0;
                                                                MinFlag--;
                                                                EmpFlag--;
                                                                map[res_i][res_j] = 19;
                                                                if (NP_P == 1)
                                                                {
                                                                    Printmap(res_i, res_j);
                                                                    system("clear");
                                                                }
                                                            }
                                                            flag++;
                                                        }
                                                        goto whi_C;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return flag;
}

// 交集个数
int IntersectionN(int arr1[], int arr2[], int m, int n)
{
    int i, j, num = 0;
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < m; j++)
        {
            if (arr2[i] == arr1[j])
                num++;
        }
    }
    if (num == 0)
    {
        return 0;
    }
    else
    {
        return num;
    }
}

// 交集集合
int *IntersectionSet(int *result, int arr1[], int arr2[], int m, int n, int lenRes)
{
    int i, j, x = 0;
    result = (int *)malloc((lenRes) * sizeof(int));
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < m; j++)
        {
            if (arr2[i] == arr1[j])
                result[x++] = arr2[i];
        }
    }
    if (lenRes == 0)
    {
        return NULL;
    }
    else
    {
        return result;
    }
}

// IsInArray[-.-]
bool isInArray(int num, int array[], int len)
{
    for (int i = 0; i < len; i++)
    {
        if (array[i] == num)
        {
            return true;
        }
    }
    return false;
}

// 双集合非交集区域
int *difference(int *result, int A[], int lenA, int B[], int lenB, int lenRes)
{
    result = (int *)malloc((lenRes) * sizeof(int));
    int x = 0;
    for (int i = 0; i < lenA; i++)
    {
        if (!isInArray(A[i], B, lenB))
        {
            result[x++] = A[i];
        }
    }
    for (int j = 0; j < lenB; j++)
    {
        if (!isInArray(B[j], A, lenA))
        {
            result[x++] = B[j];
        }
    }
    return result;
}

// 打印
void Printmap(int m, int n)
{
    double Rate = (double)SUC / (double)(SUC + FAI);
    printf("   [胜利局数]:{%d} [失败局数]:{%d} [Rate]:{%.2lf}\n", SUC, FAI, Rate);
    Rate = (double)MinFlag / (double)EmpFlag;
    printf("   [剩余雷数]:{%d} [剩余格数]:{%d} [Rate]:{%.2lf}\n", MinFlag, EmpFlag, Rate);
    printf("   ");
    for (int r = 1; r <= COL; r++)
    {
        printf("[%d]", r % 10);
    }
    printf("\n");
    for (int i = 1; i <= ROW; i++)
    {
        if (n == 1 && i == m)
        {
            printf("[%d]", i % 10);
        }
        else
        {
            printf("[%d] ", i % 10);
        }
        for (int j = 1; j <= COL; j++)
        {
            if ((i == m) && (j == n))
            {
                YEL if (n == 1)
                {
                    printf("[");
                }
                else
                {
                    printf(" [");
                }
                if (map[i][j] == -1)
                {
                    printf("@");
                }
                if (map[i][j] < 9 && map[i][j] > -1)
                {
                    printf("%d", map[i][j]);
                }
                if (map[i][j] < 19 && map[i][j] > 8)
                {
                    printf("#");
                }
                if (map[i][j] == 19)
                {
                    printf("$");
                }
                printf("] ");
                WHI
            }
            else if ((i == m) && (j == (n - 1)))
            {
                if (map[i][j] == -1)
                {
                    YEL
                        printf("@");
                    WHI
                }
                if (map[i][j] < 9 && map[i][j] > -1)
                {
                    if (map[i][j] == 0)
                    {
                        BLA
                    }
                    printf("%d", map[i][j]);
                    WHI
                }
                if (map[i][j] < 19 && map[i][j] > 8)
                {
                    printf("#");
                }
                if (map[i][j] == 19)
                {
                    CYA
                        printf("$");
                    WHI
                }
            }
            else
            {
                if (map[i][j] == -1)
                {
                    YEL
                        printf("@  ");
                    WHI
                }
                if (map[i][j] < 9 && map[i][j] > -1)
                {
                    if (map[i][j] == 0)
                    {
                        BLA
                    }
                    printf("%d  ", map[i][j]);
                    WHI
                }
                if (map[i][j] < 19 && map[i][j] > 8)
                {
                    printf("#  ");
                }
                if (map[i][j] == 19)
                {
                    CYA
                        printf("$  ");
                    WHI
                }
            }
        }
        printf("\n");
    }
}

// 主函数
int main()
{
    int _Count = 0;
NEXT:
    HID if (_Count == COUNT)
    {
        goto OVER;
    }
    MinFlag = NUM;
    EmpFlag = ROW * COL;
    srand((unsigned)time(NULL));
    Initmap();
whi:
    if (RandCho())
    {
        FAI++;
        // int next;
        // scanf("%d", &next);
        sleep(1);
        _Count++;
        system("clear");
        goto NEXT;
    }
    do
    {
        int flag_One = 0, flag_Two = 0, flag_Thr = 0;
        flag_One = OneSet();
        flag_Two = TwoSet();
        flag_Thr = ThrSet();
        if (MinFlag == 0)
        {
            if (EmpFlag != 0)
            {
                for (int i = 1; i <= ROW; i++)
                {
                    for (int j = 1; j <= COL; j++)
                    {
                        if ((IndexF[i][j]) && (map[i][j] < 19 && map[i][j] > 8))
                        {
                            EmpFlag--;
                            map[i][j] -= 10;
                        }
                    }
                }
            }
            Printmap(0, 0);
            printf("[清空]-游戏结束!\n");
            SUC++;
            // int next;
            // scanf("%d", &next);
            sleep(1);
            _Count++;
            system("clear");
            goto NEXT;
        }
        if (flag_One == 0 && flag_Two == 0 && flag_Thr == 0)
        {
            goto whi;
        }
    } while (1);
OVER:
    Printmap(0, 0);
    int sure;
    do
    {
        printf("确认结果请输入:0\n");
        scanf("%d", &sure);
    } while (sure);
    return 0;
}

// --- Start ---

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <math.h>

#define ROW 30
#define COL 16
#define NUM 99

#define COUNT 100
#define NP_P 0

#define HID printf("\33[?25l");
#define WHI printf("\033[37m");
#define BLA printf("\033[30m");
#define CYA printf("\033[36m");
#define YEL printf("\033[33m");

int FAI = 0;
int SUC = 0;
int MinFlag = NUM;
int EmpFlag = ROW * COL;
int map[ROW + 2][COL + 2];
int IndexF[ROW + 2][COL + 2];
//
void Initmap();
int RandCho();
void CleZer(int, int);
int OneSet();
int TwoSet();
int ThrSet();
int IntersectionN(int[], int[], int, int);
int *IntersectionSet(int *, int[], int[], int, int, int);
bool isInArray(int, int[], int);
int *difference(int *, int[], int, int[], int, int);
void Printmap(int, int);
//
void Initmap()
{
    // 初始化
    for (int i = 0; i < ROW + 2; i++)
    {
        for (int j = 0; j < COL + 2; j++)
        {
            map[i][j] = 0;
            IndexF[i][j] = 1;
        }
    }
    // 埋雷
    int num = NUM;
    while (num)
    {
        int r = rand() % ROW + 1;
        int c = rand() % COL + 1;
        if (map[r][c] != -1)
        {
            map[r][c] = -1;
            num--;
        }
    }
    // 处理
    for (int i = 1; i <= ROW; i++)
    {
        for (int j = 1; j <= COL; j++)
        {
            if (map[i][j] != -1)
            {
                for (int x = i - 1; x <= i + 1; x++)
                {
                    for (int y = j - 1; y <= j + 1; y++)
                    {
                        if (map[x][y] == -1)
                        {
                            map[i][j]++;
                        }
                    }
                }
            }
        }
    }
    // 掩盖
    for (int i = 1; i <= ROW; i++)
    {
        for (int j = 1; j <= COL; j++)
        {
            map[i][j] += 10;
        }
    }
}

// 随机函数
int RandCho()
{
    int r = 1, c = 1, r_o = 1, c_o = 1;
    double Rate = (double)MinFlag / (double)EmpFlag;
    double Rate_N = (double)MinFlag / (double)EmpFlag;
    double Rate_O = (double)MinFlag / (double)EmpFlag;
    if (MinFlag == EmpFlag)
    {
        for (int i = 1; i <= ROW; i++)
        {
            for (int j = 1; j <= COL; j++)
            {
                if ((IndexF[i][j]) && (map[i][j] < 19 && map[i][j] > 8))
                {
                    MinFlag--;
                    EmpFlag--;
                    map[i][j] = 19;
                }
            }
        }
        Printmap(0, 0);
        printf("[清空]-游戏结束!\n");
        return 1;
    }
    for (int i = 1; i <= ROW; i++)
    {
        for (int j = 1; j <= COL; j++)
        {
            if ((IndexF[i][j]) && (map[i][j] < 9 && map[i][j] > 0))
            {
                int Mnum = 0, Nnum = 0, NnumM = 0;
                for (int x = i - 1; x <= i + 1; x++)
                {
                    for (int y = j - 1; y <= j + 1; y++)
                    {
                        if ((IndexF[x][y]) && (x > 0 && x <= ROW && y > 0 && y <= COL) && (map[x][y] < 19 && map[x][y] > 8))
                        {
                            Nnum++;
                            r_o = x;
                            c_o = y;
                        }
                        if (map[x][y] == 19)
                        {
                            Mnum++;
                        }
                    }
                }
                NnumM = map[i][j] - Mnum;
                if (Nnum != 0)
                {
                    Rate_O = (double)NnumM / (double)Nnum;
                    if (Rate_O < Rate_N)
                    {
                        Rate_N = Rate_O;
                        r = r_o;
                        c = c_o;
                    }
                }
            }
        }
    }
    if (Rate_N == Rate)
    {
        r = rand() % ROW + 1;
        c = rand() % COL + 1;
        while ((map[r][c] == 19 || map[r][c] < 9))
        {
            r = rand() % ROW + 1;
            c = rand() % COL + 1;
        }
    }
    map[r][c] -= 10;
    if (map[r][c] == 0)
    {
        CleZer(r, c);
        return 0;
    }
    else if (map[r][c] == -1)
    {
        MinFlag--;
        EmpFlag--;
        Printmap(r, c);
        printf("[爆炸]-游戏结束!\n");
        return 1;
    }
    else
    {
        EmpFlag--;
        return 0;
    }
}

// 清空连锁函数
void CleZer(int r, int c)
{
    EmpFlag--;
    IndexF[r][c] = 0;
    for (int x = r - 1; x <= r + 1; x++)
    {
        for (int y = c - 1; y <= c + 1; y++)
        {
            if ((x > 0 && x <= ROW && y > 0 && y <= COL) && (map[x][y] < 19 && map[x][y] > 8))
            {
                map[x][y] -= 10;
                if (map[x][y] == 0)
                {
                    CleZer(x, y);
                }
                else
                {
                    EmpFlag--;
                }
            }
        }
    }
}

// 单集合判雷
int OneSet()
{
    int flag = 0;
whi_A:
    for (int i = 1; i <= ROW; i++)
    {
        for (int j = 1; j <= COL; j++)
        {
            if ((IndexF[i][j]) && (map[i][j] < 9 && map[i][j] > 0))
            {
                int Mnum = 0, Nnum = 0;
                for (int x = i - 1; x <= i + 1; x++)
                {
                    for (int y = j - 1; y <= j + 1; y++)
                    {
                        if ((IndexF[x][y]) && (x > 0 && x <= ROW && y > 0 && y <= COL) && (map[x][y] < 19 && map[x][y] > 8))
                        {
                            Nnum++;
                        }
                        if (map[x][y] == 19)
                        {
                            Mnum++;
                        }
                    }
                }
                if (Nnum == 0)
                {
                    IndexF[i][j] = 0;
                }
                if (Mnum + Nnum == map[i][j] && Mnum != map[i][j])
                {
                    IndexF[i][j] = 0;
                    for (int x = i - 1; x <= i + 1; x++)
                    {
                        for (int y = j - 1; y <= j + 1; y++)
                        {
                            if ((IndexF[x][y]) && (x > 0 && x <= ROW && y > 0 && y <= COL) && (map[x][y] < 19 && map[x][y] > 8))
                            {
                                if (NP_P == 1)
                                {
                                    Printmap(x, y);
                                    system("clear");
                                }
                                IndexF[x][y] = 0;
                                MinFlag--;
                                EmpFlag--;
                                map[x][y] = 19;
                                if (NP_P == 1)
                                {
                                    Printmap(x, y);
                                    system("clear");
                                }
                            }
                        }
                    }
                    flag++;
                    goto whi_A;
                }
                else if (Mnum == map[i][j])
                {
                    IndexF[i][j] = 0;
                    for (int x = i - 1; x <= i + 1; x++)
                    {
                        for (int y = j - 1; y <= j + 1; y++)
                        {
                            if ((IndexF[x][y]) && (x > 0 && x <= ROW && y > 0 && y <= COL) && (map[x][y] < 19 && map[x][y] > 8))
                            {
                                if (NP_P == 1)
                                {
                                    Printmap(x, y);
                                    system("clear");
                                }
                                map[x][y] -= 10;
                                if (map[x][y] == 0)
                                {
                                    CleZer(x, y);
                                }
                                else
                                {
                                    EmpFlag--;
                                }
                                if (NP_P == 1)
                                {
                                    Printmap(x, y);
                                    system("clear");
                                }
                            }
                        }
                    }
                    flag++;
                    goto whi_A;
                }
            }
        }
    }
    return flag;
}

// 双集合判雷
int TwoSet()
{
    int flag = 0;
whi_B:
    for (int i = 1; i <= ROW; i++)
    {
        for (int j = 1; j <= COL; j++)
        {
            if ((IndexF[i][j]) && (map[i][j] < 9 && map[i][j] > 0))
            {
                for (int x = i - 2; x <= i + 2; x++)
                {
                    for (int y = j - 2; y <= j + 2; y++)
                    {
                        if ((IndexF[x][y]) && (x > 0 && x <= ROW && y > 0 && y <= COL) && (!(i == x && j == y)) && (map[x][y] < 9 && map[x][y] > 0))
                        {
                            int S_1_Mnum = 0, S_1_Nnum = 0, S_1_NnumM = 0;
                            int S_2_Mnum = 0, S_2_Nnum = 0, S_2_NnumM = 0;
                            int S_1 = 0, S_2 = 0;
                            int Set_1[8], Set_2[8];
                            for (int _i = i - 1; _i <= i + 1; _i++)
                            {
                                for (int _j = j - 1; _j <= j + 1; _j++)
                                {
                                    if ((IndexF[_i][_j]) && (_i > 0 && _i <= ROW && _j > 0 && _j <= COL) && (map[_i][_j] < 19 && map[_i][_j] > 8))
                                    {
                                        if (ROW > COL)
                                        {
                                            Set_1[S_1++] = _i * (ROW + 2) + _j;
                                        }
                                        else
                                        {
                                            Set_1[S_1++] = _j * (COL + 2) + _i;
                                        }
                                        S_1_Nnum++;
                                    }
                                    if ((_i > 0 && _i <= ROW && _j > 0 && _j <= COL) && (map[_i][_j] == 19))
                                    {
                                        S_1_Mnum++;
                                    }
                                }
                            }
                            if (S_1_Nnum == 0)
                            {
                                IndexF[i][j] = 0;
                            }
                            S_1_NnumM = map[i][j] - S_1_Mnum;
                            for (int _i = x - 1; _i <= x + 1; _i++)
                            {
                                for (int _j = y - 1; _j <= y + 1; _j++)
                                {
                                    if ((IndexF[_i][_j]) && (_i > 0 && _i <= ROW && _j > 0 && _j <= COL) && (map[_i][_j] < 19 && map[_i][_j] > 8))
                                    {
                                        if (ROW > COL)
                                        {
                                            Set_2[S_2++] = _i * (ROW + 2) + _j;
                                        }
                                        else
                                        {
                                            Set_2[S_2++] = _j * (COL + 2) + _i;
                                        }
                                        S_2_Nnum++;
                                    }
                                    if ((_i > 0 && _i <= ROW && _j > 0 && _j <= COL) && (map[_i][_j] == 19))
                                    {
                                        S_2_Mnum++;
                                    }
                                }
                            }
                            if (S_2_Nnum == 0)
                            {
                                IndexF[x][y] = 0;
                            }
                            S_2_NnumM = map[x][y] - S_2_Mnum;
                            int INnum = IntersectionN(Set_2, Set_1, S_2, S_1);
                            if (INnum)
                            {
                                int a_I = S_1_NnumM, b_I = S_2_NnumM, x_I = S_1_Nnum - INnum, y_I = INnum, z_I = S_2_Nnum - INnum;
                                int S_difN = x_I + z_I;
                                int *result = NULL;
                                result = difference(result, Set_1, S_1, Set_2, S_2, S_difN);
                                if ((((a_I >= b_I && b_I > 0) && (a_I - b_I == x_I)) || ((b_I >= a_I && a_I > 0) && (b_I - a_I == z_I))) && ((x_I == 0 && z_I != 0) || (x_I != 0 && z_I == 0)))
                                {
                                    for (int SD_i = 0; SD_i < S_difN; SD_i++)
                                    {
                                        int SD_R = 0, SD_C = 0;
                                        if (ROW > COL)
                                        {
                                            SD_R = result[SD_i] / (ROW + 2);
                                            SD_C = result[SD_i] % (ROW + 2);
                                        }
                                        else
                                        {
                                            SD_R = result[SD_i] % (COL + 2);
                                            SD_C = result[SD_i] / (COL + 2);
                                        }
                                        int dif_r = 0, dif_c = 0;
                                        if (a_I > b_I)
                                        {
                                            dif_r = i;
                                            dif_c = j;
                                        }
                                        if (a_I < b_I)
                                        {
                                            dif_r = x;
                                            dif_c = y;
                                        }
                                        if (a_I == b_I)
                                        {
                                            if (x_I > z_I)
                                            {
                                                dif_r = x;
                                                dif_c = y;
                                            }
                                            else
                                            {
                                                dif_r = i;
                                                dif_c = j;
                                            }
                                        }
                                        if ((abs(SD_R - dif_r) >= 0 && abs(SD_R - dif_r) <= 1) && (abs(SD_C - dif_c) >= 0 && abs(SD_C - dif_c) <= 1))
                                        {
                                            if (map[SD_R][SD_C] < 19 && map[SD_R][SD_C] > 8)
                                            {
                                                if (NP_P == 1)
                                                {
                                                    Printmap(SD_R, SD_C);
                                                    system("clear");
                                                }
                                                IndexF[SD_R][SD_C] = 0;
                                                MinFlag--;
                                                EmpFlag--;
                                                map[SD_R][SD_C] = 19;
                                                if (NP_P == 1)
                                                {
                                                    Printmap(SD_R, SD_C);
                                                    system("clear");
                                                }
                                            }
                                        }
                                        else
                                        {
                                            if (map[SD_R][SD_C] < 19 && map[SD_R][SD_C] > 8)
                                            {
                                                if (NP_P == 1)
                                                {
                                                    Printmap(SD_R, SD_C);
                                                    system("clear");
                                                }
                                                map[SD_R][SD_C] -= 10;
                                                if (map[SD_R][SD_C] == 0)
                                                {
                                                    CleZer(SD_R, SD_C);
                                                }
                                                else
                                                {
                                                    EmpFlag--;
                                                }
                                                if (NP_P == 1)
                                                {
                                                    Printmap(SD_R, SD_C);
                                                    system("clear");
                                                }
                                            }
                                        }
                                    }
                                    flag++;
                                    goto whi_B;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return flag;
}

// 三集合判雷
int ThrSet()
{
    int flag = 0;
whi_C:
    for (int i = 1; i <= ROW; i++)
    {
        for (int j = 1; j <= COL; j++)
        {
            if ((IndexF[i][j]) && (map[i][j] < 9 && map[i][j] > 0))
            {
                for (int x = i - 2; x <= i + 2; x++)
                {
                    for (int y = j - 2; y <= j + 2; y++)
                    {
                        if ((IndexF[x][y]) && (x > 0 && x <= ROW && y > 0 && y <= COL) && (!(i == x && j == y)) && (map[x][y] < 9 && map[x][y] > 0))
                        {
                            for (int r = i - 2; r <= i + 2; r++)
                            {
                                for (int c = j - 2; c <= j + 2; c++)
                                {
                                    if ((IndexF[r][c]) && (r > 0 && c <= ROW && r > 0 && c <= COL) && (!(r == x && c == y)) && (!(r == i && c == j)) && (map[r][c] < 9 && map[r][c] > 0))
                                    {
                                        int S_1_Mnum = 0, S_1_Nnum = 0, S_1_NnumM = 0;
                                        int S_2_Mnum = 0, S_2_Nnum = 0, S_2_NnumM = 0;
                                        int S_3_Mnum = 0, S_3_Nnum = 0, S_3_NnumM = 0;
                                        int S_1 = 0, S_2 = 0, S_3 = 0;
                                        int Set_1[8], Set_2[8], Set_3[8];
                                        for (int _i = 0; _i < 8; _i++)
                                        {
                                            Set_1[_i] = 0;
                                            Set_2[_i] = 0;
                                            Set_3[_i] = 0;
                                        }
                                        for (int i_i = i - 1; i_i <= i + 1; i_i++)
                                        {
                                            for (int j_i = j - 1; j_i <= j + 1; j_i++)
                                            {
                                                if ((IndexF[i_i][j_i]) && (map[i_i][j_i] < 19 && map[i_i][j_i] > 8))
                                                {
                                                    if (ROW > COL)
                                                    {
                                                        Set_1[S_1++] = i_i * (ROW + 2) + j_i;
                                                    }
                                                    else
                                                    {
                                                        Set_1[S_1++] = j_i * (COL + 2) + i_i;
                                                    }
                                                    S_1_Nnum++;
                                                }
                                                if (map[i_i][j_i] == 19)
                                                {
                                                    S_1_Mnum++;
                                                }
                                            }
                                        }
                                        S_1_NnumM = map[i][j] - S_1_Mnum;
                                        for (int x_i = x - 1; x_i <= x + 1; x_i++)
                                        {
                                            for (int y_i = y - 1; y_i <= y + 1; y_i++)
                                            {
                                                if ((IndexF[x_i][y_i]) && (map[x_i][y_i] < 19 && map[x_i][y_i] > 8))
                                                {
                                                    if (ROW > COL)
                                                    {
                                                        Set_2[S_2++] = x_i * (ROW + 2) + y_i;
                                                    }
                                                    else
                                                    {
                                                        Set_2[S_2++] = y_i * (COL + 2) + x_i;
                                                    }
                                                    S_2_Nnum++;
                                                }
                                                if (map[x_i][y_i] == 19)
                                                {
                                                    S_2_Mnum++;
                                                }
                                            }
                                        }
                                        S_2_NnumM = map[x][y] - S_2_Mnum;
                                        for (int r_i = r - 1; r_i <= r + 1; r_i++)
                                        {
                                            for (int c_i = c - 1; c_i <= c + 1; c_i++)
                                            {
                                                if ((IndexF[r_i][c_i]) && (map[r_i][c_i] < 19 && map[r_i][c_i] > 8))
                                                {
                                                    if (ROW > COL)
                                                    {
                                                        Set_3[S_3++] = r_i * (ROW + 2) + c_i;
                                                    }
                                                    else
                                                    {
                                                        Set_3[S_3++] = c_i * (COL + 2) + r_i;
                                                    }
                                                    S_3_Nnum++;
                                                }
                                                if (map[r_i][c_i] == 19)
                                                {
                                                    S_3_Mnum++;
                                                }
                                            }
                                        }
                                        S_3_NnumM = map[r][c] - S_3_Mnum;
                                        int INnum_2_1 = IntersectionN(Set_2, Set_1, S_2, S_1);
                                        int INnum_3_1 = IntersectionN(Set_3, Set_1, S_3, S_1);
                                        int INnum_3_2 = IntersectionN(Set_3, Set_2, S_3, S_2);
                                        if (INnum_2_1 != 0 && INnum_3_1 != 0 && INnum_3_2 == 0) // A
                                        {
                                            int a_I = S_2_NnumM, b_I = S_1_NnumM, c_I = S_3_NnumM, v_I = S_2_Nnum - INnum_2_1, w_I = INnum_2_1, x_I = S_1_Nnum - INnum_2_1 - INnum_3_1, y_I = INnum_3_1, z_I = S_3_Nnum - INnum_3_1;
                                            if (((b_I >= a_I + c_I && a_I + c_I > 0) && (b_I - a_I - c_I == x_I) || ((a_I + c_I >= b_I && b_I > 0) && (a_I + c_I - b_I == v_I + z_I))) && (v_I + z_I != 0))
                                            {
                                                int S_2_1_difN = v_I + x_I + y_I;
                                                int S_3_1_difN = w_I + x_I + z_I;
                                                int *result_2_1 = NULL;
                                                int *result_3_1 = NULL;
                                                int result_2_1_x_i = 0;
                                                int result_3_1_r_i = 0;
                                                int result_2_1_i_i = 0;
                                                int result_3_1_i_i = 0;
                                                int result_2_1_x[8];
                                                int result_3_1_r[8];
                                                int result_2_1_i[8];
                                                int result_3_1_i[8];
                                                int *result_x = NULL;
                                                for (int _i = 0; _i < 8; _i++)
                                                {
                                                    result_2_1_x[_i] = 0;
                                                    result_3_1_r[_i] = 0;
                                                    result_2_1_i[_i] = 0;
                                                    result_3_1_i[_i] = 0;
                                                }
                                                result_2_1 = difference(result_2_1, Set_1, S_1, Set_2, S_2, S_2_1_difN);
                                                for (int res = 0; res < S_2_1_difN; res++)
                                                {
                                                    int res_i = 0, res_j = 0;
                                                    if (ROW > COL)
                                                    {
                                                        res_i = result_2_1[res] / (ROW + 2);
                                                        res_j = result_2_1[res] % (ROW + 2);
                                                    }
                                                    else
                                                    {
                                                        res_i = result_2_1[res] % (COL + 2);
                                                        res_j = result_2_1[res] / (COL + 2);
                                                    }
                                                    if ((abs(res_i - x) >= 0 && abs(res_i - x) <= 1) && (abs(res_j - y) >= 0 && abs(res_j - y) <= 1))
                                                    {
                                                        result_2_1_x[result_2_1_x_i++] = result_2_1[res];
                                                    }
                                                    if ((abs(res_i - i) >= 0 && abs(res_i - i) <= 1) && (abs(res_j - j) >= 0 && abs(res_j - j) <= 1))
                                                    {
                                                        result_2_1_i[result_2_1_i_i++] = result_2_1[res];
                                                    }
                                                }
                                                result_3_1 = difference(result_3_1, Set_1, S_1, Set_3, S_3, S_3_1_difN);
                                                for (int res = 0; res < S_3_1_difN; res++)
                                                {
                                                    int res_i = 0, res_j = 0;
                                                    if (ROW > COL)
                                                    {
                                                        res_i = result_3_1[res] / (ROW + 2);
                                                        res_j = result_3_1[res] % (ROW + 2);
                                                    }
                                                    else
                                                    {
                                                        res_i = result_3_1[res] % (COL + 2);
                                                        res_j = result_3_1[res] / (COL + 2);
                                                    }
                                                    if ((abs(res_i - r) >= 0 && abs(res_i - r) <= 1) && (abs(res_j - c) >= 0 && abs(res_j - c) <= 1))
                                                    {
                                                        result_3_1_r[result_3_1_r_i++] = result_3_1[res];
                                                    }
                                                    if ((abs(res_i - i) >= 0 && abs(res_i - i) <= 1) && (abs(res_j - j) >= 0 && abs(res_j - j) <= 1))
                                                    {
                                                        result_3_1_i[result_3_1_i_i++] = result_3_1[res];
                                                    }
                                                }
                                                result_x = IntersectionSet(result_x, result_2_1_i, result_3_1_i, result_2_1_i_i, result_3_1_i_i, x_I);
                                                for (int res = 0; res < x_I; res++)
                                                {
                                                    int res_i = 0, res_j = 0;
                                                    if (ROW > COL)
                                                    {
                                                        res_i = result_x[res] / (ROW + 2);
                                                        res_j = result_x[res] % (ROW + 2);
                                                    }
                                                    else
                                                    {
                                                        res_i = result_x[res] % (COL + 2);
                                                        res_j = result_x[res] / (COL + 2);
                                                    }
                                                    if (b_I > a_I + c_I)
                                                    {
                                                        if (map[res_i][res_j] < 19 && map[res_i][res_j] > 8)
                                                        {
                                                            if (NP_P == 1)
                                                            {
                                                                Printmap(res_i, res_j);
                                                                system("clear");
                                                            }
                                                            IndexF[res_i][res_j] = 0;
                                                            MinFlag--;
                                                            EmpFlag--;
                                                            map[res_i][res_j] = 19;
                                                            if (NP_P == 1)
                                                            {
                                                                Printmap(res_i, res_j);
                                                                system("clear");
                                                            }
                                                        }
                                                    }
                                                    if (b_I < a_I + c_I)
                                                    {
                                                        if (map[res_i][res_j] < 19 && map[res_i][res_j] > 8)
                                                        {
                                                            if (NP_P == 1)
                                                            {
                                                                Printmap(res_i, res_j);
                                                                system("clear");
                                                            }
                                                            map[res_i][res_j] -= 10;
                                                            if (map[res_i][res_j] == 0)
                                                            {
                                                                CleZer(res_i, res_j);
                                                            }
                                                            else
                                                            {
                                                                EmpFlag--;
                                                            }
                                                            if (NP_P == 1)
                                                            {
                                                                Printmap(res_i, res_j);
                                                                system("clear");
                                                            }
                                                        }
                                                    }
                                                    flag++;
                                                }
                                                for (int res = 0; res < result_2_1_x_i; res++)
                                                {
                                                    int res_i = 0, res_j = 0;
                                                    if (ROW > COL)
                                                    {
                                                        res_i = result_2_1_x[res] / (ROW + 2);
                                                        res_j = result_2_1_x[res] % (ROW + 2);
                                                    }
                                                    else
                                                    {
                                                        res_i = result_2_1_x[res] % (COL + 2);
                                                        res_j = result_2_1_x[res] / (COL + 2);
                                                    }
                                                    if (b_I >= a_I + c_I)
                                                    {
                                                        if (map[res_i][res_j] < 19 && map[res_i][res_j] > 8)
                                                        {
                                                            if (NP_P == 1)
                                                            {
                                                                Printmap(res_i, res_j);
                                                                system("clear");
                                                            }
                                                            map[res_i][res_j] -= 10;
                                                            if (map[res_i][res_j] == 0)
                                                            {
                                                                CleZer(res_i, res_j);
                                                            }
                                                            else
                                                            {
                                                                EmpFlag--;
                                                            }
                                                            if (NP_P == 1)
                                                            {
                                                                Printmap(res_i, res_j);
                                                                system("clear");
                                                            }
                                                        }
                                                    }
                                                    if (b_I < a_I + c_I)
                                                    {
                                                        if (map[res_i][res_j] < 19 && map[res_i][res_j] > 8)
                                                        {
                                                            if (NP_P == 1)
                                                            {
                                                                Printmap(res_i, res_j);
                                                                system("clear");
                                                            }
                                                            IndexF[res_i][res_j] = 0;
                                                            MinFlag--;
                                                            EmpFlag--;
                                                            map[res_i][res_j] = 19;
                                                            if (NP_P == 1)
                                                            {
                                                                Printmap(res_i, res_j);
                                                                system("clear");
                                                            }
                                                        }
                                                    }
                                                    flag++;
                                                }
                                                for (int res = 0; res < result_3_1_r_i; res++)
                                                {
                                                    int res_i = 0, res_j = 0;
                                                    if (ROW > COL)
                                                    {
                                                        res_i = result_3_1_r[res] / (ROW + 2);
                                                        res_j = result_3_1_r[res] % (ROW + 2);
                                                    }
                                                    else
                                                    {
                                                        res_i = result_3_1_r[res] % (COL + 2);
                                                        res_j = result_3_1_r[res] / (COL + 2);
                                                    }
                                                    if (b_I >= a_I + c_I)
                                                    {
                                                        if (map[res_i][res_j] < 19 && map[res_i][res_j] > 8)
                                                        {
                                                            if (NP_P == 1)
                                                            {
                                                                Printmap(res_i, res_j);
                                                                system("clear");
                                                            }
                                                            map[res_i][res_j] -= 10;
                                                            if (map[res_i][res_j] == 0)
                                                            {
                                                                CleZer(res_i, res_j);
                                                            }
                                                            else
                                                            {
                                                                EmpFlag--;
                                                            }
                                                            if (NP_P == 1)
                                                            {
                                                                Printmap(res_i, res_j);
                                                                system("clear");
                                                            }
                                                        }
                                                    }
                                                    if (b_I < a_I + c_I)
                                                    {
                                                        if (map[res_i][res_j] < 19 && map[res_i][res_j] > 8)
                                                        {
                                                            if (NP_P == 1)
                                                            {
                                                                Printmap(res_i, res_j);
                                                                system("clear");
                                                            }
                                                            IndexF[res_i][res_j] = 0;
                                                            MinFlag--;
                                                            EmpFlag--;
                                                            map[res_i][res_j] = 19;
                                                            if (NP_P == 1)
                                                            {
                                                                Printmap(res_i, res_j);
                                                                system("clear");
                                                            }
                                                        }
                                                    }
                                                    flag++;
                                                }
                                                goto whi_C;
                                            }
                                        }
                                        else if (INnum_2_1 != 0 && INnum_3_1 != 0 && INnum_3_2 != 0) // B
                                        {
                                            int *result_3_2 = NULL;
                                            result_3_2 = IntersectionSet(result_3_2, Set_3, Set_2, S_3, S_2, INnum_3_2);
                                            int IS_B = IntersectionN(result_3_2, Set_1, INnum_3_2, S_1);
                                            if (IS_B == INnum_3_2 && IS_B < S_1_Nnum && INnum_2_1 + INnum_3_1 > S_1_Nnum)
                                            {
                                                int a_I = S_2_NnumM, b_I = S_1_NnumM, c_I = S_3_NnumM, v_I = S_2_Nnum - INnum_2_1, w_I = INnum_2_1 - INnum_3_2, x_I = INnum_3_2, y_I = INnum_3_1 - INnum_3_2, z_I = S_3_Nnum - INnum_3_1;
                                                if (w_I != 0 && y_I != 0 && v_I != 0 && z_I != 0)
                                                {
                                                    int *result_2_1 = NULL;
                                                    int *result_3_1 = NULL;
                                                    int result_2_1_x[8];
                                                    int result_3_1_r[8];
                                                    int result_2_1_x_i = 0;
                                                    int result_3_1_r_i = 0;
                                                    for (int _i = 0; _i < 8; _i++)
                                                    {
                                                        result_2_1_x[_i] = 0;
                                                        result_3_1_r[_i] = 0;
                                                    }
                                                    result_2_1 = difference(result_2_1, Set_1, S_1, Set_2, S_2, INnum_2_1);
                                                    for (int res = 0; res < INnum_2_1; res++)
                                                    {
                                                        int res_i = 0, res_j = 0;
                                                        if (ROW > COL)
                                                        {
                                                            res_i = result_2_1[res] / (ROW + 2);
                                                            res_j = result_2_1[res] % (ROW + 2);
                                                        }
                                                        else
                                                        {
                                                            res_i = result_2_1[res] % (COL + 2);
                                                            res_j = result_2_1[res] / (COL + 2);
                                                        }
                                                        if ((abs(res_i - x) >= 0 && abs(res_i - x) <= 1) && (abs(res_j - y) >= 0 && abs(res_j - y) <= 1))
                                                        {
                                                            result_2_1_x[result_2_1_x_i++] = result_2_1[res];
                                                        }
                                                    }
                                                    result_3_1 = difference(result_3_1, Set_1, S_1, Set_3, S_3, INnum_3_1);
                                                    for (int res = 0; res < INnum_3_1; res++)
                                                    {
                                                        int res_i = 0, res_j = 0;
                                                        if (ROW > COL)
                                                        {
                                                            res_i = result_3_1[res] / (ROW + 2);
                                                            res_j = result_3_1[res] % (ROW + 2);
                                                        }
                                                        else
                                                        {
                                                            res_i = result_3_1[res] % (COL + 2);
                                                            res_j = result_3_1[res] / (COL + 2);
                                                        }
                                                        if ((abs(res_i - r) >= 0 && abs(res_i - r) <= 1) && (abs(res_j - c) >= 0 && abs(res_j - c) <= 1))
                                                        {
                                                            result_3_1_r[result_3_1_r_i++] = result_3_1[res];
                                                        }
                                                    }
                                                    if (b_I == a_I + c_I)
                                                    {
                                                        for (int res = 0; res < result_2_1_x_i; res++)
                                                        {
                                                            int res_i = 0, res_j = 0;
                                                            if (ROW > COL)
                                                            {
                                                                res_i = result_2_1_x[res] / (ROW + 2);
                                                                res_j = result_2_1_x[res] % (ROW + 2);
                                                            }
                                                            else
                                                            {
                                                                res_i = result_2_1_x[res] % (COL + 2);
                                                                res_j = result_2_1_x[res] / (COL + 2);
                                                            }
                                                            if (map[res_i][res_j] < 19 && map[res_i][res_j] > 8)
                                                            {
                                                                if (NP_P == 1)
                                                                {
                                                                    Printmap(res_i, res_j);
                                                                    system("clear");
                                                                }
                                                                map[res_i][res_j] -= 10;
                                                                if (map[res_i][res_j] == 0)
                                                                {
                                                                    CleZer(res_i, res_j);
                                                                }
                                                                else
                                                                {
                                                                    EmpFlag--;
                                                                }
                                                                if (NP_P == 1)
                                                                {
                                                                    Printmap(res_i, res_j);
                                                                    system("clear");
                                                                }
                                                            }
                                                            flag++;
                                                        }
                                                        for (int res = 0; res < result_3_1_r_i; res++)
                                                        {
                                                            int res_i = 0, res_j = 0;
                                                            if (ROW > COL)
                                                            {
                                                                res_i = result_3_1_r[res] / (ROW + 2);
                                                                res_j = result_3_1_r[res] % (ROW + 2);
                                                            }
                                                            else
                                                            {
                                                                res_i = result_3_1_r[res] % (COL + 2);
                                                                res_j = result_3_1_r[res] / (COL + 2);
                                                            }
                                                            if (map[res_i][res_j] < 19 && map[res_i][res_j] > 8)
                                                            {
                                                                if (NP_P == 1)
                                                                {
                                                                    Printmap(res_i, res_j);
                                                                    system("clear");
                                                                }
                                                                map[res_i][res_j] -= 10;
                                                                if (map[res_i][res_j] == 0)
                                                                {
                                                                    CleZer(res_i, res_j);
                                                                }
                                                                else
                                                                {
                                                                    EmpFlag--;
                                                                }
                                                                if (NP_P == 1)
                                                                {
                                                                    Printmap(res_i, res_j);
                                                                    system("clear");
                                                                }
                                                            }
                                                            flag++;
                                                        }
                                                        for (int res = 0; res < INnum_3_2; res++)
                                                        {
                                                            int res_i = 0, res_j = 0;
                                                            if (ROW > COL)
                                                            {
                                                                res_i = result_3_2[res] / (ROW + 2);
                                                                res_j = result_3_2[res] % (ROW + 2);
                                                            }
                                                            else
                                                            {
                                                                res_i = result_3_2[res] % (COL + 2);
                                                                res_j = result_3_2[res] / (COL + 2);
                                                            }
                                                            if (map[res_i][res_j] < 19 && map[res_i][res_j] > 8)
                                                            {
                                                                if (NP_P == 1)
                                                                {
                                                                    Printmap(res_i, res_j);
                                                                    system("clear");
                                                                }
                                                                map[res_i][res_j] -= 10;
                                                                if (map[res_i][res_j] == 0)
                                                                {
                                                                    CleZer(res_i, res_j);
                                                                }
                                                                else
                                                                {
                                                                    EmpFlag--;
                                                                }
                                                                if (NP_P == 1)
                                                                {
                                                                    Printmap(res_i, res_j);
                                                                    system("clear");
                                                                }
                                                            }
                                                            flag++;
                                                        }
                                                        goto whi_C;
                                                    }
                                                    if (a_I + c_I - b_I == v_I + x_I + z_I)
                                                    {
                                                        for (int res = 0; res < result_2_1_x_i; res++)
                                                        {
                                                            int res_i = 0, res_j = 0;
                                                            if (ROW > COL)
                                                            {
                                                                res_i = result_2_1_x[res] / (ROW + 2);
                                                                res_j = result_2_1_x[res] % (ROW + 2);
                                                            }
                                                            else
                                                            {
                                                                res_i = result_2_1_x[res] % (COL + 2);
                                                                res_j = result_2_1_x[res] / (COL + 2);
                                                            }
                                                            if (map[res_i][res_j] < 19 && map[res_i][res_j] > 8)
                                                            {
                                                                if (NP_P == 1)
                                                                {
                                                                    Printmap(res_i, res_j);
                                                                    system("clear");
                                                                }
                                                                IndexF[res_i][res_j] = 0;
                                                                MinFlag--;
                                                                EmpFlag--;
                                                                map[res_i][res_j] = 19;
                                                                if (NP_P == 1)
                                                                {
                                                                    Printmap(res_i, res_j);
                                                                    system("clear");
                                                                }
                                                            }
                                                            flag++;
                                                        }
                                                        for (int res = 0; res < result_3_1_r_i; res++)
                                                        {
                                                            int res_i = 0, res_j = 0;
                                                            if (ROW > COL)
                                                            {
                                                                res_i = result_3_1_r[res] / (ROW + 2);
                                                                res_j = result_3_1_r[res] % (ROW + 2);
                                                            }
                                                            else
                                                            {
                                                                res_i = result_3_1_r[res] % (COL + 2);
                                                                res_j = result_3_1_r[res] / (COL + 2);
                                                            }
                                                            if (map[res_i][res_j] < 19 && map[res_i][res_j] > 8)
                                                            {
                                                                if (NP_P == 1)
                                                                {
                                                                    Printmap(res_i, res_j);
                                                                    system("clear");
                                                                }
                                                                IndexF[res_i][res_j] = 0;
                                                                MinFlag--;
                                                                EmpFlag--;
                                                                map[res_i][res_j] = 19;
                                                                if (NP_P == 1)
                                                                {
                                                                    Printmap(res_i, res_j);
                                                                    system("clear");
                                                                }
                                                            }
                                                            flag++;
                                                        }
                                                        for (int res = 0; res < INnum_3_2; res++)
                                                        {
                                                            int res_i = 0, res_j = 0;
                                                            if (ROW > COL)
                                                            {
                                                                res_i = result_3_2[res] / (ROW + 2);
                                                                res_j = result_3_2[res] % (ROW + 2);
                                                            }
                                                            else
                                                            {
                                                                res_i = result_3_2[res] % (COL + 2);
                                                                res_j = result_3_2[res] / (COL + 2);
                                                            }
                                                            if (map[res_i][res_j] < 19 && map[res_i][res_j] > 8)
                                                            {
                                                                if (NP_P == 1)
                                                                {
                                                                    Printmap(res_i, res_j);
                                                                    system("clear");
                                                                }
                                                                IndexF[res_i][res_j] = 0;
                                                                MinFlag--;
                                                                EmpFlag--;
                                                                map[res_i][res_j] = 19;
                                                                if (NP_P == 1)
                                                                {
                                                                    Printmap(res_i, res_j);
                                                                    system("clear");
                                                                }
                                                            }
                                                            flag++;
                                                        }
                                                        goto whi_C;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return flag;
}

// 交集个数
int IntersectionN(int arr1[], int arr2[], int m, int n)
{
    int i, j, num = 0;
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < m; j++)
        {
            if (arr2[i] == arr1[j])
                num++;
        }
    }
    if (num == 0)
    {
        return 0;
    }
    else
    {
        return num;
    }
}

// 交集集合
int *IntersectionSet(int *result, int arr1[], int arr2[], int m, int n, int lenRes)
{
    int i, j, x = 0;
    result = (int *)malloc((lenRes) * sizeof(int));
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < m; j++)
        {
            if (arr2[i] == arr1[j])
                result[x++] = arr2[i];
        }
    }
    if (lenRes == 0)
    {
        return NULL;
    }
    else
    {
        return result;
    }
}

// IsInArray[-.-]
bool isInArray(int num, int array[], int len)
{
    for (int i = 0; i < len; i++)
    {
        if (array[i] == num)
        {
            return true;
        }
    }
    return false;
}

// 双集合非交集区域
int *difference(int *result, int A[], int lenA, int B[], int lenB, int lenRes)
{
    result = (int *)malloc((lenRes) * sizeof(int));
    int x = 0;
    for (int i = 0; i < lenA; i++)
    {
        if (!isInArray(A[i], B, lenB))
        {
            result[x++] = A[i];
        }
    }
    for (int j = 0; j < lenB; j++)
    {
        if (!isInArray(B[j], A, lenA))
        {
            result[x++] = B[j];
        }
    }
    return result;
}

// 打印
void Printmap(int m, int n)
{
    double Rate = (double)SUC / (double)(SUC + FAI);
    printf("   [胜利局数]:{%d} [失败局数]:{%d} [Rate]:{%.2lf}\n", SUC, FAI, Rate);
    Rate = (double)MinFlag / (double)EmpFlag;
    printf("   [剩余雷数]:{%d} [剩余格数]:{%d} [Rate]:{%.2lf}\n", MinFlag, EmpFlag, Rate);
    printf("   ");
    for (int r = 1; r <= COL; r++)
    {
        printf("[%d]", r % 10);
    }
    printf("\n");
    for (int i = 1; i <= ROW; i++)
    {
        if (n == 1 && i == m)
        {
            printf("[%d]", i % 10);
        }
        else
        {
            printf("[%d] ", i % 10);
        }
        for (int j = 1; j <= COL; j++)
        {
            if ((i == m) && (j == n))
            {
                YEL if (n == 1)
                {
                    printf("[");
                }
                else
                {
                    printf(" [");
                }
                if (map[i][j] == -1)
                {
                    printf("@");
                }
                if (map[i][j] < 9 && map[i][j] > -1)
                {
                    printf("%d", map[i][j]);
                }
                if (map[i][j] < 19 && map[i][j] > 8)
                {
                    printf("#");
                }
                if (map[i][j] == 19)
                {
                    printf("$");
                }
                printf("] ");
                WHI
            }
            else if ((i == m) && (j == (n - 1)))
            {
                if (map[i][j] == -1)
                {
                    YEL
                        printf("@");
                    WHI
                }
                if (map[i][j] < 9 && map[i][j] > -1)
                {
                    if (map[i][j] == 0)
                    {
                        BLA
                    }
                    printf("%d", map[i][j]);
                    WHI
                }
                if (map[i][j] < 19 && map[i][j] > 8)
                {
                    printf("#");
                }
                if (map[i][j] == 19)
                {
                    CYA
                        printf("$");
                    WHI
                }
            }
            else
            {
                if (map[i][j] == -1)
                {
                    YEL
                        printf("@  ");
                    WHI
                }
                if (map[i][j] < 9 && map[i][j] > -1)
                {
                    if (map[i][j] == 0)
                    {
                        BLA
                    }
                    printf("%d  ", map[i][j]);
                    WHI
                }
                if (map[i][j] < 19 && map[i][j] > 8)
                {
                    printf("#  ");
                }
                if (map[i][j] == 19)
                {
                    CYA
                        printf("$  ");
                    WHI
                }
            }
        }
        printf("\n");
    }
}

// 主函数
int main()
{
    int _Count = 0;
NEXT:
    HID if (_Count == COUNT)
    {
        goto OVER;
    }
    MinFlag = NUM;
    EmpFlag = ROW * COL;
    srand((unsigned)time(NULL));
    Initmap();
whi:
    if (RandCho())
    {
        FAI++;
        // int next;
        // scanf("%d", &next);
        sleep(1);
        _Count++;
        system("clear");
        goto NEXT;
    }
    do
    {
        int flag_One = 0, flag_Two = 0, flag_Thr = 0;
        flag_One = OneSet();
        flag_Two = TwoSet();
        flag_Thr = ThrSet();
        if (MinFlag == 0)
        {
            if (EmpFlag != 0)
            {
                for (int i = 1; i <= ROW; i++)
                {
                    for (int j = 1; j <= COL; j++)
                    {
                        if ((IndexF[i][j]) && (map[i][j] < 19 && map[i][j] > 8))
                        {
                            EmpFlag--;
                            map[i][j] -= 10;
                        }
                    }
                }
            }
            Printmap(0, 0);
            printf("[清空]-游戏结束!\n");
            SUC++;
            // int next;
            // scanf("%d", &next);
            sleep(1);
            _Count++;
            system("clear");
            goto NEXT;
        }
        if (flag_One == 0 && flag_Two == 0 && flag_Thr == 0)
        {
            goto whi;
        }
    } while (1);
OVER:
    Printmap(0, 0);
    int sure;
    do
    {
        printf("确认结果请输入:0\n");
        scanf("%d", &sure);
    } while (sure);
    return 0;
}

// --- End ---

下面是一个简单的控制台版本扫雷游戏的C语言代码示例,仅供参考: ```c #include <stdio.h> #include <stdlib.h> #include <time.h> #include <stdbool.h> #define ROWS 10 // 行数 #define COLS 10 // 列数 #define MINE_NUM 10 // 雷数 int minefield[ROWS][COLS]; // 雷区 bool visible[ROWS][COLS]; // 可视区 // 初始化雷区 void initMinefield() { int i, j, count; for (i = 0; i < ROWS; i++) { for (j = 0; j < COLS; j++) { minefield[i][j] = 0; visible[i][j] = false; } } // 随机生成雷 count = 0; while (count < MINE_NUM) { i = rand() % ROWS; j = rand() % COLS; if (minefield[i][j] == 0) { minefield[i][j] = -1; count++; } } // 计算每个格子周围的雷数 for (i = 0; i < ROWS; i++) { for (j = 0; j < COLS; j++) { if (minefield[i][j] == -1) { continue; } if (i > 0 && j > 0 && minefield[i-1][j-1] == -1) { minefield[i][j]++; } if (i > 0 && minefield[i-1][j] == -1) { minefield[i][j]++; } if (i > 0 && j < COLS-1 && minefield[i-1][j+1] == -1) { minefield[i][j]++; } if (j > 0 && minefield[i][j-1] == -1) { minefield[i][j]++; } if (j < COLS-1 && minefield[i][j+1] == -1) { minefield[i][j]++; } if (i < ROWS-1 && j > 0 && minefield[i+1][j-1] == -1) { minefield[i][j]++; } if (i < ROWS-1 && minefield[i+1][j] == -1) { minefield[i][j]++; } if (i < ROWS-1 && j < COLS-1 && minefield[i+1][j+1] == -1) { minefield[i][j]++; } } } } // 输出雷区 void printMinefield() { int i, j; printf(" "); for (j = 0; j < COLS; j++) { printf("%d ", j); } printf("\n"); printf(" "); for (j = 0; j < COLS; j++) { printf("- "); } printf("\n"); for (i = 0; i < ROWS; i++) { printf("%d|", i); for (j = 0; j < COLS; j++) { if (visible[i][j]) { if (minefield[i][j] == -1) { printf("* "); } else { printf("%d ", minefield[i][j]); } } else { printf(". "); } } printf("\n"); } } // 检查是否赢了 bool checkWin() { int i, j, count = 0; for (i = 0; i < ROWS; i++) { for (j = 0; j < COLS; j++) { if (minefield[i][j] == -1 && visible[i][j]) { return false; } if (minefield[i][j] != -1 && visible[i][j]) { count++; } } } return count == ROWS * COLS - MINE_NUM; } // 处理输入的命令 void handleCommand(char cmd, int row, int col) { if (row < 0 || row >= ROWS || col < 0 || col >= COLS) { printf("Invalid position.\n"); return; } if (cmd == 'o') { // 打开格子 if (visible[row][col]) { printf("This position has already been opened.\n"); return; } visible[row][col] = true; if (minefield[row][col] == -1) { printf("Game over!\n"); printMinefield(); exit(0); } if (minefield[row][col] == 0) { int i, j; for (i = row-1; i <= row+1; i++) { for (j = col-1; j <= col+1; j++) { if (i >= 0 && i < ROWS && j >= 0 && j < COLS && minefield[i][j] != -1) { visible[i][j] = true; } } } } if (checkWin()) { printf("You win!\n"); printMinefield(); exit(0); } } else if (cmd == 'f') { // 标记格子为雷 visible[row][col] = true; } else { printf("Invalid command.\n"); } } int main() { srand(time(NULL)); initMinefield(); printf("Welcome to Minesweeper!\n"); printf("You can enter commands like 'o 3 4' to open the cell at row 3 and column 4.\n"); printf("You can also mark a cell as containing a mine by using the 'f' command.\n"); printf("Good luck!\n"); while (true) { printMinefield(); char cmd; int row, col; printf("Enter command: "); scanf("%c %d %d", &cmd, &row, &col); while (getchar() != '\n'); // 清空输入缓冲区 handleCommand(cmd, row, col); } return 0; } ``` 该代码实现扫雷游戏的基本功能,包括随机生成雷区、计算每个格子周围的雷数、打开格子、标记格子为雷、检查是否赢了等。在控制台中运行该程序,便可以体验简单的扫雷游戏了。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值