├── pic ├── 1.png ├── 2.2.png ├── 3.1.png ├── 3.2.png ├── 3.3.png ├── 3.4.png ├── 3.5.png └── 3.6.png ├── src ├── Start.cpp ├── Global.h ├── CnfParser.cpp ├── main.cpp ├── DPLLSolver.cpp └── Sudoku.cpp ├── problems ├── problem2-50.cnf ├── sat-20.cnf ├── problem1-20.cnf ├── tst_v25_c100.cnf ├── problem6-50.cnf ├── problem9-100.cnf ├── problem8-50.cnf ├── problem3-100.cnf ├── unsat-5cnf-30.cnf ├── problem11-100.cnf ├── 7cnf20_90000_90000_7.shuffled-20.cnf ├── sud00009.cnf └── ais10.cnf └── README.md /pic/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Billy1900/DPLL-Algorithm/HEAD/pic/1.png -------------------------------------------------------------------------------- /pic/2.2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Billy1900/DPLL-Algorithm/HEAD/pic/2.2.png -------------------------------------------------------------------------------- /pic/3.1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Billy1900/DPLL-Algorithm/HEAD/pic/3.1.png -------------------------------------------------------------------------------- /pic/3.2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Billy1900/DPLL-Algorithm/HEAD/pic/3.2.png -------------------------------------------------------------------------------- /pic/3.3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Billy1900/DPLL-Algorithm/HEAD/pic/3.3.png -------------------------------------------------------------------------------- /pic/3.4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Billy1900/DPLL-Algorithm/HEAD/pic/3.4.png -------------------------------------------------------------------------------- /pic/3.5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Billy1900/DPLL-Algorithm/HEAD/pic/3.5.png -------------------------------------------------------------------------------- /pic/3.6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Billy1900/DPLL-Algorithm/HEAD/pic/3.6.png -------------------------------------------------------------------------------- /src/Start.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by PC on 2019/2/16. 3 | // 4 | #include "Global.h" 5 | 6 | void Start() { 7 | cout<<"Welcome to use the system!\n" 8 | <<"Please input the way you want to get into:" 9 | <<" Sudoku(For 1) SAT(For 2)\n"; 10 | } -------------------------------------------------------------------------------- /problems/problem2-50.cnf: -------------------------------------------------------------------------------- 1 | c 2 | c 3 | c 4 | p cnf 50 80 5 | 7 19 24 0 6 | 7 -19 26 0 7 | 7 24 -26 0 8 | 7 22 -24 0 9 | -7 22 46 0 10 | -7 22 -46 0 11 | 8 -22 -27 0 12 | -8 -22 -27 0 13 | -22 27 43 0 14 | 9 27 -50 0 15 | -9 -43 -50 0 16 | 38 -43 50 0 17 | 34 -38 50 0 18 | -34 -38 -41 0 19 | -34 41 -46 0 20 | -17 -34 41 0 21 | -6 17 -34 0 22 | -4 6 17 0 23 | 4 6 16 0 24 | 4 -16 40 0 25 | -16 19 -40 0 26 | -16 29 -40 0 27 | -19 -29 45 0 28 | 24 -29 -36 0 29 | -24 -36 -45 0 30 | 7 36 -45 0 31 | -15 36 -45 0 32 | -2 15 36 0 33 | 2 15 30 0 34 | 2 -30 35 0 35 | -8 -30 -35 0 36 | 8 -35 -47 0 37 | 8 -28 47 0 38 | 28 33 47 0 39 | 28 -33 37 0 40 | -21 -33 -37 0 41 | -14 21 -37 0 42 | 14 21 44 0 43 | -9 14 -44 0 44 | 9 -13 -44 0 45 | 13 31 -44 0 46 | 13 -31 42 0 47 | -18 -31 -42 0 48 | -12 18 -42 0 49 | 12 18 -48 0 50 | -1 12 18 0 51 | 1 48 -49 0 52 | 1 -32 49 0 53 | 1 -11 49 0 54 | 1 3 49 0 55 | 5 11 44 0 56 | -3 5 11 0 57 | -3 -5 -20 0 58 | -5 20 -25 0 59 | -5 23 25 0 60 | -23 25 -39 0 61 | 10 -23 25 0 62 | -10 -26 39 0 63 | -10 24 26 0 64 | -18 38 48 0 65 | 31 35 39 0 66 | -36 38 0 67 | -2 -6 37 0 68 | 34 -39 -47 0 69 | -2 3 -28 0 70 | -19 -20 42 0 71 | 16 32 -49 0 72 | -25 -27 46 0 73 | -12 16 31 0 74 | -2 -4 43 0 75 | -1 23 32 0 76 | 10 -17 40 0 77 | -21 34 45 0 78 | 20 -21 30 0 79 | 5 -13 33 0 80 | -15 -48 0 81 | -14 29 37 0 82 | 3 -14 -47 0 83 | -11 35 37 0 84 | 10 -32 -41 0 85 | -------------------------------------------------------------------------------- /src/Global.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by PC on 2019/2/27. 3 | // 4 | 5 | #ifndef WORKSHOP_GLOBAL_H 6 | #define WORKSHOP_GLOBAL_H 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | #define TRUE 1 14 | #define FALSE 0 15 | #define ROW 9 16 | #define COL 9 17 | #define NoAnwser -1 18 | typedef int status; 19 | 20 | typedef struct DataNode { 21 | int data = 0; 22 | DataNode *next{}; 23 | }DataNode; 24 | 25 | typedef struct HeadNode { 26 | int Num = 0; 27 | DataNode *right{}; 28 | HeadNode *down{}; 29 | }HeadNode; 30 | 31 | struct consequence { 32 | int value = -1;//存真值 真时为true-1,假时为false-0 33 | }; 34 | 35 | struct conse { 36 | int num = 0; 37 | int value = -1;//存真值 真时为true-1,假时为false-0 38 | }; 39 | 40 | void Start(); 41 | 42 | HeadNode* CreateClause(int &,string &); 43 | 44 | status DPLL(HeadNode *LIST,consequence *result); 45 | HeadNode* IsSingleClause(HeadNode*); 46 | status IsEmptyClause(HeadNode*); 47 | HeadNode* ADDSingleClause(HeadNode*,int); 48 | HeadNode* Duplication(HeadNode*); 49 | void DeleteHeadNode(HeadNode*,HeadNode*&); 50 | void DeleteDataNode(int,HeadNode*&); 51 | void show(struct consequence *,int); 52 | 53 | int Digit(int a[][COL], int i, int j); 54 | void randomFirstRow(int a0[], int n); 55 | void createSudoku(int a[][COL]); 56 | void createStartinggrid(const int a[][COL], int b[][COL], int numDigits); 57 | void print(const int a[][COL]); 58 | string ToCnf(int a[][COL],int holes); 59 | string createSudokuToFile(); 60 | status SudoDPLL(HeadNode *LIST,conse *result,int VARNUM); 61 | void SudokuShow(conse *result,int VARNUM); 62 | 63 | #endif //WORKSHOP_GLOBAL_H 64 | -------------------------------------------------------------------------------- /problems/sat-20.cnf: -------------------------------------------------------------------------------- 1 | c This Formula is generated by mcnf 2 | c 3 | c horn? no 4 | c forced? no 5 | c mixed sat? no 6 | c clause length = 3 7 | c 8 | p cnf 20 91 9 | 4 -18 19 0 10 | 3 18 -5 0 11 | -5 -8 -15 0 12 | -20 7 -16 0 13 | 10 -13 -7 0 14 | -12 -9 17 0 15 | 17 19 5 0 16 | -16 9 15 0 17 | 11 -5 -14 0 18 | 18 -10 13 0 19 | -3 11 12 0 20 | -6 -17 -8 0 21 | -18 14 1 0 22 | -19 -15 10 0 23 | 12 18 -19 0 24 | -8 4 7 0 25 | -8 -9 4 0 26 | 7 17 -15 0 27 | 12 -7 -14 0 28 | -10 -11 8 0 29 | 2 -15 -11 0 30 | 9 6 1 0 31 | -11 20 -17 0 32 | 9 -15 13 0 33 | 12 -7 -17 0 34 | -18 -2 20 0 35 | 20 12 4 0 36 | 19 11 14 0 37 | -16 18 -4 0 38 | -1 -17 -19 0 39 | -13 15 10 0 40 | -12 -14 -13 0 41 | 12 -14 -7 0 42 | -7 16 10 0 43 | 6 10 7 0 44 | 20 14 -16 0 45 | -19 17 11 0 46 | -7 1 -20 0 47 | -5 12 15 0 48 | -4 -9 -13 0 49 | 12 -11 -7 0 50 | -5 19 -8 0 51 | 1 16 17 0 52 | 20 -14 -15 0 53 | 13 -4 10 0 54 | 14 7 10 0 55 | -5 9 20 0 56 | 10 1 -19 0 57 | -16 -15 -1 0 58 | 16 3 -11 0 59 | -15 -10 4 0 60 | 4 -15 -3 0 61 | -10 -16 11 0 62 | -8 12 -5 0 63 | 14 -6 12 0 64 | 1 6 11 0 65 | -13 -5 -1 0 66 | -7 -2 12 0 67 | 1 -20 19 0 68 | -2 -13 -8 0 69 | 15 18 4 0 70 | -11 14 9 0 71 | -6 -15 -2 0 72 | 5 -12 -15 0 73 | -6 17 5 0 74 | -13 5 -19 0 75 | 20 -1 14 0 76 | 9 -17 15 0 77 | -5 19 -18 0 78 | -12 8 -10 0 79 | -18 14 -4 0 80 | 15 -9 13 0 81 | 9 -5 -1 0 82 | 10 -19 -14 0 83 | 20 9 4 0 84 | -9 -2 19 0 85 | -5 13 -17 0 86 | 2 -10 -18 0 87 | -18 3 11 0 88 | 7 -9 17 0 89 | -15 -6 -3 0 90 | -2 3 -13 0 91 | 12 3 -2 0 92 | -2 -3 17 0 93 | 20 -15 -16 0 94 | -5 -17 -19 0 95 | -20 -18 11 0 96 | -9 1 -5 0 97 | -19 9 17 0 98 | 12 -2 17 0 99 | 4 -16 -5 0 100 | -------------------------------------------------------------------------------- /problems/problem1-20.cnf: -------------------------------------------------------------------------------- 1 | c This Formula is generated by mcnf 2 | c 3 | c horn? no 4 | c forced? no 5 | c mixed sat? no 6 | c clause length = 3 7 | c 8 | p cnf 20 91 9 | 4 -18 19 0 10 | 3 18 -5 0 11 | -5 -8 -15 0 12 | -20 7 -16 0 13 | 10 -13 -7 0 14 | -12 -9 17 0 15 | 17 19 5 0 16 | -16 9 15 0 17 | 11 -5 -14 0 18 | 18 -10 13 0 19 | -3 11 12 0 20 | -6 -17 -8 0 21 | -18 14 1 0 22 | -19 -15 10 0 23 | 12 18 -19 0 24 | -8 4 7 0 25 | -8 -9 4 0 26 | 7 17 -15 0 27 | 12 -7 -14 0 28 | -10 -11 8 0 29 | 2 -15 -11 0 30 | 9 6 1 0 31 | -11 20 -17 0 32 | 9 -15 13 0 33 | 12 -7 -17 0 34 | -18 -2 20 0 35 | 20 12 4 0 36 | 19 11 14 0 37 | -16 18 -4 0 38 | -1 -17 -19 0 39 | -13 15 10 0 40 | -12 -14 -13 0 41 | 12 -14 -7 0 42 | -7 16 10 0 43 | 6 10 7 0 44 | 20 14 -16 0 45 | -19 17 11 0 46 | -7 1 -20 0 47 | -5 12 15 0 48 | -4 -9 -13 0 49 | 12 -11 -7 0 50 | -5 19 -8 0 51 | 1 16 17 0 52 | 20 -14 -15 0 53 | 13 -4 10 0 54 | 14 7 10 0 55 | -5 9 20 0 56 | 10 1 -19 0 57 | -16 -15 -1 0 58 | 16 3 -11 0 59 | -15 -10 4 0 60 | 4 -15 -3 0 61 | -10 -16 11 0 62 | -8 12 -5 0 63 | 14 -6 12 0 64 | 1 6 11 0 65 | -13 -5 -1 0 66 | -7 -2 12 0 67 | 1 -20 19 0 68 | -2 -13 -8 0 69 | 15 18 4 0 70 | -11 14 9 0 71 | -6 -15 -2 0 72 | 5 -12 -15 0 73 | -6 17 5 0 74 | -13 5 -19 0 75 | 20 -1 14 0 76 | 9 -17 15 0 77 | -5 19 -18 0 78 | -12 8 -10 0 79 | -18 14 -4 0 80 | 15 -9 13 0 81 | 9 -5 -1 0 82 | 10 -19 -14 0 83 | 20 9 4 0 84 | -9 -2 19 0 85 | -5 13 -17 0 86 | 2 -10 -18 0 87 | -18 3 11 0 88 | 7 -9 17 0 89 | -15 -6 -3 0 90 | -2 3 -13 0 91 | 12 3 -2 0 92 | -2 -3 17 0 93 | 20 -15 -16 0 94 | -5 -17 -19 0 95 | -20 -18 11 0 96 | -9 1 -5 0 97 | -19 9 17 0 98 | 12 -2 17 0 99 | 4 -16 -5 0 100 | -------------------------------------------------------------------------------- /problems/tst_v25_c100.cnf: -------------------------------------------------------------------------------- 1 | c 2 | c This file was generated by -=randSat=- cnf formula generator 3 | c 4 | p cnf 25 100 5 | -5 16 10 0 6 | -14 17 10 0 7 | 17 -19 13 0 8 | -14 -4 18 0 9 | -9 22 3 0 10 | 1 19 -9 0 11 | -7 18 -2 0 12 | 22 11 17 0 13 | -19 1 4 0 14 | -22 16 6 0 15 | 2 18 5 0 16 | 24 -16 1 0 17 | 24 14 17 0 18 | 15 -1 11 0 19 | 8 24 10 0 20 | 18 1 -16 0 21 | 1 12 4 0 22 | 8 -25 9 0 23 | 14 6 1 0 24 | -18 -1 23 0 25 | 16 13 -17 0 26 | 2 1 20 0 27 | -16 -24 13 0 28 | -16 13 2 0 29 | -25 9 17 0 30 | -21 -12 7 0 31 | 10 6 17 0 32 | 3 23 15 0 33 | -11 7 1 0 34 | 17 -14 5 0 35 | -17 11 -7 0 36 | -17 22 23 0 37 | 20 25 -4 0 38 | 13 12 7 0 39 | -11 8 7 0 40 | 21 -24 9 0 41 | 16 -15 -23 0 42 | 10 2 17 0 43 | -5 12 -18 0 44 | 4 -9 -24 0 45 | 18 13 1 0 46 | 15 -13 25 0 47 | 10 -25 14 0 48 | 22 3 -24 0 49 | 21 15 5 0 50 | 10 4 18 0 51 | -24 23 -7 0 52 | 15 -16 -21 0 53 | 18 21 2 0 54 | -18 19 -7 0 55 | 4 3 -16 0 56 | 14 13 4 0 57 | -8 -10 -7 0 58 | -13 15 -3 0 59 | 16 1 -20 0 60 | 21 15 4 0 61 | -9 -15 21 0 62 | 2 12 21 0 63 | 18 21 6 0 64 | 15 -3 6 0 65 | 12 20 15 0 66 | 6 24 23 0 67 | 6 -2 -17 0 68 | 15 -20 9 0 69 | 22 -13 -25 0 70 | 10 -21 -16 0 71 | 13 1 -14 0 72 | -1 24 -6 0 73 | 12 -13 -5 0 74 | 14 1 -23 0 75 | 11 18 25 0 76 | 6 10 5 0 77 | 11 9 23 0 78 | 4 -24 19 0 79 | 11 10 19 0 80 | 10 -2 7 0 81 | 8 9 13 0 82 | -10 -19 2 0 83 | -2 -15 -21 0 84 | -13 11 -1 0 85 | -14 10 17 0 86 | 16 25 -6 0 87 | 2 4 25 0 88 | -14 21 24 0 89 | 6 -2 22 0 90 | 16 -12 1 0 91 | -18 -12 22 0 92 | -7 -14 5 0 93 | -20 23 15 0 94 | -4 -9 -19 0 95 | -2 21 8 0 96 | -18 -14 -16 0 97 | 18 -24 8 0 98 | 11 7 23 0 99 | -4 -13 9 0 100 | -19 12 14 0 101 | 18 6 17 0 102 | 9 16 -4 0 103 | 14 2 -25 0 104 | -5 17 -11 0 105 | -------------------------------------------------------------------------------- /problems/problem6-50.cnf: -------------------------------------------------------------------------------- 1 | c 2 | c 3 | p cnf 50 100 4 | 4 37 -44 0 5 | -4 37 39 0 6 | -4 37 -39 0 7 | -37 40 -44 0 8 | -37 -40 -44 0 9 | 13 31 44 0 10 | -13 31 44 0 11 | 26 -31 44 0 12 | -2 -26 -31 0 13 | -26 -31 49 0 14 | 5 -26 -49 0 15 | -5 -27 -49 0 16 | -5 -39 -49 0 17 | 2 -5 43 0 18 | -2 39 43 0 19 | 39 -43 48 0 20 | -15 -43 -48 0 21 | -34 -43 -48 0 22 | 28 34 -48 0 23 | 18 -28 34 0 24 | -18 -28 -36 0 25 | -18 24 -28 0 26 | -3 -18 -24 0 27 | 3 -12 -24 0 28 | 5 10 12 0 29 | 10 12 -24 0 30 | -10 12 -20 0 31 | -10 20 25 0 32 | -10 -25 -35 0 33 | 20 23 -25 0 34 | 2 20 23 0 35 | -2 23 -39 0 36 | -19 -23 -25 0 37 | 19 -23 47 0 38 | 19 -20 47 0 39 | 13 43 -47 0 40 | 13 -23 -47 0 41 | -13 14 34 0 42 | 14 17 -34 0 43 | 14 -17 -34 0 44 | -13 22 -47 0 45 | -14 -22 29 0 46 | -21 -22 35 0 47 | -22 -29 -35 0 48 | -29 33 41 0 49 | 1 33 -41 0 50 | -1 33 -41 0 51 | 16 -29 -33 0 52 | -16 21 50 0 53 | 27 -33 42 0 54 | 27 -42 50 0 55 | -21 -27 50 0 56 | -11 -16 -33 0 57 | 8 22 -50 0 58 | 8 -16 -50 0 59 | -8 46 -50 0 60 | -6 -8 -46 0 61 | 1 -8 -17 0 62 | -1 -17 -46 0 63 | 6 42 45 0 64 | 6 42 -46 0 65 | 6 -37 -42 0 66 | 15 17 45 0 67 | -15 17 45 0 68 | 9 -42 -45 0 69 | -9 38 -45 0 70 | -7 -38 -45 0 71 | -1 -9 -38 0 72 | 7 15 -30 0 73 | 7 -30 -38 0 74 | 7 30 -41 0 75 | 10 30 -40 0 76 | 30 -40 41 0 77 | -4 15 40 0 78 | -15 40 41 0 79 | 4 -32 35 0 80 | 4 21 -32 0 81 | -21 -32 -35 0 82 | -30 35 46 0 83 | 21 22 46 0 84 | -11 26 28 0 85 | 29 -36 49 0 86 | 5 18 36 0 87 | -7 9 49 0 88 | 19 36 48 0 89 | -3 -14 16 0 90 | 24 31 32 0 91 | 2 24 32 0 92 | 3 38 48 0 93 | -7 8 16 0 94 | 1 -11 -19 0 95 | 11 29 36 0 96 | 3 -14 25 0 97 | -3 38 47 0 98 | -6 9 -12 0 99 | 11 26 0 100 | -12 25 28 0 101 | 18 -19 -36 0 102 | -6 -9 27 0 103 | -20 -27 32 0 -------------------------------------------------------------------------------- /src/CnfParser.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by PC on 2019/2/17. 3 | // 4 | #include "Global.h" 5 | 6 | HeadNode* CreateClause(int &VARNUM,string &filename) { 7 | //FileOpen 8 | string HFilePath = R"(C:\Users\PC\CLionProjects\Workshop\)"; 9 | string path = HFilePath + filename; 10 | ifstream fis(path); 11 | if(!fis){ 12 | cout<<"File can not open."; 13 | exit; 14 | } 15 | char ch; 16 | char buf[100]; 17 | fis>>ch; 18 | while (ch != 'p') { 19 | fis.getline(buf,100); 20 | fis>>ch; 21 | } 22 | string cnf; 23 | int VarNum,ClauseNum; 24 | fis>>cnf>>VarNum>>ClauseNum; 25 | fis.get(); 26 | 27 | /*and write into 2 dimensional link lists*/ 28 | //loading the initial node 29 | HeadNode* HEAD = new HeadNode; 30 | HeadNode* headRear = HEAD; 31 | HeadNode* END = new HeadNode; 32 | for (int i = 0 ; i < ClauseNum ; i++) { 33 | //load on the data lists 34 | int temp; 35 | fis>>temp; 36 | //load the first data node 37 | DataNode* front = new DataNode; 38 | front->data = temp; 39 | headRear->right = front; 40 | headRear->Num++; 41 | //the >2th data loading 42 | fis >> temp; 43 | while (temp != 0) { 44 | DataNode* rear = new DataNode; 45 | front->next = rear; 46 | rear->data = temp; 47 | front = front->next; 48 | headRear->Num++; 49 | fis >> temp; 50 | } 51 | front->next = nullptr; 52 | fis.get();//换行符 53 | HeadNode *tp = new HeadNode; 54 | headRear->down = tp; 55 | END = headRear; 56 | headRear = headRear->down; 57 | } 58 | END->down = nullptr; 59 | 60 | //output link lists 61 | HeadNode* Phead = HEAD; 62 | DataNode* front; 63 | cout<<"CnfParser\n"; 64 | while(Phead != nullptr) { 65 | front = Phead->right; 66 | while(front != nullptr) { 67 | cout<data<<" "; 68 | front = front->next; 69 | } 70 | cout<down; 72 | } 73 | 74 | VARNUM = VarNum; 75 | return HEAD; 76 | } -------------------------------------------------------------------------------- /src/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Modular/Global.h" 2 | 3 | 4 | 5 | int main() { 6 | Start(); 7 | int choice = 0; 8 | cin>>choice; 9 | while (choice){ 10 | if(choice == 1) { 11 | string filename = createSudokuToFile(); 12 | int VARNUM; 13 | HeadNode* LIST = CreateClause(VARNUM,filename); 14 | conse SudoResult[VARNUM];//记录最终的真假值 15 | clock_t StartTime,EndTime; 16 | StartTime = clock(); 17 | int value = SudoDPLL(LIST,SudoResult,VARNUM); 18 | EndTime = clock(); 19 | cout<<"T "<<(double)(EndTime-StartTime)/CLOCKS_PER_SEC*1000.0<<" ms\n"; 20 | if(value == 1) 21 | SudokuShow(SudoResult,VARNUM); 22 | else 23 | cout<<"there is no anwser."; 24 | } 25 | 26 | else if (choice == 2) {//SAT 27 | int VARNUM; 28 | string filename = "sud00009.cnf"; 29 | HeadNode* LIST = CreateClause(VARNUM,filename); 30 | consequence result[VARNUM];//记录最终的真假值 31 | clock_t StartTime,EndTime; 32 | //cout<<"Result: \n"; 33 | //StartTime = clock(); 34 | //int value = DPLL(LIST,result); 35 | //EndTime = clock(); 36 | //if(value) { 37 | // cout << "S " << TRUE << endl; 38 | // show(result, VARNUM);//输出解 39 | //} 40 | //else { 41 | // cout << "S " << NoAnwser << endl; 42 | // cout<<"V "<typedef struct HeadNode { 34 | 35 | int Num = 0; 36 | 37 | DataNode *right{}; 38 | 39 | HeadNode *down{}; 40 | 41 | }HeadNode; 42 | 43 | 子句头节点有三个成员;Num记录该行子句的数据个数,指针right指向到子句第一个数据节点,指针down指向到下一行子句。 44 | 45 | 2)子句数据节点定义: 46 | 47 |
typedef struct DataNode {
 48 |     
 49 |     int data = 0;
 50 |     
 51 |     DataNode *next{};
 52 | 
 53 | }DataNode;
