Leetcode Sudoku Solver

Source: Internet
Author: User

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

Test instructions: Sudoku game

Thinking: Backtracking, is not a long time no write, open tag array is always wrong

Class Solution {Public:bool check (vector<vector<char> > &board, int position) {int x = Position/9;int y  = position% 9;for (int i = 0; i < 9; i++) if (i! = x && board[i][y] = = Board[x][y]) return false;  for (int j = 0; J < 9; J + +) if (j! = y && board[x][j] = = Board[x][y]) return false; for (int i = X/3 * 3, I < (X/3 + 1) * 3; i++) for (int j = Y/3 * 3; J < (Y/3 + 1) * 3; j + +) if (i! = x &am  p;& J! = y && board[i][j] = = Board[x][y]) return false;  return true;  }bool Solve (vector<vector<char> > &board, int position) {if (position = = Bayi) return true;int row = position /9;int col = position% 9;if (board[row][col] = = '. ') {for (int i = 1; I <= 9; i++) {Board[row][col] = i + ' 0 '; if (check (board, position) && solve (board, position+1) ) return True;board[row][col] = '. ';}} else {if (Solve (board, position + 1)) return true;} return false;} void Solvesudoku (Vector<vector<char> > &boarD) {Solve (board, 0);}}; 



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.