LeetCode: sudoku solver and valid sudoku

problem: valid sudoku

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.

The Sudoku board could be partially filled, where empty cells are filled with the character '.'.

A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.

思路;

使用bool型二维数组来记录每个数字在每个column,每个row,以及每个(3 * 3)的box是否存在。 例如二维数组cols[i][j]标志着数字 i + 1在 第 j + 1个column是否存在。

代码:

class Solution {
public:
    bool isValidSudoku(vector<vector<char> > &board) {
        bool col[9][9], row[9][9], box[9][9];
        memset(col, 0, 81 * sizeof(bool));
        memset(row, 0, 81 * sizeof(bool));
        memset(box, 0, 81 * sizeof(bool));
        for(int i = 0; i < board.size(); i++)
            for(int j = 0; j < board[i].size(); j++)
            {
                char c = board[i][j];
                if(c != '.')
                {
                    int num = c - '0' - 1;
                    int boxIndex = (i / 3) * 3 + (j / 3);
                    if(col[num][i] || row[num][j] || box[num][boxIndex])
                        return false;
                    col[num][i] = row[num][j] = box[num][boxIndex] = true;
                }
            }
        return true;
    }
};


Leetcode另一道题:

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.

思路:

主要就是使用dfs的思想来解题。另外,可以沿用上一道题目的hash的思想来提高算法的效率。在某个空格处,在试验某个数字i(1 - 9)之前,先在hash矩阵中设置true。在回溯时,将已经设置成true的hash矩阵元素设回false。

代码:

class Solution {
    bool cols[9][9] ;
    bool rows[9][9];
    bool boxes[9][9] ;
    
    bool solveSudokuAux(vector<vector<char> > &board, int pos)
    {   
        if(pos == 81)
            return true;
        int r = pos / 9;
        int c = pos % 9;
        if(board[r][c] != '.')
            return solveSudokuAux(board, pos + 1);
        else
        {
            for(int i = 1; i < 10; i++)
            {
                int boxindex = (r / 3) * 3 + (c / 3);
                if(!cols[i - 1][c] && !rows[i - 1][r] && !boxes[i - 1][boxindex])
                {
                    board[r][c] = i + '0';
                    cols[i - 1][c] = rows[i - 1][r] = boxes[i - 1][boxindex] = true;
                    if(solveSudokuAux(board, pos + 1))
                        return true;
                    board[r][c] = '.';
                    cols[i - 1][c] = rows[i - 1][r] = boxes[i - 1][boxindex] = false;
                }
            }
            return false;
        }
    }
    
public:
    void solveSudoku(vector<vector<char> > &board) {
        memset(cols, 0, 81 * sizeof(bool));
        memset(rows, 0, 81 * sizeof(bool));
        memset(boxes, 0, 81 * sizeof(bool));
        for(int i = 0; i < 9; i++)
            for(int j = 0; j < 9; j++)
            {
                if(board[i][j] != '.' )
                {
                    int val = board[i][j] - '0';
                    cols[val - 1][j] = true;
                    rows[val - 1][i] = true;
                    int boxindex = (i / 3) * 3 + (j / 3);
                    boxes[val - 1][boxindex] = true;
                }
            }
        solveSudokuAux(board, 0);
    }
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值