[Leetcode] Sudoku Solver

Source: Internet
Author: User

Sudoku Solver

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.

Problem Solving Ideas:

I didn't play Sudoku before, so I went to the internet for a bit of evil. So I fell into a misunderstanding, sudoku several skills to use the program fully realized. So, tangled for a long time. It is very easy to use a programmer's mind, that is, backtracking. I realized that only a number of methods and ranks of the exclusion method, if not continue to solve, then use the law of violence. The code is as follows.

Class Solution {Public:void Solvesudoku (vector<vector<char> > &board) {set<int> allCan ({1, 2, 3, 4 , 5, 6, 7, 8, 9}); Map<int, set<int>> candidates;map<int, set<int>> rowleft;//What's left of a row map< int, set<int>> colleft;//which number is left in a column for (int i = 0; i < 9; i++) {rowleft[i] = allcan;colleft[i] = Allcan;} for (int i = 0; i<9; i++) {for (int j = 0; j<9; j + +) {if (board[i][j] = = '. ') {CANDIDATES[GET1D (I, j)] = allcan;prunning (board, I, J, candidates);} Else{rowleft[i].erase (board[i][j]-' 0 '); colleft[j].erase (Board[i][j]-' 0 ');}}} while (Candidates.size () >0) {int unknownumber = Candidates.size (); Showboard (board);//Only the remainder of the method {for (Map<int, set <int>>::iterator it = Candidates.begin (); It! = Candidates.end ();) {if (it->second.size () = = 1) {int i = Get2di (it->first); int j = GET2DJ (It->first); board[i ][J] = * (It->second.begin ()) + ' 0 '; Rowleft[i].erase (board[i][j]-' 0 '); colleft[j].erase (Board[i][j]-' 0 '); Candidates.erase (it++);}else{it++;}} for (int i = 0; i<9; i++) {for (int j = 0; j<9; j + +) {if (board[i][j] = = '. ') {prunning (board, I, J, candidates);}}} Line Exclusion method {for (int i = 0; i < 9; i++) {for (Set<int>::iterator it = Rowleft[i].begin (); It! = Rowleft[i].end ();) {int pos = -1;bool Onlyone = true;for (int j = 0; J < 9; J + +) {int index = GET1D (i, j); if (Candidates.find (index)! = Candidat Es.end () && candidates[index].find (*it)! = Candidates[index].end ()) {if (pos >= 0) {onlyone = False;break;} pos = j;}} if (onlyone&&pos >= 0) {Board[i][pos] = *it + ' 0 '; colleft[pos].erase (*it); Rowleft[i].erase (it++); Candidates.erase (GET1D (i, POS));} else{it++;}}} for (int i = 0, i < 9; i++) {for (int j = 0; J < 9; J + +) {if (board[i][j] = = '. ') {prunning (board, I, J, candidates);}}} Column Exclusion method {for (int j = 0; J < 9; J + +) {for (Set<int>::iterator it = Colleft[j].begin (); It! = Colleft[j].end ();) {int pos = -1;bool Onlyone = true;for (int i = 0; i < 9; i++) {int index = GET1D (i, j); if (candidates. Find (Index)! = Candidates.end () && candidates[index].find (*it)! = Candidates[index].end ()) {if (pos >= 0) { Onlyone = False;break;} pos = i;}} if (onlyone&&pos >= 0) {Board[pos][j] = *it + ' 0 '; rowleft[pos].erase (*it); Colleft[j].erase (it++); Candidates.erase (GET1D (POS, J));} else{it++;}}} for (int i = 0; i<9; i++) {for (int j = 0; j<9; j + +) {if (board[i][j] = = '. ') {prunning (board, I, J, candidates);}}} if (Unknownumber = = Candidates.size ()) {break;}} If still not answered, then violent enumeration if (Candidates.size () > 0) {dfscheck (board, candidates, Candidates.begin ());} Showboard (board);} BOOL Dfscheck (vector<vector<char> > &board, Map<int, set<int>> &candidates, map< int, set<int>>::iterator it) {if (it = = Candidates.end ()) {return true;} int i = Get2di (it->first); int j = GET2DJ (It->first); for (Set<int>::iterator Setit = It->second.begin (); SE Tit! = It->second.end (); setit++) {Board[i][j] = *setit+ ' 0 '; it++;if (Isvalidsudoku (board) &&Dfscheck (Board, candidates, it)) {return true;} IT--;BOARD[I][J] = '. ';}} Private:int get1d (int i, int j) {return I * 9 + j;} int get2di (int index) {return INDEX/9;} int get2dj (int index) {return index% 9;} void Showboard (Vector<vector<char> > &board) {/*for (int i = 0; i < 9; i++) {for (int j = 0; J < 9; j+ +) {cout << board[i][j];if ((j + 1)% 3 = = 0) {cout << "";}} cout << Endl;} cout << endl;*/};void prunning (vector<vector<char> > &board, int i, Int J, Map<int, SET&LT;INT&G T;> &candidates) {pruninggrid (board, I, J, candidates);p Runingrow (board, I, J, candidates);p Runingcolumn (board, I, J, candidates);} void Pruninggrid (Vector<vector<char> > &board, int i, Int J, Map<int, Set<int>> & Candidates) {int index = GET1D (i, j); while (i% 3! = 0) {i--;} while (j% 3! = 0) {j--;} for (int m = i, M < i + 3; m++) {for (int n = j; n < j + 3; n++) {if (board[m][n]! = '. ') {candidates[index].erase (Board[m][n]- ' 0 ');}}} void Pruningrow (Vector<vector<char> > &board, int i, Int J, Map<int, Set<int>> & Candidates) {int index = GET1D (i, j); for (int m = 0; m < 9; m++) {if (board[i][m]! = '. ') {candidates[index].erase (board[i][m]-' 0 ');}}} void Pruningcolumn (Vector<vector<char> > &board, int i, Int J, Map<int, Set<int>> & Candidates) {int index = GET1D (i, j); for (int m = 0; m < 9; m++) {if (board[m][j]! = '. ') {candidates[index].erase (board[m][j]-' 0 ');}}} BOOL Isvalidsudoku (vector<vector<char>>& board) {//verifies that each row is valid for (int i = 0; i<9; i++) {if (! Checkrowvalid (Board, I)) {return false;}} Verify that each column is valid for (int i = 0; i<9; i++) {if (!checkcolumnvalid (board, I)) {return false;}} Verify that each of the cells is valid for (int i = 0; i<9; i = i + 3) {for (int j = 0; j<9; j = j + 3) {if (!checkgridvalid (board, I, J)) {return false;}}} return true;} Verifies that each lattice is valid, passing in the upper-left corner of the subscript bool Checkgridvalid (vector<vector<char>>& board, int m, int n) {bool FLag[9];memset (flag, 0, sizeof (BOOL) * 9), for (int i = m; i<m + 3; i++) {for (int j = n; j<n + 3; j + +) {if (Board[i][j] == '.') {continue;} if (Flag[board[i][j]-' 1 ']) {return false;} FLAG[BOARD[I][J]-' 1 '] = true;}} return true;} Verify that each row is valid, passing in the line number bool Checkrowvalid (vector<vector<char>>& board, int m) {bool Flag[9];memset (flag, 0 , sizeof (BOOL) * 9), for (int i = 0; i<9; i++) {if (board[m][i] = = '. ') {continue;} if (Flag[board[m][i]-' 1 ']) {return false;} Flag[board[m][i]-' 1 '] = true;} return true;} Verify that each column is valid, passing in the column number bool Checkcolumnvalid (vector<vector<char>>& board, int n) {bool Flag[9];memset ( Flag, 0, sizeof (BOOL) * 9); for (int i = 0; i<9; i++) {if (board[i][n] = = '. ') {continue;} if (Flag[board[i][n]-' 1 ']) {return false;} Flag[board[i][n]-' 1 '] = true;} return true;}};


[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.