int main() { Board b1; b1.push_back({4, 3, 5, 2, 6, 9, 7, 8, 1}); b1.push_back({6, 8, 2, 5, 7, 1, 4, 9, 3}); b1.push_back({1, 9, 7, 8, 3, 4, 5, 6, 2}); b1.push_back({8, 2, 6, 1, 9, 5, 3, 4, 7}); b1.push_back({3, 7, 4, 6, 8, 2, 9, 1, 5}); b1.push_back({9, 5, 1, 7, 4, 3, 6, 2, 8}); b1.push_back({5, 1, 9, 3, 2, 6, 8, 7, 4}); b1.push_back({2, 4, 8, 9, 5, 7, 1, 3, 6}); b1.push_back({7, 6, 3, 4, 1, 8, 2, 5, 9}); std::cout << "b1 is " << (isValidSudoku(b1) ? "" : "not ") << "valid\n"; Board b2; b2.push_back({4, 3, 5, 2, 6, 9, 7, 8, 1}); b2.push_back({6, 8, 2, 5, 7, 1, 4, 9, 3}); b2.push_back({1, 9, 7, 8, 3, 4, 5, 6, 2}); b2.push_back({8, 2, 6, 1, 9, 5, 3, 4, 7}); b2.push_back({3, 7, 4, 6, 8, 2, 9, 1, 5}); b2.push_back({9, 5, 1, 7, 4, 3, 6, 2, 8}); b2.push_back({5, 1, 9, 3, 2, 6, 8, 7, 4}); b2.push_back({2, 4, 8, 9, 5, 7, 1, 3, 6}); b2.push_back({9, 6, 3, 4, 1, 8, 2, 5, 9}); std::cout << "b2 is " << (isValidSudoku(b2) ? "" : "not ") << "valid\n"; }
void main() { //char * board[] = { // "9.4....13", // "..39.....", // "...68.9..", // "....3.6.1", // "..2...7..", // "5.1.2....", // "..6.51...", // ".....93..", // "29....1.6" //}; char * board[] = { "..5.....6", "....14...", ".........", ".....92..", "5....2...", ".......3.", "...54....", "3.....42.", "...27.6.." }; bool result = isValidSudoku(board, 9, 9); // Note: Array is converted to pointer. if (result) printf("The Sudoku is valid.\n"); else printf("The Sudoku is invalid.\n"); }
int main(int arg, char *argv[]) { // insert code here... printf("LeetCode 036 Valid Sudoku, C ...\n\n"); char c[][10] = { "..9748...", "7........", ".2.1.9...", "..7...24.", ".64.1.59.", ".98...3..", "...8.3.2.", "........6", "...2759.." }; int boardRowSize = 9; int boardColSize = 9; char **board = (char**)malloc(sizeof(char*)*boardRowSize); for (int i = 0; i < boardRowSize; i++) { board[i] = (char*)malloc(sizeof(char) * boardColSize); memcpy(board[i], c[i], sizeof(char) * boardColSize); } printBoard(board, boardRowSize, boardColSize); printf("%s", isValidSudoku((char**)board, boardRowSize, boardColSize) ? "true" : "false"); return 0; }
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; }
bool solve(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;k++) { board[i][j] = '1'+k; if(isValidSudoku(board) && solve(board)) return true; } board[i][j] = '.'; return false; } } return true; }
int main() { char *board[9] = { "53..7....", "6..195...", ".98....6.", "8...6...3", "4..8.3..1", "7...2...6", ".6....28.", "...419..5", "....8..79" }; assert(isValidSudoku(board, 9, 9) == 1); return 0; }
// Backtracking algorithm bool sudokuSolver(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; ++ k) { board[i][j] = k + '1'; if (isValidSudoku(board, i, j) && sudokuSolver(board)) return true; board[i][j] = '.'; } return false; } } } return true; }
int main() { char board[9][9] = { '5','3','.','.','7','.','.','.','.', '6','.','.','1','9','5','.','.','.', '.','9','8','.','.','.','.','6','.', '8','.','.','.','6','.','.','.','3', '4','.','.','8','.','3','.','.','1', '7','.','.','.','2','.','.','.','6', '.','6','.','.','.','.','2','8','.', '.','.','.','4','1','9','.','.','5', '.','.','.','.','8','.','.','7','9' }; bool ans = isValidSudoku(board); if (ans) printf("YES!\n"); else printf("NO!\n"); system("pause"); return 0; }
int main(){ string b[] = { "53..7....", "6..195...", ".98....6.", "8...6...3", "4..8.3..1", "7...2...6", ".6....28.", "...419..5", "....8..79" }; vector<vector<char> > board(sizeof(b)/sizeof(b[0]),vector<char>(sizeof(b[0])/sizeof(b[0][0], '.'))); for(int i=0; i<sizeof(b)/sizeof(b[0]); i++){ for(int j=0; j<b[i].size(); j++) board[i][j] = b[i][j]; } bool res = isValidSudoku(board); printf("%s\n", res?"true":"false"); return 0; }
bool internalSolveSudoku(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<10;++k) { board[i][j] = '0' + k; if(isValidSudoku(board,i,j)) if(internalSolveSudoku(board)) return true; board[i][j] = '.'; } return false; } } return true; }
bool solve(vector<vector<char> > &board){ const int SIZE = 9; for(int i = 0; i < SIZE; i++){ for(int j = 0; j < SIZE; j++){ // found an empty cell if(board[i][j] == '.'){ // try from 1~9 for(char c = '1'; c <= '9'; c++){ board[i][j] = c; if(isValidSudoku(board, i, j) && solve(board)) return true; // rollback board[i][j] = '.'; } // all tries failed, current sudoku unsolvable return false; } } } // no more empty cell return true; }