首页 > 其他 > 详细

Sudoku Solver

时间:2016-02-07 17:25:19      阅读:215      评论:0      收藏:0      [点我收藏+]

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

原文:http://www.cnblogs.com/SpeakSoftlyLove/p/5184705.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!