简体   繁体   English

Java数独求解器不会更改空单元格

[英]Java sudoku solver not changing empty cells

I am in the process of writing a sudoku solver (still need to write the box check and actually complete the program) but I'm testing it as I know. 我正在编写一个数独求解器(仍然需要编写盒子检查并实际完成程序),但我正在测试它,因为我知道。 The puzzle I'm testing right now is "very easy" as in there is only one empty cell in any row/column. 我正在测试的难题是“非常容易”,因为在任何行/列中只有一个空单元格。 THe puzzle starts with "empty" cells as zeros. 这个谜题以“空”单元格开始为零。 My issues is that when I run the program and print out the puzzle after solve() is called, the zeros aren't changing and the original puzzle is just printed out. 我的问题是,当我调用solve()后运行程序并打印出拼图时,零点不会改变,原始拼图只是打印出来。 Not sure what my issue is, would appreciate some direction! 不确定我的问题是什么,会欣赏一些方向!

public ArrayList<Integer> create(){

    ArrayList<Integer> possible = new ArrayList<Integer>(); 

    for(int i=1; i<10; i++){
        possible.add(i);
    }
    return possible;
}
public sudoku( int size )
{
    SIZE = size;
    N = size*size;

    Grid = new int[N][N];
    for( int i = 0; i < N; i++ ) 
        for( int j = 0; j < N; j++ ) 
            Grid[i][j] = 0;
}

public void solve()
{ 
    int a, b, c, d, i, j, k, l; 

    int count = 0;
    int value= 0;

    for(i=0; i<N;i++){
        for(j=0; j<N;j++){  
            if(Grid[i][j]==0){

                ArrayList<Integer> possible = create();

                //check row             
                for(a=0; a<N;a++){
                    for(b=0; b<N; b++){  
                        if(Grid[a][0]==possible.get(a)){
                            possible.set(a, 0);
                        }
                    }
                }
                //check column
                for(c=0; c<N;c++){
                    for(d=0; d<N;d++){  
                        if(Grid[0][d]==possible.get(d)){
                            possible.set(d,0);
                        }
                    }
                }
                for(k=0; k<9; k++){
                    if(possible.get(k)!=0){
                        count++;
                    }
                }
                if(count==1){
                    for(l=0; l<9; l++){
                        if(possible.get(l)!=0){
                            value=possible.get(l);
                        }
                    }
                }
                Grid[i][j]=value;
            }
        }
    }
}
if(Grid[a][0]==possible.get(a))

if(Grid[0][d]==possible.get(d))

You don't use b or c in these lines. 您不在这些行中使用b或c。 You probably want: 你可能想要:

if(Grid[a][i]==possible.get(b))

if(Grid[j][d]==possible.get(c))

Also, the Grid[i][j]=value check should be inside the if block. 此外, Grid[i][j]=value检查应该在if块内。

You might want to use a Set for the possible values instead of an ArrayList . 您可能希望将Set用于可能的值而不是ArrayList

Look at your line if(Grid[a][0]==possible.get(a)) (and similar spots). 看看你的行if(Grid[a][0]==possible.get(a)) (以及类似的点)。 What is it doing there vs what do you actually want? 它在做什么与你真正想要的是什么?

Your possible array looks something like this: [1,2,3,4,5,6,7,8,9] 您可能的数组看起来像这样: [1,2,3,4,5,6,7,8,9]

and your grid (just the first row, since you're only checking Grid[a][ 0 ]) might look something like this: [3,7,8,1,2,9,5,0,4] 和你的网格(只是第一行,因为你只检查Grid [a] [ 0 ])可能看起来像这样: [3,7,8,1,2,9,5,0,4]

Your loop is looking at each element stepwise individually and seeing if they're equal, like this: 你的循环分别逐步查看每个元素并查看它们是否相等,如下所示:

if(1 == 3) ... it's not
if(2 == 7) ... it's not
if(3 == 8) ... it's not

... etc ......等

So, as you can see, when you do your 所以,正如你所看到的,当你做你的时候

for(k=0; k<9; k++){
    if(possible.get(k)!=0){
        count++;
    }
}

Your possible array is still going to be full of options most of the time, unless your first row happens to be some variation on [1,2,3,4,5,6,7,8,9] with a 0 in one of the spaces... so count is definitely going to be > 1 你的可能数组在大多数情况下仍然会充满选项,除非你的第一行碰巧是[1,2,3,4,5,6,7,8,9]一些变化,其中0为一空间...所以计数肯定会> 1

So, your next loop ( for(l=0; l<9; l++) ) next gets executed, so value is still (as you initialized it) 0. 所以,你的下一个循环( for(l=0; l<9; l++) )next会被执行,所以value仍然是(当你初始化它时)0。

Try stepping through a debugger on these points and seeing how the arrays are interacting. 尝试逐步调试这些点上的调试器,看看数组是如何交互的。

You are always checking only the first row and first column and the way you check for the possible numbers is also not doing what you want. 您始终只检查第一行和第一列,并且检查可能的数字的方式也不是您想要的。

A few tips first: 首先提示一些:

First of all, it is not necessary to always define a new variable for the loops, you can reuse them and then you won't have too many of them and you won't get so easily confused in them. 首先,没有必要总是为循环定义一个新的变量,你可以重复使用它们,然后你就不会有太多它们,你就不会那么容易混淆它们。

Secondly, if you name all the variables a, b, c, d, etc. you can also easily get confused. 其次,如果你命名所有的变量a,b,c,d等,你也很容易混淆。 While it is ok to name variables in loops as i, j, if you have too many loops, it may be better to think of better names. 虽然可以将循环中的变量命名为i,j,但如果循环太多,那么考虑更好的名称可能会更好。 In this case row and column for example. 在这种情况下,例如行和列。

Why don't you remove the numbers from the possible list? 为什么不从可能的列表中删除这些数字? Something like: 就像是:

int index = possible.indexOf(a);
if (index != -1) possible.remove(index);

Then it would be easier to determine how many values you still have left. 然后,更容易确定您还剩下多少个值。 You can simply do: 你可以简单地做:

if (possible.size()==1) value = possible.get(0);

And one last note, to obey the convention for variable names, you should probably use grid instead of Grid. 最后一点,为了遵守变量名称的约定,你应该使用grid而不是Grid。

And now the code: 现在代码:

public void solve() { 
    int row, column, i;
    int count = 0;
    int value= 0;
    int index = 0;

    for(row=0; row<N; row++){
        for(column=0; column<N; column++){  
            if(Grid[row][column]==0){

                ArrayList<Integer> possible = create();

                //check row             
                for(i=0; i<N; i++){
                    index = possible.indexOf(Grid[row][i]);
                    if (index != -1) possible.remove(index);
                }
                //check column
                for(i=0; i<N; i++){
                    index = possible.indexOf(Grid[i][column]);
                    if (index != -1) possible.remove(index);
                }

                if (possible.size()==1) value = possible.get(0);

                Grid[row][column]=value;
            }
        }
    }
}

EDIT: Rewritten the whole answer into a better form. 编辑:将整个答案重写为更好的形式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM