Topic Connection
https://leetcode.com/problems/sudoku-solver/
Sudoku solverdescription
Write a program to solve a Sudoku puzzle by filling the empty cells.
Empty cells is indicated by the character ‘.‘
.
Assume that there would be is only one unique solution.
A Sudoku Puzzle ...
... and its solution numbers marked in red.
DFS back-up:
class solution {public:static const int N = 9;int cnt;struct Node { int x, y; Node () {}node (int i, int j): X (i), Y (j) {}}st[n * n];void Solvesudoku (vector<vector<char>>& board) {cnt = 0;for (int i = 0; i < n; i++) {for (int j = 0; J < N; J + +) {if (board[i][j] = = '. ') {st[cnt++] = Node (i, j);}}} DFS (board, 0);} BOOL Dfs (vector<vector<char>>& board, int t) {if (t = = cnt) return true;for (int i = 1; I <= N; i++) {i NT x = st[t].x, y = st[t].y;if (isOk (board, X, y, i + ' 0 ')) {Board[x][y] = i + ' 0 '; if (DFS (board, T + 1)) return True;boar D[x][y] = '. ';}} return false;} BOOL IsOk (vector<vector<char>>& board, int x, int y, char ch) {for (int i = 0; i < N; i++) {if (boa Rd[x][i] = = CH | | Board[i][y] = = ch) return false;} for (int i = X/3 * 3, I <= X/3 * 3 + 2; i++) {for (int j = Y/3 * 3; J <= Y/3 * 3 + 2; j + +) {if (Board[i][j] = = ch) return false;}} return true;}};
Leetcode Sudoku Solver