[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 sudoku solver was finally reached. With the basis of valid Sudoku, I learned that using a hash table can easily identify conflicts. The idea is to traverse the Board. This time, we found a blank location and then filtered the possible result set: we removed the numbers in rows, columns, and 3x3, the rest is the possible result set. Then, extract the elements in the result set one by one for in-depth search until the recursive exit is met: ROW = 8 & Col = 8 indicates that the Board traversal is complete and the result is obtained; if the final result returned by the function does not meet that condition, it indicates that there is no solution.

The main problem I encountered in solving this problem was the ambiguity of the recursive exit judgment, and I always liked to upload a reference when I passed the board, which caused a deep search to change the board .. This is a bug, which was also encountered in Word breakii. The overall understanding of this question still needs to be further elaborated.

 1 void solver(vector<vector<char>> board, int row, int col, vector<vector<char>> &ret) { 2     if (board[row][col] != ‘.‘) { 3         if (row == 8 && col == 8) { 4             ret = board; 5             return; 6         } 7         else if (++col >= 9) { 8             ++row; 9             col = 0;10         }11         solver(board, row, col, ret);12         return;13     }14     15     // calculate solve space16     unordered_set<char> set = preset;17     // filter colum18     for (int k = 0; k < 9; k++) {19         char searched = board[k][col];20         if (searched != ‘.‘ && set.find(searched) != set.end()) {21             set.erase(searched);22         }23     }24     // filter row25     for (int k = 0; k < 9; k++) {26         char searched = board[row][k];27         if (searched != ‘.‘ && set.find(searched) != set.end()) {28             set.erase(searched);29         }30     }31     // filter 3*332     int square_row_offset = row / 3;33     int square_col_offset = col / 3;34     for (int k = square_row_offset * 3; k < square_row_offset * 3 + 3; k++) {35         for (int l = square_col_offset * 3; l < square_col_offset * 3 + 3; l++) {36             char searched = board[k][l];37             if (searched != ‘.‘ && set.find(searched) != set.end()) {38                 set.erase(searched);39             }40         }41     }42     43     for (char ele: set) {44         board[row][col] = ele;45         46         if (row == 8 && col == 8) {47             ret = board;48             return;49         }50         51         solver(board, row, col+1, ret);52     }53 }54 55 56 vector<vector<char>> sudokuSolver(vector<vector<char>> &board) {57     vector<vector<char>> ret;58     solver(board, 0, 0, ret);59     return ret;60 }

 

[Leetcode] Sudoku Solver

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.