数独的Java与Java中的Sudoku相比

时间:2013-08-04 11:38:34

标签: java c reference sudoku

我最近编写了一个简单直接的C代码,它解决了Sudoku并使用了递归和回溯。您将在下面找到我刚才描述的代码。一切正常。

#include <stdio.h>
# define INsigned 0

int grid[9][9];
void printGrid(){
int row,col;

for(row=0;row<9;row++){
    for(col=0;col<9;col++){
        printf("%2d",grid[row][col]);
        }
    printf("\n");
        }   
}

int Unsigned(int *row, int* col){
int i,j;
for(i=0;i<9;i++){
    for(j=0;j<9;j++){

        if(grid[i][j]==0){
            *row=i;
            *col=j;
            return 1;


        }
    }

}
return 0;

}
int check(){
int row=0;
int col=0;
int i;
if(Unsigned(&row, &col)==0)
    return 1;


for(i=1;i<=9;i++){
    if(conflict(i,row-row%3, col-col%3, row, col)==0){

        grid[row][col]=i;
        if(check()==1)
            return 1;
    }


    grid[row][col]=0;
}

return 0;
}

int conflict (int num, int srow, int scol, int row, int col){

int i,j;
for(i=0;i<9;i++){
    if(grid[row][i]==num) return 1;
    if(grid[i][col]==num) return 2;
}

for(i=0;i<3;i++){
    for(j=0;j<3;j++){
        if(grid[i+srow][j+scol]==num) return 3;
    }
}
return 0;

}   

int main(){

int i;

for(i=0;i<9;i++){
    scanf("%d %d %d %d %d %d %d %d %d",&grid[i][0],&grid[i][1],&grid[i][2],
                                       &grid[i][3],&grid[i][4],&grid[i][5],
                                       &grid[i][6],&grid[i][7],&grid[i][8]);
}

check();
printf("\n\n");
printGrid();

}

输入是:

0 0 0 0 0 9 0 0 3
0 0 5 0 0 0 0 9 0
3 0 0 8 1 0 2 0 0
0 0 0 0 4 3 1 0 0
5 6 0 0 9 0 0 7 8
0 0 3 5 6 0 0 0 0
9 0 6 0 8 7 0 0 4
0 2 0 0 0 0 8 0 0
1 0 0 9 0 0 0 0 0

当我尝试在java中编写精确算法时,我遇到了这个问题。因为java不允许通过引用传递原始类型,因为我在无符号函数中使用了引用传递来查找下一个空单元格。我决定在java中使用一个包装器类来克服这个障碍,但是这个代码的java等价物应该是不行的。这里是代码。

    public class intObj {

        public int value;
    }





public class Grid {

int grid[][] = new int[9][9];
int r = 0;
int c = 0;

public Grid(int grid[][]) {
    this.grid = grid;
}

void printGrid() {
    int row;
    int col;
    System.out.println("");
    for (row = 0; row < 9; row++) {
        for (col = 0; col < 9; col++) {
            System.out.printf("%2d", grid[row][col]);
        }
        System.out.println("");

    }
}

int conflict(int num, int srow, int scol, int row, int col) {

    int i, j;
    for (i = 0; i < 9; i++) {
        if (grid[row][i] == num) {
            return 1;
        }
        if (grid[i][col] == num) {
            return 2;
        }
    }

    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            if (grid[i + srow][j + scol] == num) {
                return 3;
            }
        }
    }
    return 0;

}

int Unsigned(intObj row, intObj col) {

    for (int i = 0; i < 9; i++) {
        for (int j = 0; j < 9; j++) {
            if (grid[i][j] == 0) {

                row.value = i;
                col.value = j;
                return 1;
            }
        }
    }
    return 0;

}

int check() {

    intObj row = new intObj();
    intObj col = new intObj();

    if (Unsigned(row, col) == 0) {
        return 1;
    }

    for (int i = 1; i <= 9; i++) {
        if (conflict(i, (row.value) - (row.value) % 3,
                (col.value) - (col.value) % 3, row.value, col.value) == 0) {


            grid[row.value][col.value] = i;
            if (check() == 1) {
                return 1;
            }

        }

        grid[row.value][col.value] = 0;
    }

    return 0;

}
}

主要班级:

public class Main {

public static void main(String args[]) {

    int grid[][] = {{0, 0, 0, 0, 0, 9, 0, 0, 3},
        {0, 0, 5, 0, 0, 0, 0, 9, 3},
        {3, 0, 0, 8, 1, 0, 2, 0, 0},
        {0, 0, 0, 0, 4, 3, 1, 0, 0},
        {5, 6, 0, 0, 9, 0, 0, 7, 8},
        {0, 0, 3, 5, 6, 0, 0, 0, 0},
        {9, 0, 6, 0, 8, 7, 0, 0, 4},
        {0, 2, 0, 0, 0, 0, 8, 0, 0},
        {1, 0, 0, 9, 0, 0, 0, 0, 0}};

    Grid g = new Grid(grid);


    g.check();

    g.printGrid();

}
}

对不起,长篇文章会非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我能想到的最简单的解决方案是“将你的未签名代码移入check()本身”

int check(){
int row=0;
int col=0;
int i;
if(Unsigned(&row, &col)==0) //Move Unsigned() method code here and that's it
    return 1;

II'nd解决方案:您在check()中创建的这两个对象  方法必须是你的Grid类的一部分,不要在check()方法中本地创建它。它应该解决你的问题

 intObj row = new intObj();
    intObj col = new intObj();