• [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 };
  • 相关阅读:
    C#?和??运算符以及合并条件表达式
    Nhibernate Batch update returned unexpected row count from update; actual row count: 0 解决方案
    js根据ClassName来删除元素(有坑误入)
    C#正则表达式(通俗易懂)
    AngularJs自定义表单验证
    基于angularJs坐标转换指令(经纬度中的度分秒转化为小数形式 )
    网页乱码问题
    交换两个数值(值类型,引用类型)
    不用临时变量,交换二个变量的值
    PDF在线预览 (flexpaper+swftools+saveaspdfandxps)
  • 原文地址:https://www.cnblogs.com/diegodu/p/3808444.html
Copyright © 2020-2023  润新知