[Leetcode] Sudoku Solver

Source: Internet
Author: User

Write a program to solve a Sudoku puzzle by filling the empty cells.

Empty cells are indicated by the character‘.‘.

You may assume that there will be only one unique solution.

A Sudoku puzzle...

 

... And its solution numbers marked in red.

 

The input of a question is valid. With the backtracking method, we only need to check whether the newly added values are valid in rows, columns, and small squares, and there is no need to check the entire matrix.

In fact, it is for every '.' try 1 ~ 9. If the value is valid, query the next '.'. If the value is invalid, try the next value.

If a '.' 1 ~ 9. If all attempts have been made and failed, there is no solution. You can return the results in advance.

 

This question is solved while checking ..

 

1 class solution {2 public: 3 bool isvalidsudoku (vector <char> & board, int X, int y) {4 int row, Col; 5 6 // same value in the same column? 7 For (ROW = 0; row <9; ++ row) {8 If (X! = Row) & (Board [row] [Y] = Board [x] [Y]) {9 return false; 10} 11} 12 13 // same value in the same row? 14 For (COL = 0; Col <9; ++ col) {15 if (y! = Col) & (Board [x] [col] = Board [x] [Y]) {16 return false; 17} 18} 19 20 // same value in the 3*3 block it belong? 21 For (ROW = (X/3) * 3; row <(X/3 + 1) * 3; ++ row) {22 for (COL = (y/3) * 3; Col <(y/3 + 1) * 3; ++ col) {23 if (X! = Row) & (y! = Col) & (Board [row] [col] = Board [x] [Y]) {24 return false; 25} 26} 27} 28 29 return true; 30} 31 32 // internal check. Can we use the bool return value to indicate 33 bool internalsolvesudokucheck (vector <char> & board) {34 for (int row = 0; row <9; ++ row) {35 for (INT Col = 0; Col <9; ++ col) {36 IF ('. '= Board [row] [col]) {37 // For each '. 'Try 1 ~ 9. If it is invalid, 38 for (INT I = 1; I <= 9; ++ I) is returned immediately) {39 Board [row] [col] = '0' + I; 40 41 if (isvalidsudoku (board, row, col) {42 if (internalsolvesudokucheck (board )) {43 return true; 44} 45} 46 // if it is invalid, reply '. ', go to the next attempt 47 Board [row] [col] = '. '; 48} 49 // for a certain '. ', try 1 ~ If none of them work, false50 return false; 51} 52} 53} 54 55 return true; 56} 57 58 void solvesudoku (vector <char> & Board) is returned) {59 internalsolvesudokucheck (Board); 60} 61 };

 

Contact Us

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.