数独代码(回溯)

Posted workharder

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数独代码(回溯)相关的知识,希望对你有一定的参考价值。

不知道从什么时候养成的习惯,过年也开始读书,写算法,也可能自己太穷想多挣钱,也可能想做出一款像王者荣耀那样巅峰产品,好向家里或者周围的人炫耀,哪种可能都有。尽管资质不高,距梦想差距很大,只要每天做正向积累,努力争取,总有机会被你抓住。

学习回溯算法后,做了习题0-1背包、八皇后、数独,归纳一下:递归函数用于进入下一层选择,如果下层选择失败,退回当层重置状态,再次进入下层试探。结束条件一定在递归函数开头。

回溯算法,数独代码:

技术图片
  1 #include <iostream>
  2 #include <math.h>
  3 #include <string>
  4 #include <vector>
  5 using namespace std;
  6 
  7 int MaxLen = 9;
  8 bool Check(vector<vector<char>>& board, int nX, int nY, char szWillSet)//都是从0开始 nX nY 为要检查的行列
  9 {
 10     //此行,此列,此9宫格
 11     for (int i = 0; i < MaxLen; ++i)
 12     {
 13         if (board[nX][i] == szWillSet || board[i][nY] == szWillSet)
 14         {
 15             return false;
 16         }
 17     }
 18     //计算坐标所在定点位置
 19     int nPointX = (nX/3) * 3;
 20     int nPointY = (nY/3) * 3;
 21     for (int i = nPointX; i < nPointX + 3; ++i)
 22     {
 23         if (i != nX)
 24         {
 25             for (int j = nPointY; j < nPointY+3; ++j)
 26             {
 27                 if (board[i][j] == szWillSet)
 28                 {
 29                     return false;
 30                 }
 31             }
 32         }
 33     }
 34     return true;
 35 }
 36 
 37 bool TrySet(vector<vector<char>>& board, int nNextX, int nNextY)//xy为考察到哪个坐标了
 38 {
 39     //考察下个坐标
 40     if (nNextY < MaxLen-1)
 41     {
 42         ++nNextY;
 43     }
 44     else if (nNextX < MaxLen-1)
 45     {
 46         ++nNextX;
 47         nNextY=0;
 48     }
 49     else
 50     {
 51         return true;
 52     }
 53     if (board[nNextX][nNextY] == .)
 54     {
 55         bool bHave = false;
 56         for (char szDian = 1; szDian <= 9; ++szDian)
 57         {
 58             if(Check(board,nNextX,nNextY,szDian))
 59             {
 60                 bHave = true;
 61                 board[nNextX][nNextY] = szDian;
 62                 
 63                 if(!TrySet(board, nNextX, nNextY))
 64                 {
 65                     board[nNextX][nNextY] = .;
 66                     bHave = false;
 67                 }
 68             }
 69         }
 70         if (!bHave)
 71         {
 72             return false;
 73         }
 74     }
 75     else
 76     {
 77         return TrySet(board, nNextX, nNextY);
 78     }
 79     return true;
 80 }
 81 void solveSudoku(vector<vector<char>>& board)
 82 {
 83     TrySet(board, 0, -1);
 84 }
 85 //------------------------------
 86 //----------以下是测试代码-------
 87 //------------------------------
 88 void Print(vector<vector<char>>& board)
 89 {
 90     for (int i = MaxLen-1; i>=0 ;--i)
 91     {
 92         for (int j = 0; j<MaxLen;++j)
 93         {
 94             cout<<board[i][j];
 95         }
 96         cout<<endl;
 97     }
 98     cout<<endl;
 99     cout<<"--------------------"<<endl;
100 }
101 int main()
102 {
103     vector<vector<char>> board;
104     char arrp1[9] = {5,3,.,.,7,.,.,.,.};
105     char arrp2[9] = {6,.,.,1,9,5,.,.,.};
106     char arrp3[9] = {.,9,8,.,.,.,.,6,.};
107     char arrp4[9] = {8,.,.,.,6,.,.,.,3};
108     char arrp5[9] = {4,.,.,8,.,3,.,.,1};
109     char arrp6[9] = {7,.,.,.,2,.,.,.,6};
110     char arrp7[9] = {.,6,.,.,.,.,2,8,.};
111     char arrp8[9] = {.,.,.,4,1,9,.,.,5};
112     char arrp9[9] = {.,.,.,.,8,.,.,7,9};
113     vector<char> vectemp9(arrp9, arrp9 + 9);
114     vector<char> vectemp8(arrp8, arrp8 + 9);
115     vector<char> vectemp7(arrp7, arrp7 + 9);
116     vector<char> vectemp6(arrp6, arrp6 + 9);
117     vector<char> vectemp5(arrp5, arrp5 + 9);
118     vector<char> vectemp4(arrp4, arrp4 + 9);
119     vector<char> vectemp3(arrp3, arrp3 + 9);
120     vector<char> vectemp2(arrp2, arrp2 + 9);
121     vector<char> vectemp1(arrp1, arrp1 + 9);
122     board.push_back(vectemp9);
123     board.push_back(vectemp8);
124     board.push_back(vectemp7);
125     board.push_back(vectemp6);
126     board.push_back(vectemp5);
127     board.push_back(vectemp4);
128     board.push_back(vectemp3);
129     board.push_back(vectemp2);
130     board.push_back(vectemp1);
131     Print(board);
132     solveSudoku(board);
133     Print(board);
134     return 0;
135 }
Study.cpp

 

以上是关于数独代码(回溯)的主要内容,如果未能解决你的问题,请参考以下文章

数独代码(回溯)

python中数独的回溯算法

数独回溯算法

Javascript递归回溯数独求解器

数独求解器回溯算法不起作用

回溯数独仅适用于 Python 3