54 | 55 | 数据节点有两个成员;data记录该数据节点的值,指针next指向下一个数据节点。 56 | 57 | 3)存储真值结构定义: 58 | 59 |
struct consequence {
 60 |     
 61 |     int value = -1;//存真值 真时为true-1,假时为false-0
 62 | 
 63 | };
64 | 65 | value存入数组的某一元素的真值;若为真,value = 1;若为假,value = 0。 66 | 67 | 4)存储数独真值结构定义: 68 | 69 |
struct conse {
 70 |     
 71 |     int num = 0;
 72 |     
 73 |     int value = -1;//存真值 真时为true-1,假时为false-0
 74 | 
 75 | };
76 | 77 | 结构体conse有两个成员;num存储数独转换为cnf公式的数值,value存储真值;若为真,value = 1;若为假,value = 0。 78 | 79 | 5)系统总体存储结构关系 80 | 81 | 主要运用结构体HeadNode和DataNode存储数据,根据两种结构体的数据成员,下图展示出结构体之间的逻辑关系。 82 | 83 | ![image](https://github.com/Billy1900/DPLL-Algorithm/blob/master/pic/3.1.png) 84 | 85 | ## 3. 主要算法设计 86 | 87 | 88 | ## 3.1 CnfParser模块 89 | 90 | 91 | 该模块主要功能是将cnf文件中的数据读取出来并将它存储到数据结构中。根据文件的特点,现做如下算法设计。算法流程如下图: 92 | 93 | ![image](https://github.com/Billy1900/DPLL-Algorithm/blob/master/pic/3.2.png) 94 | 95 | ## 3.2 DPLLSolver模块 96 |
 97 | 
 98 | DPLL算法思想如下:
 99 | 
100 | Status DPLL( S) {
101 | 
102 | /* S为公式对应的子句集。若其满足,返回TURE;否则返回FALSE. */
103 | 
104 | while(S中存在单子句) { 
105 | 
106 | 在S中选一个单子句L,并将单子句装入结果数组,value置为1;
107 | 
108 | 依据单子句规则,利用L化简S;
109 | 
110 | if S = Φ return(TRUE);
111 | 
112 | else if (S中有空子句 ) return(FALSE);
113 | 
114 | }
115 | 
116 | V = S第一个数据节点数值
117 | 
118 | if DPLL(S ∪v )return(TURE);
119 | 
120 | return DPLL(S ∪¬v);
121 | 
122 | }
123 | 124 | 1)HeadNode* IsSingleClause(HeadNode*)函数:寻找是否存在单子句。遍历头节点,若任意一行的Num存在为1的情况,即证明存在单子句,并返回单子句的指针。 125 | 126 | 2)void DeleteHeadNode(HeadNode* des, HeadNode* &src) 函数:在src指向的子句集中删掉des子句。此处应用了src的引用指针变量。 127 | 128 | 3)void DeleteDataNode(int data.HeadNode*&src) 函数:在src指向的子句集中若有节点的数值与data相等,则删除该变元所在的子句;若只是绝对值相等,则删除该变元。 129 | 130 | 4)status IsEmptyClause(HeadNode* src)函数:判断src函数中是否存在空子句,即遍历头节点,若存在头节点不为空且Num值为0的,即表示存在空子句,返回TRUE;否则,返回FALSE。 131 | 132 | 5)HeadNode* ADDSingleClause(HeadNode* src,int Var)函数:将变元Var装载成为单子句,并将该单子句装载到src的子句集中,依照函数内容,该单子句始终添加到头节点,最终返回头节点指针。 133 | 134 | 6)HeadNode* Duplication(HeadNode* src)函数:将src所指向的子句集复制一个副本,并将副本的头指针返回。 135 | 136 | ## 3.3 Sudoku模块 137 | 138 | 139 | 该模块功能是①创建数独终盘,②通过挖洞法创建数独初盘,○3将数独初盘转换成SAT问题并转换成cnf公式输出到文件,并返回文件名。 140 | 141 | 1)CreateSudoku模块: 142 | 143 | 伪代码如下: 144 | 145 |
void createSudoku(int a[][COL]) { //生成数独
146 |     
147 |     randomFirstRow(a[0],COL);//随机生成第一行
148 |     
149 |     Digit(a,1,0);//递归生成后i行
150 | 
151 | }
152 | 
153 | randomFirstRow函数算法流程: 154 | 155 | ![image](https://github.com/Billy1900/DPLL-Algorithm/blob/master/pic/3.3.png) 156 | 157 | Digit函数算法流程: 158 | 159 | ![image](https://github.com/Billy1900/DPLL-Algorithm/blob/master/pic/3.4.png) 160 | 161 | 2)数独初盘生成模块 162 | createStartinggrid函数算法流程: 163 | 164 | ![image](https://github.com/Billy1900/DPLL-Algorithm/blob/master/pic/3.5.png) 165 | 166 | 3)数独问题转换为SAT问题 167 | 168 | ToCnf函数模块: 169 | 170 | 以下为函数算法说明: 171 | ![image](https://github.com/Billy1900/DPLL-Algorithm/blob/master/pic/3.6.png) 172 | -------------------------------------------------------------------------------- /src/DPLLSolver.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by PC on 2019/2/18. 3 | // 4 | #include "Global.h" 5 | 6 | status IsEmptyClause(HeadNode* LIST) { 7 | HeadNode* PHead = LIST; 8 | while (PHead != nullptr) { 9 | if(PHead->Num == 0) 10 | return TRUE; 11 | PHead = PHead->down; 12 | } 13 | return FALSE; 14 | } 15 | 16 | HeadNode* IsSingleClause(HeadNode* Pfind) { 17 | while (Pfind != nullptr ) { 18 | if(Pfind->Num == 1) 19 | return Pfind; 20 | Pfind = Pfind->down; 21 | } 22 | return nullptr; 23 | } 24 | 25 | HeadNode* Duplication(HeadNode* LIST) { //此处检验传参正常,开始检查复制有无逻辑错误 26 | HeadNode* SrcHead = LIST; 27 | HeadNode* ReHead = new HeadNode;//新链表的头节点 28 | ReHead->Num = SrcHead->Num;//复制第一个头节点 29 | HeadNode* Phead = ReHead;//Phead创建头节点 30 | DataNode *ReData = new DataNode;//新链表的数据节点 31 | DataNode *FirstSrcData = SrcHead->right;//用于创建第一行的第一个数据节点 32 | ReData->data = FirstSrcData->data;//新链表的第一个数据节点的数值 33 | Phead->right = ReData; 34 | for (FirstSrcData = FirstSrcData->next;FirstSrcData != nullptr; FirstSrcData = FirstSrcData->next) {//第一行链表复制完成 35 | DataNode *NewDataNode = new DataNode; 36 | NewDataNode->data = FirstSrcData->data; 37 | ReData->next = NewDataNode; 38 | ReData = ReData->next; 39 | } 40 | //此下行数节点的复制 >=2th 41 | for(SrcHead = SrcHead->down; SrcHead != nullptr ; SrcHead = SrcHead->down) { 42 | HeadNode* NewHead = new HeadNode; 43 | DataNode* NewData = new DataNode; 44 | NewHead->Num = SrcHead->Num; 45 | Phead->down = NewHead; 46 | Phead = Phead->down; 47 | DataNode* SrcData = SrcHead->right; 48 | NewData->data = SrcData->data; 49 | Phead->right = NewData;//第一个数据节点 50 | for (SrcData = SrcData->next;SrcData != nullptr; SrcData = SrcData->next) {//此行剩下的数据节点 51 | DataNode* node = new DataNode; 52 | node->data = SrcData->data; 53 | NewData->next = node; 54 | NewData = NewData->next; 55 | } 56 | NewData->next = nullptr; 57 | } 58 | Phead->down = nullptr; 59 | 60 | return ReHead; 61 | } 62 | 63 | HeadNode* ADDSingleClause(HeadNode* LIST,int Var) { //所加的单子句位于链表的头 64 | HeadNode* AddHead = new HeadNode; 65 | DataNode* AddData = new DataNode; 66 | AddData->data = Var; 67 | AddData->next = nullptr; 68 | AddHead->right = AddData; 69 | AddHead->Num = 1; 70 | AddHead->down = LIST; 71 | LIST = AddHead; 72 | return LIST; 73 | } 74 | 75 | void DeleteDataNode(int temp,HeadNode *&LIST) { 76 | for (HeadNode* pHeadNode = LIST; pHeadNode != nullptr ; pHeadNode = pHeadNode->down) 77 | for (DataNode *rear = pHeadNode->right; rear != nullptr ; rear = rear->next) { 78 | if (rear->data == temp) //相等删除整行 79 | DeleteHeadNode(pHeadNode,LIST); 80 | else if (abs(rear->data) == abs(temp)) { //仅仅是绝对值相等铲除该节点 81 | if(rear == pHeadNode->right) { //头节点删除 82 | pHeadNode->right = rear->next; 83 | pHeadNode->Num--; 84 | } 85 | else{ //删除普通节点 86 | for (DataNode* front = pHeadNode->right; front != nullptr; front= front->next) 87 | if(front->next == rear) { 88 | front->next = rear->next; 89 | pHeadNode->Num--; 90 | } 91 | } 92 | } 93 | } 94 | } 95 | 96 | void DeleteHeadNode(HeadNode *Clause,HeadNode *&LIST) { 97 | if (!Clause) return; 98 | if(Clause == LIST) 99 | LIST = Clause->down; 100 | else { 101 | for (HeadNode *front = LIST; front != nullptr; front = front->down) 102 | if (front->down == Clause) { 103 | front->down = Clause->down; 104 | } 105 | } 106 | } 107 | 108 | void show(struct consequence *result,int VarNum) { 109 | cout<<"V "; 110 | for(int i = 0; i < VarNum; i++) { 111 | if (result[i].value == TRUE) 112 | cout<right->data > 0 ? result[abs(SingleClause->right->data)-1].value = TRUE : result[abs(SingleClause->right->data)-1].value = FALSE; 127 | int temp = SingleClause->right->data; 128 | DeleteHeadNode(SingleClause,LIST);//删除单子句这一行 129 | DeleteDataNode(temp,LIST);//删除相等或相反数的节点 130 | if(!LIST) return TRUE; 131 | else if(IsEmptyClause(LIST)) return FALSE; 132 | Pfind = LIST; 133 | SingleClause = IsSingleClause(Pfind);//回到头节点继续进行检测是否有单子句 134 | } 135 | //分裂策略 136 | int Var = LIST->right->data;//选取变元 137 | HeadNode* replica = Duplication(LIST);//存放LIST的副本replica 138 | HeadNode *temp1 = ADDSingleClause(LIST,Var);//装载变元成为单子句 139 | if(DPLL(temp1,result)) return TRUE; 140 | else { 141 | HeadNode *temp2 = ADDSingleClause(replica,-Var); 142 | return DPLL(temp2,result); 143 | } 144 | } -------------------------------------------------------------------------------- /problems/problem8-50.cnf: -------------------------------------------------------------------------------- 1 | c 2 | c 3 | p cnf 50 300 4 | 1 21 24 0 5 | -1 7 11 0 6 | 5 -11 48 0 7 | 5 7 -11 0 8 | 5 21 34 0 9 | -5 24 34 0 10 | -24 34 42 0 11 | 21 -24 -42 0 12 | 16 17 42 0 13 | 16 -17 42 0 14 | 15 31 42 0 15 | 15 -16 -31 0 16 | 15 -34 39 0 17 | 38 -39 41 0 18 | -34 -38 -39 0 19 | 15 40 -41 0 20 | 15 -40 -41 0 21 | 21 45 50 0 22 | 21 -34 -45 0 23 | -15 28 37 0 24 | -15 21 -50 0 25 | -15 21 -30 0 26 | -28 -34 -50 0 27 | 9 -19 50 0 28 | 16 -19 -50 0 29 | 9 -16 50 0 30 | 9 -21 -50 0 31 | -19 -21 32 0 32 | 4 -19 -21 0 33 | -4 -19 -21 0 34 | 17 23 -32 0 35 | 17 -23 -28 0 36 | 6 9 26 0 37 | 9 -17 26 0 38 | -9 24 26 0 39 | 12 22 24 0 40 | 12 -22 39 0 41 | 12 -22 24 0 42 | 7 24 38 0 43 | -7 -19 38 0 44 | -19 24 -26 0 45 | 1 -21 -24 0 46 | -1 -21 -24 0 47 | 19 24 -41 0 48 | 19 -24 44 0 49 | 19 -41 -44 0 50 | 11 19 20 0 51 | -11 19 20 0 52 | -12 19 20 0 53 | 14 -20 41 0 54 | -14 15 41 0 55 | 22 27 -45 0 56 | -20 -22 27 0 57 | 1 -20 -45 0 58 | -1 -20 -27 0 59 | 6 19 41 0 60 | 9 31 33 0 61 | -2 27 -31 0 62 | -2 9 -31 0 63 | -2 -9 38 0 64 | -2 -6 -38 0 65 | 1 -2 -33 0 66 | -1 -2 -33 0 67 | -15 36 50 0 68 | 4 6 31 0 69 | -6 31 -50 0 70 | 4 -6 36 0 71 | -15 -36 48 0 72 | -4 -15 50 0 73 | 5 -6 50 0 74 | -4 5 -48 0 75 | -5 -6 -48 0 76 | 30 38 42 0 77 | -5 30 -38 0 78 | 10 30 -42 0 79 | 30 -48 50 0 80 | -6 18 -48 0 81 | 6 18 -48 0 82 | -6 18 -37 0 83 | -6 -18 50 0 84 | 5 10 25 0 85 | 2 5 -25 0 86 | 2 -5 10 0 87 | -20 23 39 0 88 | 2 -23 37 0 89 | -23 30 37 0 90 | -23 37 39 0 91 | -20 37 39 0 92 | 4 -10 42 0 93 | 4 37 -42 0 94 | -4 -10 18 0 95 | -4 -14 18 0 96 | -20 -23 -39 0 97 | -10 -18 37 0 98 | 32 38 41 0 99 | -32 38 -50 0 100 | 3 32 -37 0 101 | -2 -17 -37 0 102 | 30 39 47 0 103 | -3 14 47 0 104 | -14 -24 47 0 105 | 29 -37 43 0 106 | -3 43 47 0 107 | -3 -39 -43 0 108 | 2 -3 -47 0 109 | -3 -32 -47 0 110 | 2 -37 -47 0 111 | 2 -17 48 0 112 | 2 26 -32 0 113 | 5 -32 -48 0 114 | -26 -32 -48 0 115 | -30 34 44 0 116 | -17 -30 -44 0 117 | 24 -30 -34 0 118 | 6 -24 -30 0 119 | 2 -24 -30 0 120 | -10 -30 -34 0 121 | 3 37 45 0 122 | 3 -37 45 0 123 | -3 4 45 0 124 | -3 42 45 0 125 | -3 30 45 0 126 | -10 17 -25 0 127 | 8 -37 43 0 128 | -8 -37 43 0 129 | 20 23 -50 0 130 | 23 33 -50 0 131 | 28 -33 43 0 132 | 28 -33 -43 0 133 | 8 -28 -38 0 134 | 8 -38 -45 0 135 | -8 23 -28 0 136 | -8 -21 -28 0 137 | 17 25 29 0 138 | 31 -38 40 0 139 | 9 31 -40 0 140 | -9 31 -40 0 141 | -18 31 -40 0 142 | 22 25 -43 0 143 | 17 -38 44 0 144 | -29 -31 32 0 145 | 17 -29 -48 0 146 | -17 -31 35 0 147 | -29 -31 -35 0 148 | 25 -29 -47 0 149 | 25 -39 -47 0 150 | 6 -29 -44 0 151 | -6 32 -44 0 152 | -29 38 -44 0 153 | -25 -29 -38 0 154 | -8 27 47 0 155 | -8 -27 44 0 156 | -8 -22 -44 0 157 | 7 8 41 0 158 | 7 -41 48 0 159 | 13 27 45 0 160 | -7 -13 45 0 161 | -7 15 -45 0 162 | -7 27 -31 0 163 | -7 16 28 0 164 | -7 10 28 0 165 | -7 -28 -31 0 166 | 9 -10 -31 0 167 | 8 10 -33 0 168 | 10 25 -40 0 169 | -25 -33 -40 0 170 | -10 25 -33 0 171 | 8 -33 48 0 172 | 8 -25 -48 0 173 | 23 33 48 0 174 | 22 33 48 0 175 | 12 -22 33 0 176 | 12 -13 14 0 177 | -12 14 39 0 178 | -13 -14 39 0 179 | 6 -13 49 0 180 | -13 19 49 0 181 | 21 30 -49 0 182 | -13 -21 -49 0 183 | -9 -39 -49 0 184 | 4 46 -49 0 185 | -4 22 46 0 186 | -4 -22 37 0 187 | -4 -13 -37 0 188 | -39 -46 -49 0 189 | 12 -13 41 0 190 | 12 -13 -41 0 191 | -19 33 41 0 192 | -19 33 -41 0 193 | -9 26 -27 0 194 | 2 16 -26 0 195 | 14 16 -44 0 196 | -14 16 -26 0 197 | -2 16 -26 0 198 | -11 -12 13 0 199 | -11 13 -14 0 200 | 3 22 25 0 201 | 22 -27 34 0 202 | -25 -27 -34 0 203 | 3 -22 -27 0 204 | 1 11 12 0 205 | 11 -17 19 0 206 | 1 11 -17 0 207 | 1 -9 -11 0 208 | -1 -9 32 0 209 | 7 -9 12 0 210 | -7 -9 32 0 211 | -12 -27 32 0 212 | 16 26 44 0 213 | 11 -16 30 0 214 | 11 -16 44 0 215 | -15 -16 44 0 216 | 11 13 26 0 217 | -12 44 45 0 218 | 43 44 -45 0 219 | -21 43 -45 0 220 | -12 -43 -45 0 221 | 41 42 -44 0 222 | -12 17 -41 0 223 | -12 -17 -41 0 224 | -16 -28 47 0 225 | -16 -28 -47 0 226 | -28 -29 -42 0 227 | -1 -16 25 0 228 | -25 43 50 0 229 | -25 43 -50 0 230 | -1 27 -43 0 231 | -25 36 -43 0 232 | 13 -36 -43 0 233 | -27 -36 -43 0 234 | 1 10 18 0 235 | 10 -42 47 0 236 | -1 10 -47 0 237 | -10 -16 18 0 238 | 28 40 47 0 239 | 7 27 48 0 240 | 3 27 40 0 241 | 3 40 -45 0 242 | 3 7 -27 0 243 | 3 40 -47 0 244 | -23 40 47 0 245 | -23 -26 -47 0 246 | -3 -26 40 0 247 | 28 42 -46 0 248 | 28 -42 -46 0 249 | -30 38 -46 0 250 | -18 -38 -46 0 251 | -14 -18 -46 0 252 | 1 -5 46 0 253 | -5 -12 46 0 254 | -1 -5 46 0 255 | 13 23 32 0 256 | 13 15 23 0 257 | 13 33 -49 0 258 | -2 13 -49 0 259 | 23 -32 -42 0 260 | 34 -42 -46 0 261 | -23 29 -32 0 262 | 15 -23 -29 0 263 | -15 -32 -49 0 264 | 28 -34 -42 0 265 | 7 29 -35 0 266 | -7 29 -35 0 267 | 29 31 -35 0 268 | 20 -35 46 0 269 | -20 -35 46 0 270 | -18 33 -39 0 271 | -18 -33 34 0 272 | -18 -34 46 0 273 | 4 39 46 0 274 | 8 35 -36 0 275 | -8 35 -36 0 276 | 11 22 36 0 277 | 5 -11 22 0 278 | 14 36 40 0 279 | 18 36 -40 0 280 | -5 -18 36 0 281 | -5 14 -39 0 282 | 14 -22 26 0 283 | 14 -22 49 0 284 | -26 36 -49 0 285 | -4 -14 -24 0 286 | 21 -30 48 0 287 | 18 -20 -36 0 288 | 6 -15 -46 0 289 | -10 20 29 0 290 | 4 20 -44 0 291 | 34 -35 -40 0 292 | 35 36 -36 0 293 | 29 -35 49 0 294 | -8 -36 0 295 | 8 26 34 0 296 | -11 -14 49 0 297 | -8 -26 35 0 298 | -11 35 -43 0 299 | 20 29 -40 0 300 | 20 35 49 0 301 | 17 -35 49 0 302 | 35 -46 49 0 303 | 6 35 49 0 -------------------------------------------------------------------------------- /problems/problem3-100.cnf: -------------------------------------------------------------------------------- 1 | c 2 | p cnf 100 340 3 | 50 59 89 0 4 | 44 -50 59 0 5 | -50 59 89 0 6 | 23 -59 72 0 7 | -23 -59 89 0 8 | 4 72 -89 0 9 | -4 80 -89 0 10 | 23 72 -80 0 11 | -23 72 -80 0 12 | -72 89 -96 0 13 | 79 88 -96 0 14 | -72 -79 -96 0 15 | 25 64 -88 0 16 | -25 64 -72 0 17 | -64 -72 -96 0 18 | 56 65 90 0 19 | -56 58 65 0 20 | -58 65 96 0 21 | -27 -65 90 0 22 | -27 -90 96 0 23 | 8 27 -67 0 24 | -8 -67 94 0 25 | -8 43 -67 0 26 | 27 -67 96 0 27 | 14 67 75 0 28 | 14 -57 75 0 29 | -57 -66 75 0 30 | 67 68 75 0 31 | 6 -14 75 0 32 | -6 -14 -68 0 33 | 30 67 -75 0 34 | -13 -30 52 0 35 | -13 -30 -52 0 36 | 5 13 -30 0 37 | -5 -31 -75 0 38 | -5 79 -84 0 39 | -5 41 -84 0 40 | -41 48 -84 0 41 | -5 -48 -84 0 42 | -3 13 -30 0 43 | 19 29 -45 0 44 | 3 -19 -45 0 45 | -29 -45 54 0 46 | -29 -45 -54 0 47 | 58 84 87 0 48 | -25 -58 87 0 49 | 64 79 -87 0 50 | -64 79 -87 0 51 | 38 39 -79 0 52 | -38 54 84 0 53 | 17 -54 -79 0 54 | -38 -54 -79 0 55 | -25 84 88 0 56 | -25 31 86 0 57 | 31 -86 -90 0 58 | 31 74 -88 0 59 | 31 60 -74 0 60 | 57 -60 -74 0 61 | 14 -57 -74 0 62 | -14 -79 -88 0 63 | -4 86 98 0 64 | 68 71 -86 0 65 | 68 -71 -86 0 66 | -68 -86 98 0 67 | -4 64 95 0 68 | 27 31 -95 0 69 | 27 -58 -95 0 70 | -4 66 -95 0 71 | -27 -66 -95 0 72 | -64 87 88 0 73 | -4 -64 -87 0 74 | -4 69 -98 0 75 | 60 -69 84 0 76 | -60 -69 84 0 77 | 26 -43 45 0 78 | -26 29 -43 0 79 | -26 45 56 0 80 | -26 -43 -56 0 81 | 3 25 -47 0 82 | 22 45 48 0 83 | 4 -22 45 0 84 | 4 39 40 0 85 | 4 -40 59 0 86 | 4 39 -40 0 87 | 41 54 91 0 88 | -41 47 54 0 89 | 47 74 91 0 90 | 43 -74 91 0 91 | 43 47 -86 0 92 | -18 -39 42 0 93 | 3 -18 47 0 94 | -18 -47 82 0 95 | -18 -21 -42 0 96 | 3 -18 -82 0 97 | -3 -39 51 0 98 | -18 -51 94 0 99 | -3 -51 -94 0 100 | -39 -78 -91 0 101 | -78 -91 -98 0 102 | 18 -63 -91 0 103 | 52 -85 86 0 104 | 30 78 86 0 105 | -30 78 -85 0 106 | 63 -80 95 0 107 | -80 -93 95 0 108 | 63 86 -95 0 109 | 18 24 63 0 110 | 27 66 78 0 111 | 13 -27 66 0 112 | 3 -13 66 0 113 | -3 66 78 0 114 | 11 34 80 0 115 | -11 15 77 0 116 | -11 34 39 0 117 | -11 43 -77 0 118 | 34 -39 100 0 119 | -11 80 -100 0 120 | 34 -43 85 0 121 | -24 80 -88 0 122 | -34 -37 88 0 123 | -24 -66 72 0 124 | -66 -81 85 0 125 | -72 -81 -85 0 126 | 12 -34 90 0 127 | 12 -90 98 0 128 | 7 12 -84 0 129 | -12 16 94 0 130 | -16 -34 94 0 131 | -34 -94 98 0 132 | -7 -34 98 0 133 | 37 -38 50 0 134 | 37 -50 82 0 135 | 37 -38 -82 0 136 | -14 37 -38 0 137 | -38 -72 -98 0 138 | 83 88 -98 0 139 | 35 -83 -98 0 140 | 17 38 -83 0 141 | 23 38 -98 0 142 | 17 -35 44 0 143 | 10 -17 -35 0 144 | -39 -47 -83 0 145 | 10 50 88 0 146 | -50 76 -83 0 147 | 10 -76 -83 0 148 | 32 -44 -88 0 149 | 10 26 -32 0 150 | -26 -32 67 0 151 | -32 -67 -83 0 152 | 17 -23 40 0 153 | 7 -23 40 0 154 | -7 -23 40 0 155 | -10 83 97 0 156 | -17 83 -97 0 157 | 13 57 78 0 158 | -13 57 83 0 159 | -10 -13 -17 0 160 | 24 57 -78 0 161 | -24 44 -78 0 162 | -24 -44 -78 0 163 | 9 -19 -40 0 164 | -9 -19 -40 0 165 | -10 66 100 0 166 | -10 -66 100 0 167 | -57 68 -100 0 168 | 19 -57 60 0 169 | 19 90 -100 0 170 | 6 -8 -100 0 171 | -6 -8 -100 0 172 | 8 -20 -60 0 173 | -58 -68 -90 0 174 | 58 -60 77 0 175 | 2 20 -93 0 176 | -2 20 -93 0 177 | 70 -77 93 0 178 | 8 76 -77 0 179 | 67 -70 76 0 180 | -8 -70 76 0 181 | 42 58 99 0 182 | 20 42 58 0 183 | -33 56 81 0 184 | 42 -56 81 0 185 | -33 68 -81 0 186 | 20 -33 -68 0 187 | -69 -76 91 0 188 | -69 -76 93 0 189 | -53 70 -76 0 190 | -53 -70 93 0 191 | 51 69 73 0 192 | 51 69 87 0 193 | 51 69 93 0 194 | -42 -87 -93 0 195 | 29 -51 53 0 196 | -42 53 71 0 197 | 53 71 -80 0 198 | -29 54 69 0 199 | -22 -51 -54 0 200 | -15 -29 33 0 201 | 21 22 -33 0 202 | 10 22 -29 0 203 | -10 -15 -21 0 204 | -7 22 79 0 205 | -7 22 -54 0 206 | -28 -71 85 0 207 | 7 -28 -71 0 208 | 15 -55 57 0 209 | 15 28 -55 0 210 | 15 -21 -55 0 211 | 7 28 52 0 212 | 15 -26 55 0 213 | -11 26 28 0 214 | 28 41 -52 0 215 | 9 12 -52 0 216 | 9 -41 -52 0 217 | -9 26 48 0 218 | -9 -12 48 0 219 | -41 -48 94 0 220 | -9 11 82 0 221 | -9 -33 -82 0 222 | 60 65 -94 0 223 | -48 65 -94 0 224 | 33 41 63 0 225 | -48 63 82 0 226 | 55 -63 77 0 227 | -48 -55 -63 0 228 | -63 76 -77 0 229 | -63 -76 82 0 230 | 70 -92 -94 0 231 | 37 -82 -92 0 232 | -37 -70 -92 0 233 | 24 50 92 0 234 | 30 -81 92 0 235 | 24 30 -69 0 236 | 24 -81 92 0 237 | 20 -22 92 0 238 | -20 -22 33 0 239 | -24 50 92 0 240 | 32 35 45 0 241 | 32 35 -45 0 242 | 29 43 -65 0 243 | 29 -43 96 0 244 | 32 -65 96 0 245 | 23 -35 77 0 246 | 49 -65 77 0 247 | -35 -49 77 0 248 | 32 -35 -77 0 249 | 16 -50 -65 0 250 | 12 -32 -82 0 251 | -16 20 64 0 252 | -16 -20 -32 0 253 | 14 -16 90 0 254 | 14 -16 -90 0 255 | -14 -36 51 0 256 | -36 -51 -64 0 257 | 1 36 59 0 258 | -1 -12 59 0 259 | -12 -59 -73 0 260 | -12 -28 -73 0 261 | 23 36 49 0 262 | 36 47 49 0 263 | -47 49 -59 0 264 | -6 41 49 0 265 | -6 -41 -47 0 266 | 19 36 74 0 267 | -19 36 74 0 268 | -6 -49 -59 0 269 | 25 73 91 0 270 | 25 44 -91 0 271 | 25 -85 -91 0 272 | -25 44 73 0 273 | 2 56 73 0 274 | 2 6 -56 0 275 | 2 6 -96 0 276 | -1 -2 6 0 277 | 1 -44 61 0 278 | 1 2 99 0 279 | 36 -44 99 0 280 | -2 -36 99 0 281 | 16 55 -99 0 282 | 55 89 -99 0 283 | 1 60 -99 0 284 | -44 -55 -60 0 285 | -2 -61 -95 0 286 | -15 16 -61 0 287 | -15 40 -61 0 288 | -15 -40 -61 0 289 | -62 70 85 0 290 | -62 85 -89 0 291 | 1 42 -62 0 292 | -42 -62 -85 0 293 | -61 95 99 0 294 | 62 95 -97 0 295 | 56 -89 97 0 296 | 9 46 70 0 297 | 46 -56 62 0 298 | 9 46 -70 0 299 | 28 46 62 0 300 | -9 -28 62 0 301 | -21 -46 74 0 302 | -21 -46 -74 0 303 | 73 87 -99 0 304 | 17 21 62 0 305 | 21 -62 -73 0 306 | -17 22 -73 0 307 | -17 -22 -73 0 308 | 5 21 -87 0 309 | -5 21 79 0 310 | 8 16 -52 0 311 | -1 30 -68 0 312 | -28 -89 -92 0 313 | -2 34 35 0 314 | 38 55 100 0 315 | 7 -19 -42 0 316 | 35 61 -71 0 317 | 18 -36 -93 0 318 | -36 -46 -99 0 319 | 26 38 83 0 320 | 5 52 -97 0 321 | -37 71 -75 0 322 | 18 19 48 0 323 | 11 -31 100 0 324 | -20 -31 -53 0 325 | -3 8 -100 0 326 | -7 97 -97 0 327 | -37 -49 0 328 | 5 -53 81 0 329 | 61 66 -75 0 330 | -49 -75 -92 0 331 | 46 52 -53 0 332 | 13 18 39 0 333 | 11 71 97 0 334 | -31 -46 61 0 335 | -31 80 97 0 336 | -37 53 81 0 337 | -20 -27 -71 0 338 | -58 81 93 0 339 | -1 11 -30 0 340 | 23 -46 53 0 341 | 5 33 61 0 342 | -1 33 -97 0 -------------------------------------------------------------------------------- /src/Sudoku.cpp: -------------------------------------------------------------------------------- 1 | #include "Global.h" 2 | 3 | #define CORRECT 0 4 | #define WRONG -1 5 | static int T = 0; 6 | 7 | int Digit(int a[][COL], int i, int j) {//递归填充数独元素 8 | if (i < ROW && j < COL) { 9 | int x,y,k; 10 | int check[COL+1]={CORRECT};//用于排除已经使用过的的数字 11 | for(x = 0 ; x < i ; x++) 12 | check[a[x][j]] = WRONG;//列已使用的数字置为WRONG 13 | for(x = 0 ; x < j ; x++) 14 | check[a[i][x]] = WRONG;//行使用过的数字置为WRONG 15 | for(x = i/3*3 ; x <= i; x++) { 16 | if(x == i) 17 | for(y = j/3*3 ; y < j; y++) 18 | check[a[x][y]] = WRONG; 19 | else 20 | for(y = j/3*3 ; y < j/3*3 + 3; y++) 21 | check[a[x][y]] = WRONG; 22 | } 23 | 24 | int flag = 0; 25 | for(k = 1; k <= COL && flag == 0 ; k++){//从check数组中查找安全的数字 26 | if(check[k] == CORRECT){ 27 | flag = 1; 28 | a[i][j] = k; 29 | if(j == COL-1 && i != ROW-1){ 30 | if(Digit(a,i+1,0) == CORRECT) return CORRECT; 31 | else flag = 0; 32 | } 33 | else if(j != COL-1){ 34 | if(Digit(a,i,j+1) == CORRECT) return CORRECT; 35 | else flag = 0; 36 | } 37 | } 38 | } 39 | if( flag == 0 ) { 40 | a[i][j] = 0; 41 | return WRONG; 42 | } 43 | } 44 | return CORRECT; 45 | } 46 | 47 | void randomFirstRow(int a0[], int n) {//随机生成第一行 48 | int i,j; 49 | srand((unsigned)time(nullptr)); 50 | for( i = 0 ; i < n ; i++){ 51 | a0[i] = rand()%9 + 1; 52 | j = 0 ; 53 | while(j < i){ 54 | if(a0[i] == a0[j]){ 55 | a0[i] = rand()%9 + 1; 56 | j = 0; 57 | } 58 | else j++; 59 | } 60 | } 61 | } 62 | 63 | void createSudoku(int a[][COL]){ //生成数独 64 | randomFirstRow(a[0],COL);//随机生成第一行 65 | Digit(a,1,0);//递归生成后i行 66 | } 67 | 68 | void createStartinggrid(const int a[][COL], int b[][COL], int numDigits) {//随机生成初盘 69 | int i,j,k; 70 | srand((unsigned)time(nullptr)); 71 | for( i = 0; i < ROW ; i ++) 72 | for( j = 0; j < COL ; j++) 73 | b[i][j] = a[i][j]; 74 | 75 | int c[numDigits][2]; 76 | int m,flag = 0; 77 | 78 | for( i = 0; i < numDigits ; i++) { 79 | j = rand()%9; 80 | k = rand()%9; 81 | 82 | flag = 0; 83 | for(m = 0; m < i ; m++) 84 | if( j == c[m][0] && k == c[m][1]) 85 | flag = 1; 86 | 87 | if(flag == 0){ 88 | b[j][k] = 0; 89 | c[i][0] = j; 90 | c[i][1] = k; 91 | } 92 | else 93 | i--; 94 | } 95 | } 96 | 97 | void print(const int a[][COL]){//打印数独数组 98 | int i,j; 99 | for( i = 0 ; i < ROW ; i++){ 100 | for( j = 0 ; j < COL ; j++) 101 | printf("%d ", a[i][j]); 102 | cout<right->data; 184 | SingleClause->right->data > 0 ? result[T++].value = TRUE : result[T++].value = FALSE; 185 | int temp = SingleClause->right->data; 186 | DeleteHeadNode(SingleClause,LIST);//删除单子句这一行 187 | DeleteDataNode(temp,LIST);//删除相等或相反数的节点 188 | if(!LIST) return TRUE; 189 | else if(IsEmptyClause(LIST)) return FALSE; 190 | Pfind = LIST; 191 | SingleClause = IsSingleClause(Pfind);//回到头节点继续进行检测是否有单子句 192 | } 193 | //分裂策略 194 | int Var = LIST->right->data;//选取变元 195 | HeadNode* replica = Duplication(LIST);//存放LIST的副本replica 196 | HeadNode *temp1 = ADDSingleClause(LIST,Var);//装载变元成为单子句 197 | if(SudoDPLL(temp1,result,VARNUM)) return TRUE; 198 | else { 199 | HeadNode *temp2 = ADDSingleClause(replica,-Var); 200 | return SudoDPLL(temp2,result,VARNUM); 201 | } 202 | } 203 | 204 | void SudokuShow(conse *result,int VARNUM) { 205 | int res[9][9] = {0}; 206 | for (int i = 0; i < VARNUM; ++i) { 207 | if(result[i].value == TRUE) { 208 | int x = (int)( abs(result[i].num) / 100 ) - 1; 209 | int y = (int)( (abs(result[i].num) - (x+1)*100) / 10 ) - 1; 210 | res[x][y] = abs(result[i].num) - (x+1)*100 - (y+1)*10; 211 | } 212 | } 213 | //输出result数组 214 | for (int i = 0; i < 9; ++i) { 215 | for (int j = 0; j < 9; ++j) { 216 | cout<