2011年4月2日 星期六

[程式分享] 數獨 Part. 2:解題程式 (C++ Class)

SudokuGen.h與SudokuGen.cpp程式內容請參考:[程式分享] 數獨 Part. 1:自動題目產生器 (C++ Class)


main.cpp (主程式)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

#include <iostream>
#include <vector>
#include "SudokuGen.h"
#include "DigSudoku.h"
#include "SolveSudoku.h"

using namespace std;

void print_board(vector <int >);

int main()
{
    int Ans = 0; // 數獨解的個數
    vector <int > digboard;
    SudokuGen NewBoard; // 產生棋盤物件
    vector <int > board = NewBoard.get_board(); //取得棋盤
    print_board(board); // 將棋盤印出

    while(Ans!=1) // 檢查題目是否有唯一解,且解答與題目一致
    {
        DigSudoku Board_Dig(board,35); // 挖洞出題
        digboard = Board_Dig.get_board(); // 取得挖洞後的棋盤
        SolveSudoku Board_Solve(digboard,0); // 解題
        Ans = Board_Solve.getAns(); // 取得題目總共有多少個解
        if (Ans==1 && board!=Board_Solve.get_board())
        {
            Ans = 0;
        }
    }
    print_board(digboard); // 印出題目
    return 0;
}

void print_board(vector <int > board) //將棋盤印出
{
    for (int i=0;i<board.size();i++)
    {
        if(board[i]==0){cout << "  ";}
        else{cout << board[i] << ' ';}
        if ((i+1) % 9 == 0)
        {
            cout << endl;
        }
    }
    cout << endl;
}


DigSudoku.h (數獨題目挖洞程式)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

#define SIZE 9
#include<vector>
using namespace std;

class DigSudoku
{
    public:
        DigSudoku(vector < int >, int); //建構子
        void board_dig(int); //挖洞函式
        vector < int > get_board(); //輸出棋盤至vector變數
        void copy_board(); //複製棋盤函式
    private:
        int i,j; //迴圈用變數
        int in_board[SIZE][SIZE]; //輸入棋盤
        int dig_board[SIZE][SIZE]; //輸出棋盤(挖完洞)
        int number_seres[SIZE*SIZE]; //數字序列
};


DigSudoku.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

#include "DigSudoku.h"
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector>
using namespace std;

DigSudoku::DigSudoku(vector < int > board, int n_hole)
{
    srand(time(NULL));//以物件初始化時間設定為亂數種子
    for (i=0;i<SIZE*SIZE;i++) //建立0到80的數字序列
    {
        number_seres[i] = i;
    }
    for (i=0;i<SIZE;i++)//將輸入的數獨題目寫入至in_board變數
        for (j=0;j<SIZE;j++)
        {
            in_board[i][j] = board[i*SIZE+j];

        }
    board_dig(n_hole);//呼叫挖洞函式
}

void DigSudoku::board_dig(int n_hole)
{
    int temp; //置換用暫存變數
    copy_board(); //呼叫複製函數
    for (i=0;i<81;i++) //將數字序列打亂
    {
        j = rand()%81;
        temp = number_seres[i];
        number_seres[i] = number_seres[j];
        number_seres[j] = temp;
    }
    for (i=0;i<n_hole;i++)
    {
        dig_board[number_seres[i]/SIZE][number_seres[i]%SIZE] = 0;
    }
}

vector < int > DigSudoku::get_board()
/*輸出挖完洞的題目至vector變數*/
{
    vector < int > out_board(SIZE*SIZE);
    for (i=0;i<SIZE;i++)
        for (j=0;j<SIZE;j++)
        {
            out_board[i*SIZE+j] = dig_board[i][j];
        }
    return out_board;
}

void DigSudoku::copy_board()
/*
複製棋盤函式
copy in_board into dig_board
*/
{
    for (i=0;i<SIZE;i++)
        for (j=0;j<SIZE;j++)
        {
            dig_board[i][j] = in_board[i][j];
        }
}


SolveSudoku.h (數獨解題程式)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

/* 本Class主要針對唯一解的數獨題目來設計
    若題目有多重解,則不適用 */
#define SIZE 9
#include <vector>
using namespace std;

class SolveSudoku
{
    public:
        static int Ans; //數獨解個數計數器 (這是個Global的變數)
        SolveSudoku(vector<int>,int); //建構子
        void sol(); //解題函式
        vector < int > get_board(); //取得數獨解
        int getAns(); //取得數獨解的個數

    private:
        int i,j,k; //迴圈用變數
        int in_board[SIZE*SIZE]; //數獨題目
        int count[SIZE*SIZE]; //每個空格排除數字個數
        int Found; //已知空格數字的個數
        bool tag[SIZE*SIZE][SIZE]; //每個空格排除的數字
        void setAns(int A); //設定Ans變數
};


SolveSudoku.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164

#include "SolveSudoku.h"
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector>
using namespace std;

