對回溯算法的理解(以數獨遊戲爲例,使用c++實現)

算法思想:ios

數獨遊戲的規則:算法

每一行都用到一、二、三、四、五、六、七、八、9位置不限;spa

每一列都用到一、二、三、四、五、六、七、八、9位置不限;code

每3×3的格子(共九個這樣的格子)都用到一、二、三、四、五、六、七、八、9位置不限。blog

遊戲的過程就是用一、二、三、四、五、六、七、八、9填充空白,並要求知足每行、每列、每一個九宮格都用到一、二、三、四、五、六、七、八、9。
實現方法:遊戲

因爲數獨都是9*9的,因此解空間有81層,每層有9個分支,咱們作的就是遍歷這個解空間。求獲得全部解的話,能夠在解出現的時候存下來:string

 1 #include "stdafx.h"
 2 #include<stdio.h>
 3 #include"stdlib.h"
 4 #include<iostream>
 5 #include<ctype.h>
 6 #include<vector>
 7 #include<algorithm>
 8 using namespace std;
 9 vector<vector<vector<char> > >sum;
10 bool check(vector<vector<char> > &board,int k)
11 {
12     int x = k / 9;
13     int y = k % 9;
14     for (int i = 0; i < 9; i++)
15     {
16         if (i != x&&board[x][y] == board[i][y])
17             return false;
18     }
19     for (int j = 0; j < 9; j++)
20     {
21         if (j != y&&board[x][y] == board[x][j])
22             return false;
23     }
24     for (int i = 3 * (x / 3); i < 3 * (x / 3 + 1); i++)
25         for (int j = 3 * (y/3); j < 3 * (y / 3 + 1); j++)
26             if (i != x&&j != y&&board[i][j] == board[x][y])
27                 return false;
28     return true;
29 }
30 void dfs(int num, vector<vector<char> >& board)
31 {
32     if (num == 81)
33     {
34         sum.push_back(board);
35         return;
36     }
37     else {
38         int x = num / 9;
39         int y = num % 9;
40         if (board[x][y] == '.')
41         {
42             for (int i = 1; i < 10; i++)
43             {
44                 board[x][y] = i + '0';
45                 if (check(board, num))
46                 {
47                     dfs(num + 1, board);
48                 }
49 
50             }
51             board[x][y] = '.';//若是沒有知足條件的數值,則恢復原來點值,向上回溯,改變父節點的值,從新往下計算,直到根節點第一個位置的值遍歷到9爲止。
52         }
53         else
54         {
55             dfs(num + 1, board);
56         }
57     }
58 }
59 void solveSudoku(vector<vector<char> >& board)
60 {
61     dfs(0,board);
62 }
63 int main()
64 {
65     vector<string> myboard({ "...748...","7........",".2.1.9...","..7...24.",".64.1.59.",".98...3..","...8.3.2.","........6","...2759.." });
66     vector<char> temp(9, '.');
67     vector<vector<char> > board(9, temp);
68     for (int i = 0; i<myboard.size(); i++) {
69         for (int j = 0; j<myboard[i].length(); j++) {
70             board[i][j] = myboard[i][j];
71         }
72     }
73     solveSudoku(board);
74     for (int k = 0; k<sum.size(); k++) {
75         for (int i = 0; i<sum[k].size(); i++) {
76             for (int j = 0; j<sum[k][i].size(); j++) {
77                 cout << sum[k][i][j] << " ";
78             }
79             cout << endl;
80         }
81         cout << "######" << endl;
82     }
83     cout << "sum is " << sum.size() << endl;
84     cout << "Hello world!" << endl;
85     system("pause");
86     return 0;
87 }

相關文章
相關標籤/搜索