# Leetcode Sudoku Solver

Source: Internet
Author: User

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

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.