// Move to next cell when its filled already void nextCell(int row, int column) { if(column < (boardSize - 1)) { solveSudoku(row, (column + 1)); }else { solveSudoku((row + 1), 0); } }
bool solveSudoku(vector<vector<char>>& board, unordered_set<char> s, int row, int col) { if (board[row][col] == '.') { for (int i = 1; i <= 9; i++) { board[row][col] = char(i + '0'); if (checkRow(board, s, row) && checkCol(board, s, col) && checkSubbox(board, s, row/3, col/3)) { if (row == board.size() - 1 && col == board[row].size() - 1) { return true; } else if (col == board[row].size() - 1) { if (solveSudoku(board, s, row + 1, 0)) { return true; } } else { if (solveSudoku(board, s, row, col + 1)) { return true; } } } } board[row][col] = '.'; } else { if (row == board.size() - 1 && col == board[row].size() - 1) { return true; } else if (col == board[row].size() - 1) { return solveSudoku(board, s, row + 1, 0); } else { return solveSudoku(board, s, row, col + 1); } } return false; }
bool solveSudoku(int * p) { //print2DArray(p); //std::cout << "\n"; int col, row; int offset = findStartingPosition(p); if (offset == -1) return true; row = offset / 9; col = offset % 9; // Possible Numbers are 1 through 9 for (int i = 1; i < 10; i++) { if (isPositionValid(p, i, col, row)) { // Try Possible Value *(p + offset) = i; // Recursion if (solveSudoku(p)) { return true; } // Backtracking if returned false *(p + offset) = UNINITIALIZED; } } return false; }
//backtrace bool solveSudoku(vector<vector<char> > &board, int row, int col) { if (board.size()==row) return true; if (board[row][col]!='.') { if (col==board.size()-1) return solveSudoku(board, row+1, 0); else return solveSudoku(board, row, col+1); } for (int i=1; i<=9; i++) { if (check(board, row, col, '0'+i)) { board[row][col] = '0'+i; if (col==board.size()-1 && solveSudoku(board, row+1, 0)) return true; else if (solveSudoku(board, row, col+1)) return true; board[row][col] = '.'; } } return false; }
int main() { char **array; array = malloc(9 * sizeof(char *)); for (int i = 0; i < 9; i++) { array[i] = malloc(9 * sizeof(char)); if (array[i] == NULL) { fprintf(stderr, "out of memory\n"); exit(EXIT_FAILURE); } } for (int i = 0; i < 9; ++i) for (int j = 0; j < 9; ++j) array[i][j] = input[i][j]; solveSudoku(array, 9, 9); printOut(array); for (int i = 0; i < 9; ++i) free(array[i]); free(array); return 0; }
void solveSudoku(vector<vector<char>> & board) { vector<int> vecFillRowIndex; vector<int> vecFillColIndex; vector<vector<bool>> vecExistRow(SUDOKU_SIZE, vector<bool>(SUDOKU_SIZE + 1, false)); vector<vector<bool>> vecExistCol(SUDOKU_SIZE, vector<bool>(SUDOKU_SIZE + 1, false)); vector<vector<bool>> vecExistCub(SUDOKU_SIZE, vector<bool>(SUDOKU_SIZE + 1, false)); for (int iIndex = 0; iIndex < SUDOKU_SIZE; iIndex ++) { for (int jIndex = 0; jIndex < SUDOKU_SIZE; jIndex ++) { if (board[iIndex][jIndex] != '.') { int iCurrValue = (int)(board[iIndex][jIndex] - '0'); vecExistRow[iIndex][iCurrValue] = true; vecExistCol[jIndex][iCurrValue] = true; vecExistCub[cubIndex(iIndex, jIndex)][iCurrValue] = true; } else { vecFillRowIndex.push_back(iIndex); vecFillColIndex.push_back(jIndex); } } } solveSudoku(board, vecFillRowIndex, vecFillColIndex, 0, vecExistRow, vecExistCol, vecExistCub); }
int findEmpty(int i,int j) { // printf("Hi1\n"); if(*(*(ptr+i)+j)==0) { put[count].xCordinate= i; put[count].yCordinate = j; //count+=1; solveSudoku(i,j,1); } if(i>=8 && j>=8) { printf("Sudoku successful \n"); return 1; } else if(j!=8) { findEmpty(i,j+1); } else { findEmpty(i+1,0); } return 1; }
void SudokuSolver::solveSudoku(vector<vector<char>>& board) { if (board.empty() || board.size() != 9 || board[0].size() != 9) return; solveSudoku(board, 0, 0); }
int main() { // Game Board int myBoard[N][N]; // Game Board Pointer int* myBoardPointer = (int*)myBoard; // Initialize Array with Default Values initialize2DArray(myBoardPointer); randomizeBoard(myBoardPointer); std::cout << "Original Board:\n"; print2DArray(myBoardPointer); if (solveSudoku(myBoardPointer)) { std::cout << "Solved: \n"; print2DArray(myBoardPointer); } else { std::cout << "No Solution Exists: \n"; } return 1; }
void start() { char name[20]; char size[1]; int sizes; printf("Sudoku Board Sizes:\na. 1\nb. 4\nc. 9\nd. 16\ne. 25\n\nEnter letter: "); scanf("%s", size); if (AreEqual(size, "a")) { sizes = 1; }else if (AreEqual(size, "b")) { sizes = 4; }else if (AreEqual(size, "c")) { sizes = 9; }else if (AreEqual(size, "d")) { sizes = 16; }else if (AreEqual(size, "e")) { sizes = 25; } boardSize = sizes; int grid[25][25]; printf("\nEnter File Name: "); scanf("%s", name); FILE* fp = fopen(name, "r"); subBoard = (sqrt(boardSize)); if (fp == NULL) { // char save[20]; // printf("Save Results to:\n"); // scanf("%s", save); // FILE* ofp = fopen(save, "w"); //fprintf(ofp, "Error: No Such File or Directory!\n"); printf("\nError: No Such File or Directory!\n\n"); system("pause"); //fclose(ofp); exit(1); } else /*if (elementCount(fp) == 81)*/ { for (int i = 0; i < boardSize; i++) { for (int j = 0; j < boardSize; j++) { fscanf(fp, "%d", &grid[i][j]); } } memcpy(table, grid, sizeof(table)); solveSudoku(0, 0); // The solving starts by passing parameters exit(1); } /*else { char save[20]; printf("Save Results to:\n"); scanf("%s", save); FILE* ofp = fopen(save, "w"); fprintf(ofp, "Error: Invalid Sudoku Inputs!\n"); fclose(ofp); exit(1); }*/ fclose(fp); exit(1); }
bool SudokuSolver::solveSudoku(vector<vector<char>>& board, int m, int n) { if (m == 9) return true; if (n == 9) return solveSudoku(board, m + 1, 0); if (board[m][n] == '.') { for (int i = 1; i <= 9; i++) { board[m][n] = i + '0'; if (isValid(board, m, n) && solveSudoku(board, m, n + 1)) return true; else board[m][n] = '.'; } } else return solveSudoku(board, m, n + 1); return false; }
bool solveSudoku(vector<vector<char> > &board) { // Start typing your C/C++ solution below // DO NOT write int main() function int dot = findFirstDot(board); if(dot == -1) return true; for(int i=1;i<=9;i++) { board[dot/9][dot%9]=i+'0'; if(isValidSudoku(board,dot) && solveSudoku(board)) return true; board[dot/9][dot%9]='.'; } return false; }
int solveSudoku(int i, int j, int no) { int a=i, b=j; //if(((searchRow(0,j,no,a)==1) || (searchCol(i,0,no,b)==1) || (searchMat(i/3,j/3,a,b,no)==1)) || (no>9)) printf("count= %d ,(%d,%d) = %d\n",count,i,j,no); if(no>9) { printf("count =%d , (%d,%d) = %d\n",count,i,j,no); printf("No Greter than 9 hence backtrack\n"); *(*(ptr+put[count].xCordinate)+put[count].yCordinate) = 0; count -= 1; printf("count =%d, (%d,%d) = %d\n",count,i,j,no); //*(*(ptr+put[count].xCordinate)+put[count].yCordinate) = 0; solveSudoku(put[count].xCordinate,put[count].yCordinate,(put[count].valuePut)+1); //count += 1; // return 1; findEmpty (put[count].xCordinate,put[count].yCordinate); return 1; } /* if(no>9) return 0;*/ //a=i; //b=j; if((searchRow(0,j,no,a)==0) && (searchCol(i,0,no,b)==0) && (searchMat(i/3,j/3,a,b,no)==0)) { *(*(ptr+i)+j)= no; put[count].valuePut = no; count+=1; return 1; } else { solveSudoku(i,j,no+1); return 1; } return 1; }
bool solveSudoku(vector<vector<char> > &board){ for (int i = 0; i < 9; ++i) for (int j = 0; j < 9; ++j){ if ('.' == board[i][j]){ for (int k = 1; k <= 9; ++k){ board[i][j] = '0' + k; if (isValid(board, i, j) && solveSudoku(board)) return true; board[i][j] = '.'; } return false; } } return true; }
// 检查从当前局面开始是否能够得到最终合法有效的解 bool solveSudoku(vector<vector<char> >& board) { // 如果没有找到空白的格子,说明已经填满了,成功返回 pair<int, int> pos = findFirstEmpty(board); if (pos.first == -1 && pos.second == -1) return true; // 否则依次尝试往当前格子中填入数字 1-9,并判断能否得到可行的解 for (int i = 1; i <= 9; ++i) { board[pos.first][pos.second] = i + '0'; if (isValidBoard(board, pos) && solveSudoku(board)) return true; // 恢复原样 board[pos.first][pos.second] = '.'; } return false; }
int main(int argc, char *argv[]) { char s1[] = "..9748..."; char s2[] = "7........"; char s3[] = ".2.1.9..."; char s4[] = "..7...24."; char s5[] = ".64.1.59."; char s6[] = ".98...3.."; char s7[] = "...8.3.2."; char s8[] = "........6"; char s9[] = "...2759.."; char* board[] = {s1, s2, s3, s4, s5, s6, s7,s8, s9}; solveSudoku(board, 9, 9); return 0; }
bool solveSudoku(vector<vector<char> > &board) { for (int i = 0; i < 9; i++){ for (int j = 0; j < 9; j++){ if (board[i][j] == '.'){ for (int k = 0; k < 9; j++){ board[i][j] = '1' + k; if (isValid(board, i, j) && solveSudoku(board)) return true; board[i][j] = '.'; } return false; } } } return true; }
bool solveSudoku(vector<vector<char> > &board) { for(int i=0; i<9; i++){ for(int j=0; j<9; j++){ if(board[i][j] == '.'){ for(int k=1; k<=9; k++){ board[i][j] = '0' + k; if(isValid(board, i, j) && solveSudoku(board)){ return true; } board[i][j] = '.'; //一定要记得还原现场 } return false; } } } return true; }
int main(){ int sudoku[9][9] = { {3, 0, 6, 5, 0, 8, 4, 0, 0}, {5, 2, 0, 0, 0, 0, 0, 0, 0}, {0, 8, 7, 0, 0, 0, 0, 3, 1}, {0, 0, 3, 0, 1, 0, 0, 8, 0}, {9, 0, 0, 8, 6, 3, 0, 0, 5}, {0, 5, 0, 0, 9, 0, 6, 0, 0}, {1, 3, 0, 0, 0, 0, 2, 5, 0}, {0, 0, 0, 0, 0, 0, 0, 7, 4}, {0, 0, 5, 2, 0, 6, 3, 0, 0}}; if (solveSudoku(sudoku) == 1) printSudoku(sudoku); else printf("No solution exists"); return 0; }
int main(int argc, const char *argv[]) { char *board[] = { "..9748...", "7........", ".2.1.9...", "..7...24.", ".64.1.59.", ".98...3..", "...8.3.2.", "........6", "...2759..", }; solveSudoku(board, 9,9); printf("no\n"); //return 0; }
bool solveSudoku(vector<vector<char> > &board, int start){ for (int r = start; r != 9; ++r){ for (int c = 0; c != 9; ++c){ if (board[r][c] == '.'){ for (int k = 1; k != 10; ++k){ board[r][c] = k + '0'; if (isvalid(board, r, c) && solveSudoku(board, r)){ return true; } board[r][c] = '.'; } return false; } } } return true; }
bool solveSudoku(int grid[N][N]) { int row,col; if(!findUnassinged(grid,row,col)) return true; for(int num=1;num<=9;++num) { if(isSafe(grid,row,col,num)) { grid[row][col]=num; if(solveSudoku(grid)) return true; grid[row][col]=0; } } return false; }
bool solveSudoku(vector<vector<char>> & vecBoard, vector<int> & vecFillRowIndex, vector<int> & vecFillColIndex, int iCurrentFill, vector<vector<bool>> & vecExistRow, vector<vector<bool>> & vecExistCol, vector<vector<bool>> & vecExistCub) { if (iCurrentFill >= vecFillRowIndex.size()) { return true; } int iCurrentFillRowIndex = vecFillRowIndex[iCurrentFill]; int iCurrentFillColIndex = vecFillColIndex[iCurrentFill]; int iCurrentFillCubIndex = cubIndex(iCurrentFillRowIndex, iCurrentFillColIndex); for (char cFill = '1'; cFill <= '9'; cFill ++) { int iValueExistIndex = (int)(cFill - '0'); if (!vecExistRow[iCurrentFillRowIndex][iValueExistIndex] && !vecExistCol[iCurrentFillColIndex][iValueExistIndex] && !vecExistCub[iCurrentFillCubIndex][iValueExistIndex]) { vecBoard[iCurrentFillRowIndex][iCurrentFillColIndex] = cFill; vecExistRow[iCurrentFillRowIndex][iValueExistIndex] = true; vecExistCol[iCurrentFillColIndex][iValueExistIndex] = true; vecExistCub[iCurrentFillCubIndex][iValueExistIndex] = true; if (solveSudoku(vecBoard, vecFillRowIndex, vecFillColIndex, iCurrentFill + 1, vecExistRow, vecExistCol, vecExistCub)) { return true; } vecExistCub[iCurrentFillCubIndex][iValueExistIndex] = false; vecExistCol[iCurrentFillColIndex][iValueExistIndex] = false; vecExistRow[iCurrentFillRowIndex][iValueExistIndex] = false; vecBoard[iCurrentFillRowIndex][iCurrentFillColIndex] = '.'; } } return false; }
int main(int argc, char *argv[]){ if (argc<3){ printf("Usage: ./sudoku <thread_count> <inputFile>\n"); exit(0); } int **originalGrid = readInput(argv[2]); int **gridToSolve = readInput(argv[2]); int thread_count = atoi(argv[1]); if (thread_count<=0){ printf("Usage: Thread Count should be positive\n"); } omp_set_num_threads(thread_count); int i,j; printf("************************INPUT GRID***********************\n"); for (i=0;i<SIZE;i++){ for (j=0;j<SIZE;j++){ printf("%d ",originalGrid[i][j]); } printf("\n"); } printf("*********************************************************\n"); double start = omp_get_wtime(); int **outputGrid = solveSudoku(originalGrid); double finish = omp_get_wtime(); printf("************************OUTPUT GRID***********************\n"); for (i=0;i<SIZE;i++){ for (j=0;j<SIZE;j++) printf("%d ",outputGrid[i][j]); printf("\n"); } printf("*********************************************************\n"); if (isValid(originalGrid,outputGrid)){ printf("SOLUTION FOUND\nTIME = %lf\n",(finish-start)); } else{ printf("NO SOLUTION FOUND\nTIME =%lf\n",(finish-start)); } }
int solveSudoku(int sudoku[MAX_BOUNDS][MAX_BOUNDS]){ int row, col,num; if (!FindUnassignedLocation(sudoku,&row,&col)) return 1; for ( num = 1; num <= 9; num++) { // if looks promising if (isSafe(sudoku, row, col, num)) { // make tentative assignment sudoku[row][col] = num; // return, if success, yay! if (solveSudoku(sudoku)) return 1; // failure, unmake & try again sudoku[row][col] = UNASSIGNED; } } return 0; // this triggers backtracking }
int main(int argc, char **argv){ int **board = readBoard(); printSudoku(board); printSudoku(solveSudoku(board, newSq(0,0))); }
int main(int argc, char** args) { solveSudoku(b, 9, 9); for (int i = 0; i < 9; i++) printf("%s\n", b[i]); return 0; }
void solveSudoku(vector<vector<char> > &board) { solveSudoku(board, 0); return; }
void solveSudoku(vector<vector<char>>& board) { unordered_set<char> s; solveSudoku(board, s, 0, 0); }