码迷,mamicode.com
首页 > 其他好文 > 详细

Sudoku Solver

时间:2015-01-06 00:49:42      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:

解决数独问题

class Solution {
public:
    bool isValid(vector<vector<char> >&board, int a, int b){
        for(int i = 0; i < 9; i++)
            if(i != a && board[i][b] == board[a][b])
                return false;
        for(int j = 0; j < 9; j++)
            if(j != b && board[a][j] == board[a][b])
                return false;
            int x = a/3*3;
            int y = b/3*3;
        for(int i = 0; i < 3; i++)
            for(int j = 0; j< 3; j++)
                if(x+i != a && y+j != b && board[x+i][y+j] == board[a][b])
                    return false;
        return true;
    }
    bool dfs(vector<vector<char> > &board){
        for(int i = 0; i < 9; i++)
            for(int j = 0; j < 9; j++)
                if(board[i][j] == '.'){
            for(int k = 1; k <= 9; k++){
                board[i][j] = '0'+ k;
                if(isValid(board,i,j) && dfs(board))
                    return true;
                board[i][j] = '.';
            }
        return false;
       }
       return true;
    }
    void solveSudoku(vector<vector<char> > &board) {
        dfs(board);
    }
};


Sudoku Solver

标签:

原文地址:http://blog.csdn.net/sina012345/article/details/42442271

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!