int SolveSudoku::Ans = 0; //數獨解個數計數器 (Global變數)
SolveSudoku::SolveSudoku(vector<int> board,int A)
{
    for (i=0;i<SIZE;i++)
        for (j=0;j<SIZE;j++)
        {
            in_board[i*SIZE+j] = board[i*SIZE+j];
        }
    //初始化 count, tag, Found變數
    for (i=0;i<SIZE;i++)
        for (j=0;j<SIZE;j++)
        {
            count[i*SIZE+j] = 0;
        }
    for (i=0;i<SIZE*SIZE;i++)
        for (j=0;j<SIZE;j++)
        {
            tag[i][j]=0;
        }
    Found = 0;
    setAns(A);
    sol();
}

void SolveSudoku::sol()
{
    int check, Max, iMax;
    /* 一般解法
    Found: 計算已知數字的個數
    count, tag: 掃描直、橫排又九宮格找出空格裡排除的數字
    */
    for (i=0;i<SIZE*SIZE;i++)
    {
        if (in_board[i])
        {
            Found++;
        }
        else
        {
            for (j=0;j<SIZE;j++)
            {
                if (in_board[(i/SIZE)*SIZE+j] && !tag[i][in_board[(i/SIZE)*SIZE+j]-1])
                {
                    count[i]++;
                    tag[i][in_board[(i/SIZE)*SIZE+j]-1] = true;
                }
                if (in_board[j*SIZE+i%SIZE] && !tag[i][in_board[j*SIZE+i%SIZE]-1])
                {
                    count[i]++;
                    tag[i][in_board[j*SIZE+i%SIZE]-1] = true;
                }
                check = in_board[((i/(SIZE*3))*3+j/3)*SIZE + ((i%SIZE)/3)*3+j%3] - 1;
                if (in_board[((i/(SIZE*3))*3+j/3)*SIZE + ((i%SIZE)/3)*3+j%3] && !tag[i][check])
                {
                    count[i]++;
                    tag[i][check] = true;

                }
            }
        }
    }
    /* 將排除數字有8個的空格填入正確的數字&增加Found個數
    並更新其直、橫排、九宮格內之空格的count, tag
    */
    for (i=0;i<SIZE*SIZE;i++)
    {
        if (!in_board[i] && count[i]==8)
        {
            count[i] = 0;
            Found++;
            for (j=0;j<SIZE;j++)
            {
                if(!tag[i][j])
                {
                    in_board[i] = j+1;
                    for (k=0;k<SIZE;k++)
                    {
                        if (!in_board[(i/SIZE)*SIZE+k] && !tag[(i/SIZE)*SIZE+k][j])
                        {
                            count[(i/SIZE)*SIZE+k]++;
                            tag[(i/SIZE)*SIZE+k][j] = true;
                        }
                        if (!in_board[k*SIZE+i%SIZE] && !tag[k*SIZE+i%SIZE][j])
                        {
                            count[k*SIZE+i%SIZE]++;
                            tag[k*SIZE+i%SIZE][j] = true;
                        }
                        check = ((i/(SIZE*3))*3+k/3)*SIZE + ((i%SIZE)/3)*3+k%3;
                        if (!in_board[check] && !tag[check][j])
                        {
                            count[check]++;
                            tag[check][j] = true;
                        }

                    }
                }
            }
            i = -1; //新填入空格數字就重新開始迴圈
        }

    }
    if (Found == 81) //全部空格數字都找到
    {
        setAns(Ans+1); //數獨解個數加1
    }
    else //一般解法不行,只好用暴力解了XD
    {
        //找出排除數字最多的空格
        Max = 0;
        iMax = -1;
        for (i=0;i<SIZE*SIZE;i++)
        {
            if (count[i] > Max)
            {
                Max = count[i];
                iMax = i;
            }
        }
        //暴力解開始!!
        //從排除數字最多的空格開始猜答案,把每個有可能的解法都跑一次
        for (j=0;j<SIZE;j++)
        {
            if(!tag[iMax][j])
            {
                in_board[iMax] = j+1;
                /*
                建立一個新的解題物件,並把目前數獨解個數代入
                若有新的解,將會更新數獨解(Ans)的個數,in_board則為其解
                若題目有多重解(Ans>1),in_board只會紀錄下最後一組的數獨解
                */
                SolveSudoku *tmp = new SolveSudoku(get_board(),getAns());
                //刪除此物件,並開始下一個可能解(節省記憶體的用量)
                delete tmp;
            }
        }
    }
}

vector < int > SolveSudoku::get_board() //取得數獨解答
{
    vector <int> out_board(SIZE*SIZE);
    for (i=0;i<SIZE*SIZE;i++)
    {
        out_board[i] = in_board[i];
    }
    return out_board;
}

int SolveSudoku::getAns() //取得數獨解答的個數
{
    return Ans;
}

void SolveSudoku::setAns(int A)//設定數獨解答的個數
{
    Ans = A;
}

1 則留言:

  1. 你好!
    想請問若 SolveSudoku *tmp = new SolveSudoku ( get_board () , getAns () ) ;
    執行後仍然沒有解
    那程式會如何跳出並執行 delete tmp ; 呢?
    感謝!

    回覆刪除