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...

 

 

 

class Solution {
public:
    //检查当前位置的值是否符合要求
    bool check(vector<vector<char>>& board, int row, int col) {
        int n = board.size();
        int i, j;
        int tmp = board[row][col];
        //判断这一行
        for (i=0; i<n; i++) {
            if (i == row) {
                continue;
            }
            if (board[i][col] == tmp) {
                return false;
            }
        }

        //判断这一列
        for (i=0; i<n; i++) {
            if (i == col) {
                continue;
            }
            if (board[row][i] == tmp) {
                return false;
            }
        }

        //判断方块
        for (i = row / 3 * 3; i < row / 3 * 3 + 3; i++) {
            for (j = col / 3 * 3; j< col /3 * 3 + 3; j++) {
                if (i == row && j == col) {
                    continue;
                }
                if (board[i][j] == tmp) {
                    return false;
                }
            }
        }

        return true;
    }
    //获取下一个需要填写的位置,如果找到了就返回成功,并修改row 和col, 如果找不到就返回失败
    bool get_next_position(vector<vector<char>>& board, int &row, int &col) {
        int i;
        int j;
        int n = board.size();

        for (j=col; j<n; j++) {
            if (board[row][j] == '.') {
                col = j;
                return true;
            }
        }

        if (row == n - 1) {
            return false;
        }

        for (i=row+1; i < n; i++) {
            for (j=0; j < n; j++) {
                if (board[i][j] == '.') {
                    row = i;
                    col = j;
                    return true;
                }
            }
        }
        return false;
    }
    /*
     * board[i][j]赋值1,如果成功,找下一个空白进行赋值
     * 如果失败,board[i][j]++, 如果board[i][j]为9,仍然失败,对上一个空白进行++
     *
     * */
    bool solve(vector<vector<char>>& board, int i, int j) {
        //给当前位置赋值,如果赋值后检测OK,就找一个位置继续。
        //如果1-9都失败了,就返回失败
        board[i][j] = '1';
        while (board[i][j]<='9') {
            while (!check(board, i, j)) {
                board[i][j]++;
            }
            //如果1-9都check失败,就讲当前位置设置为".",然后将返回上一个位置,并将内容++
            if (board[i][j] == ':') {
                board[i][j] = '.';
                return false;
            }

            int next_i = i;
            int next_j = j;
            //如果找不到下个,说明整个数组都遍历完了
            if (!get_next_position(board, next_i, next_j)) {
                return true;
            }
            if (solve(board, next_i, next_j)) {
                return true;
            } else {
                board[i][j]++;
            }
        }
        board[i][j] = '.';
        return false;
    }

    void solveSudoku(vector<vector<char>>& board) {
        int i = 0;
        int j = 0;
        if (!get_next_position(board, i, j)) {
            return;
        }
        solve(board, i, j);

    }
};

 

你可能感兴趣的:(Sudoku Solver)