LeetCode 37 - Sudoku Solver

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.

My Iterative BP Code

class Solution {
public:
    bool checkCell(vector<vector<char> >& board, int row, int col, char c)
    {
        for (int i = 0; i < 9; i++)
        {
            if (board[row][i] == c)
                return false;
            if (board[i][col] == c)
                return false;
            if (board[row-row%3+i/3][col-col%3+i%3] == c)
                return false;
        }

        return true;
    }

    void solveSudoku(vector<vector<char> >& board) {
        vector<int> indices;
        for (int i = 0; i < 9; i++)
            for (int j = 0; j < 9; j++)
                if (board[i][j] == '.')
                    indices.push_back(9*i+j);

        int size = indices.size();
        int i = 0;
        while (i < size)
        {
            int idx = indices[i];
            int row = idx / 9;
            int col = idx % 9;
            char c;
            if (board[row][col] == '.')
                c = '1';
            else
                c = board[row][col] + 1;
            for (; c < '9' + 1; c++)
            {
                // Check
                if (checkCell(board, row, col, c) == false)
                    continue;

                board[row][col] = c;
                break;
            }

            if (c < '9' + 1)
            {
                i++;
            }
            else // Backtrack
            {
                board[row][col] = '.';
                i--;
            }
        }
    }
};
Runtime: 48 ms

My Recursive BP Code

class Solution {
public:
    bool checkCell(vector<vector<char> >& board, int row, int col, char c)
    {
        for (int i = 0; i < 9; i++)
        {
            if (board[row][i] == c)
                return false;
            if (board[i][col] == c)
                return false;
            if (board[row-row%3+i/3][col-col%3+i%3] == c)
                return false;
        }

        return true;
    }

    bool doSolveSudoku(vector<vector<char> >& board, int row, int col)
    {
        if (row == 9)
            return true;

        if (col == 9)
            return doSolveSudoku(board, row + 1, 0);

        if (board[row][col] != '.')
            return doSolveSudoku(board, row, col + 1);

        for (char c = '1'; c <= '9'; c++)
        {
            if (checkCell(board, row, col, c))
            {
                board[row][col] = c;
                if (doSolveSudoku(board, row, col + 1))
                    return true;
                board[row][col] = '.';
            }
        }

        return false;
    }

    void solveSudoku(vector<vector<char> >& board) {
        doSolveSudoku(board, 0, 0);
    }
};
Runtime: 49 ms

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EnjoyCodingAndGame

愿我的知识,成为您的财富!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值