码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] Sudoku Solver

时间:2014-06-26 18:49:23      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   http   color   

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.

bubuko.com,布布扣

A sudoku puzzle...

 

bubuko.com,布布扣

...and its solution numbers marked in red.

 

题目的输入保证是有效的。用回溯法我们只要检查新加入的值能否在行、列以及小方块里有效即可,没有必要检查整个矩阵。

其实就是对于每一个‘.‘ 尝试1~9 9个值,如果合法,就继续查下一个‘.‘ ,如果非法,尝试下一个值。

若对于某一个‘.‘ 1~9都尝试过且都失败了,就是无解,可以提前返回。

 

此题是边检查边求解。。

 

 1 class Solution {
 2 public:
 3     bool isValidSudoku(vector<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 to?
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     // 内部check,能否找到结果用bool返回值标示
33     bool internalSolveSudokuCheck(vector<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             // 对每一个‘.‘ 尝试 1~9,如果不合法,立刻返回
38                     for (int i = 1; i <= 9; ++i) {
39                         board[row][col] = 0 + i;
40                         
41                         if (isValidSudoku(board, row, col)) {
42                             if (internalSolveSudokuCheck(board)) {
43                                 return true;
44                             }
45                         }
46                         //如果不合法,回复‘.‘,进入下次尝试
47                         board[row][col] = .;
48                     }
49                     //对于某一个‘.‘,尝试1~9都不行,就返回false
50                     return false;
51                 }
52             }
53         }
54         
55         return true;
56     }
57     
58     void solveSudoku(vector<vector<char> > &board) {
59         internalSolveSudokuCheck(board);
60     }
61 };

 

[LeetCode] Sudoku Solver,布布扣,bubuko.com

[LeetCode] Sudoku Solver

标签:style   class   blog   code   http   color   

原文地址:http://www.cnblogs.com/diegodu/p/3808444.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!