Sudoku Solver

Write a program to solve a Sudoku puzzle by filling the empty cells.

A sudoku solution must satisfy all of the following rules:

  1. Each of the digits 1-9 must occur exactly once in each row.
  2. Each of the digits 1-9 must occur exactly once in each column.
  3. Each of the the digits 1-9 must occur exactly once in each of the 9 3x3sub-boxes of the grid.

Empty cells are indicated by the character '.'.


A sudoku puzzle...


...and its solution numbers marked in red.

Note:

  • The given board contain only digits 1-9 and the character '.'.
  • You may assume that the given Sudoku puzzle will have a single unique solution.
  • The given board size is always 9x9.

 

题目的要求是根据规则填满九宫格,这道题一个比较简单的思路上使用递推,我们按从左到右,从上到下的顺序:

  1. 首先找到第一个空格
  2. 然后确定这个空格可能被填入的数字,填入这个数字后九宫格还能满足题目中的要求
  3. 找到第一个空格中可能被填入的数字后,在这些数字中依次选一个填进去,然后,寻找下一个空格,从这里进入递推
  4. 寻找下一个空格,然后在上一个空格已经被填入的情况下,确定本空格可能被填入的数字,如果本空格没有可填入的数字,则说明上一步填入的数字不对,返回第3步
  5. 直至所有空格都被填满,程序结束

递归的结束条件:

  1. 一是当空格中没有可填入的数字时,需返回
  2. 二是当填入当前所有可填入的数字都不能满足条件时,需将本空格重置为空,并返回
  3. 三是当所有的空格都被填满时

代码如下:

class Solution {
public:
    bool solveSudoku(vector<vector<char>>& board) {
        
        for( int i=0;i<9;i++ ){
            for( int j=0;j<9;j++ ){
                
                if( board[i][j] == '.' ){
                    vector<char> av = availableValue(i,j,board) ;
                    
                    if( av.empty() ){
                        // cout<<"empty"<<endl ;
                        return false;
                    }
                    
                    int size = av.size() ;
                
                    for( int k=0;k<size;k++ ){
                        board[i][j] = av[k] ;
                        
                        // printRes(board) ;
                        
                        bool res = solveSudoku(board) ;
                        // cout<<res<<endl ;
                        if( res ){
                            return true ;
                        }
                        else{
                            board[i][j] = '.' ;
                        }
                    }
                    
                    if( board[i][j] == '.' ){
                        return false ;
                    }
                    
                   
                }
            }
        }
        return true ;
    }
    
    //find the available value of the specific cell
    vector<char> availableValue(const int x,const int y,const vector<vector<char>>& board){
        int a[10] = {0} ;
        
        for( int i=0;i<9;i++ ){
            if( board[x][i] != '.' ){
                a[board[x][i]-'0'] = 1 ;
            }
        }
        
        for( int i=0;i<9;i++ ){
            if( board[i][y] !='.' ){
                a[board[i][y]-'0'] = 1 ;
            }
        }
        
        int x0 = 3*(x/3) ;
        int y0 = 3*(y/3) ;
        
        for( int i=x0;i<x0+3;i++ ){
            for( int j=y0;j<y0+3;j++ ){
                if( board[i][j] != '.' ){
                     a[board[i][j]-'0'] = 1 ;
                }
            }
        }
        
        vector<char> v ;
        for( int i=1;i<10;i++ ){
            if( a[i]==0 ){
                v.push_back('0'+i) ;
            }
        }
        
        return v ;
        
    }
        
};

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值