• [LeetCode] Sudoku Solver


    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.

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

    其实就是对于每一个'.' 尝试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 };
  • 相关阅读:
    axios全局配置
    014 Javascript(148
    013 Javascript(134
    axios的基本使用
    axios简介
    012 Javascript(126
    016 HTML+CSS(Class198
    015 HTML+CSS(Class184
    014 HTML+CSS(Class165
    LCA题目选讲2
  • 原文地址:https://www.cnblogs.com/diegodu/p/3808444.html
Copyright © 2020-2023  润新知