码迷,mamicode.com
首页 > 编程语言 > 详细

求解数独回溯算法

时间:2015-10-13 09:13:55      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:

实现的java代码如下:

    //判断a[i][j]取值val是否有效
    public boolean isValid(int[][] a, int i, int j, int val){
        //判断是否跟同行冲突
        for(int j1=0;j1<9;j1++){
            if(a[i][j1]==val)
                return false;
        }
        //判断是否跟同列冲突
        for(int i1=0;i1<9;i1++){
            if(a[i1][j]==val)
                return false;
        }
        //找出a[i][j]所在的九宫格
        int i1 = 0, j1 = 0;
        boolean flag = true;
        for(i1=0;i1<3&&flag;i1++){
            if(!(i>=i1*3&&i<3*(i1+1)))
                continue;
            for(j1=0;j1<3;j1++){
                if(j>=j1*3&&j<3*(j1+1)){
                    flag = false;
                    break;
                }
            }
        }
        i1--;
        //判断值是否跟所在九宫格冲突
        for(int i2=3*i1;i2<3*(i1+1);i2++){           
            for(int j2=3*j1;j2<3*(j1+1);j2++){
                if(a[i2][j2]==val)
                    return false;
            }
        }
        return true;
    }
    //打印数独
    public void printMatrix(int[][] a){
        for(int i=0;i<9;i++){
            for(int j=0;j<9;j++){
                System.out.print(a[i][j]+" ");
            }
            System.out.println();
        }
    }
    //回溯法求解数独
    public void shuDu(int[][] a, int i, int j){
        if(i==8&&j>=9){
            printMatrix(a);
            return;
        }
        if(j==9){
            j=0;
            i++;
        }
        if(a[i][j]==0){
            for(int k=1;k<=9;k++){
                if(isValid(a,i,j,k)){
                    a[i][j] = k;
                    //向下继续找
                    shuDu(a,i,j+1);
                    //如果没有找到全部答案将a[i][j]的值恢复
                    a[i][j] = 0;
                }
            }
        }else{
            shuDu(a,i,j+1);
        }
    }

算法调用示例如下:

public static void main(String[] args) {
                Solution solu = new Solution();
        int ma1[][]={
                 {0,3,0,0,0,5,0,6,0},
                 {0,1,0,0,0,3,0,8,0},
                 {0,4,0,0,0,0,0,0,7},
                 {0,0,7,0,2,4,0,0,0},
                 {5,0,0,0,9,0,0,0,0},
                 {0,8,0,3,0,0,5,0,0},
                 {0,0,0,8,0,0,0,0,0},
                 {0,0,9,0,0,0,0,7,3},
                 {0,5,0,9,0,0,0,0,2}};
        int[][] sudoku = { 
                 {8,0,0,0,0,0,0,0,0}, 
                 {0,0,3,6,0,0,0,0,0}, 
                 {0,7,0,0,9,0,2,0,0}, 
                 {0,5,0,0,0,7,0,0,0}, 
                 {0,0,0,0,4,5,7,0,0}, 
                 {0,0,0,1,0,0,0,3,0}, 
                 {0,0,1,0,0,0,0,6,8},  
                 {0,0,8,5,0,0,0,1,0}, 
                 {0,9,0,0,0,0,4,0,0}};
        solu.shuDu(sudoku, 0, 0);
}            

 

求解数独回溯算法

标签:

原文地址:http://www.cnblogs.com/gaopeng527/p/4873447.html

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