// Initialized Constructor: Accepts a vector of length 81 Sudoku::Sudoku( vector<int> initial ) { // Default constructor to initialize everything to zero *this = Sudoku(); // Invalid length => Default Constructor if( initial.size() != 81 ) { return; } for( int r = 0; r < 9; r++ ) { for( int c = 0; c < 9; c++ ) { cell[r][c] = initial[ r*9 + c ]; if( cell[r][c] == 0 ) { possible[r][c][0] = false; } else { possible[r][c][0] = true; } } } genInitPosVal(); }
// Initialized Constructor: Accepts a string of length 81 of the values of the // board by row via left-right, top-down. Sudoku::Sudoku( string initial ) { // Default constructor to initialize everything to zero *this = Sudoku(); // Invalid length => Default Constructor if( initial.length() != 81 ) { return; } // cpp bs for converting strings to ints const char *t = initial.c_str(); char temp; for( int r = 0; r < 9; r++ ) { for( int c = 0; c < 9; c++ ) { temp = t[r*9+c]; cell[r][c] = atoi(&temp); if( cell[r][c] == 0 ) { possible[r][c][0] = false; } else { possible[r][c][0] = true; } } } genInitPosVal(); }
void Solver::guess(int index) { //Where slot=0-80 Sudoku s = Sudoku(); s.setField(_field); s.print(); usleep(300); bool succes = false; for (int option : missing[index]) { if (_field.get(index) >= option) { // We have tried this slot before continue; } if (_field.fit(index, option)) { _field.set(index, option); succes = true; break; } } if (succes) { if (_field.countEmpty(true) > 0) { // Find the the next empty slot in the vector int curIndex = std::distance(emptySlots.begin(), std::find(emptySlots.begin(), emptySlots.end(), index)); guess(emptySlots.at(curIndex + 1)); } else { return; } } else { _field.set(index, 0); // Find the the previous tried slot in the vector int curIndex = std::distance(emptySlots.begin(), std::find(emptySlots.begin(), emptySlots.end(), index)); guess(emptySlots.at(curIndex - 1)); } }
void printProgress(int &count, Field &_field) { ++count; if (count % 100000 == 0) { count = 0; Sudoku s = Sudoku(); s.setField(_field); s.print(); //usleep(300); } }
//数独求解函数 void Sudoku(int a[N][N], int n){ int temp[N][N]; int i,j; for (i=0; i<N; i++) { for (j=0; j<N; j++) temp[i][j] = a[i][j]; //将原始数据赋值给临时数组temp } i = n/N; j = n%N; //求出第 n 个数的行数和列数 if (a[i][j] != 0) { //已经有原始数据 if (n == N*N-1) Print(temp); //数组最后一位,输出 else Sudoku(temp, n+1); //不是数组最后一位,求下一个数 } else { //a[i][j]=0, 即没有原始数据 //以下 for 循环为核心语句,内嵌k递增与Sudoku递归 for (int k=1; k<=N; k++) { int flag = Check(temp, i, j, k); if (flag) { //第 i 行, 第 j 列可以是 k temp[i][j] = k; //设为 k if (n == N*N-1) Print(temp); else Sudoku(temp, n+1); temp[i][j] = 0; //恢复为0,判断下一个k } } } }
int main() { //初始化 int arr[N][N] = {6,0,0, 0,8,7, 0,0,0, 0,0,0, 9,0,5, 7,0,6, 0,4,0, 0,0,0, 0,8,0, 0,3,0, 0,0,2, 0,0,0, 0,0,4, 0,0,0, 6,9,0, 0,0,0, 4,1,0, 0,2,3, 5,0,0, 0,3,0, 1,7,0, 0,8,0, 0,9,0, 2,0,0, 0,0,1, 0,7,6, 3,0,0,}; Print(arr); Sudoku(arr, 0); return 0; }
Sudoku Puzzles:: nextPuzzle() { string line; if(ifile >> line) return Sudoku(line); else return Sudoku(line, false);
void test_Sudoku() { Sudoku Game=Sudoku(); Game.print_data(); print_vector_int(Game.get_col(0)); }
void Sudoku::SetGrid(BoardGrid grid) { (*this) = Sudoku(grid); }
void Solver::solve(Sudoku& sudoku) { this->_field = sudoku.getField(); if (_field.countEmpty() == 0) { cout << "RETURN SOLVE BECAUSE NO EMPTY SLOTS 1" << endl; return; } // Look for slots with only one option, fill it in and repeat bool hit; do { hit = false; // Find all possible options for the empty slots missing = findMissing(); // Find all slots with only one option for (int index = 0; index < _field.getSizeSq(); ++index) { if (missing[index].size() == 1) { _field.set(index, missing[index].at(0)); missing[index].clear(); cout << "MADE A HIT AT " << index << " WITH " << _field.get(index) << endl; hit = true; } } } while (hit == true); // We completed the puzzle, thus return if (_field.countEmpty(true) == 0) { cout << "RETURN SOLVE BECAUSE NO EMPTY SLOTS 2" << endl; sudoku.setField(_field); return; } // No more sure slots, so start guessing std::cout << "COMPLETED ALL SURE HITS, START GUESSING" << std::endl; // Print the result so far std::cout << "RESULT SO FAR: " << std::endl; Sudoku s = Sudoku(); s.setField(_field); s.print(); // Find all possible options for the remaining empty slots missing = findMissing(); // Print all options for (int index = 0; index < _field.getSizeSq(); ++index) { cout << "[" << index << "]: "; for (int index2 = 0; index2 < missing[index].size(); ++index2) { cout << missing[index].at(index2) << " "; } cout << "\n"; } emptySlots.clear(); // Fill in the empty slots to the array for(map<int,vector<int>>::iterator it = missing.begin(); it != missing.end(); ++it) { if ((it->second).size() > 0) emptySlots.push_back(it->first); } // Sort the emptyslot indices by size small->big std::sort(emptySlots.begin(), emptySlots.end(), lessThan); guess(emptySlots.at(0)); sudoku.setField(_field); }