简体   繁体   English

递归数独求解器在Java中不起作用

[英]Recursive Sudoku Solver doesn't work in Java

I had written a Sudoku game including a solver in C, and wanted to try it out in Java so people could use it a little easier (portability). 我用C语言编写了一个包含求解器的Sudoku游戏,并想在Java中进行尝试,以便人们可以更轻松地使用它(可移植性)。 I figured the port would be fairly simple because of the vast similarities between the languages, but it seems it is being a bit of a pain. 由于语言之间的巨大相似性,我认为端口将相当简单,但似乎有点麻烦。

My solver is recursing infinitely, which never happened in C. Here is my original C function for solving the puzzle: 我的求解器无限递归,这在C语言中从未发生过。这是我最初用于解决难题的C函数:

int sudoku_solve(struct sudoku* sudoku)
{
    if(!sudoku) return 0;

    int mask = 0x1ff;
    int best_x = 0, best_y = 0;
    int best_mask = 0x2ff;


    for(int y = 0; y < 9; ++y){
        for(int x = 0; x < 9; ++x){
            if( sudoku->grid[y][x] != 0 ) continue;
            mask = sudoku_get_mask(sudoku, x, y);
            if( mask < best_mask ){
                best_mask = mask;
                best_x = x;
                best_y = y;
            }
        }
    }

    if( best_mask == 0x2ff ) return 1; // this puzzle is already solved!

    if( best_mask == 0x000 ) return 0; // this puzzle can't be solved!

    int start_c = rand() % 9;
    int c = start_c;
    do{
        if( (best_mask & (1<<c)) ){
            sudoku->grid[best_y][best_x] = c+1;
            if( sudoku_solve(sudoku) ) return 1;
        }
        c = (c+1) % 9;
    } while( c != start_c );

    sudoku->grid[best_y][best_x] = 0;


    return 0;
}

I know this isn't necessarily the fastest or best written solver, but it worked. 我知道这不一定是最快或最好的书面求解器,但它确实有效。 It simply finds the tile with the least possible values and then starts at a random values and tries all possible values until one results in a solvable puzzle (using recursion). 它只是找到具有最小可能值的图块,然后从一个随机值开始,然后尝试所有可能的值,直到得出可解决的难题(使用递归)。 The sudoku_get_mask returns an integer with the first 9 bits set for the corresponding value. sudoku_get_mask返回一个整数,其中前9位设置为相应的值。 It checks the horizontal, vertical, and sub-squares for values which are already used and removes them from the mask. 它检查水平,垂直和子正方形中是否有已使用的值,并将其从蒙版中删除。

Now, here is the Java port: 现在,这是Java端口:

public int Solve()
{
    int mask = 0x2FF;
    int bmask = 0x2FF, bx = 0, by = 0;

    for(int y = 0; y < 9; ++y){
        for(int x = 0; x < 9; ++x){
            if( grid[y][x] != 0 ) continue; // ignore spaces with values already set
            mask = GetMask(x, y);
            if( mask < bmask ) // less bits set == less possible choices
            {
                bmask = mask;
                bx = x;
                by = y;
            }
        }
    }

    if( bmask == 0x2FF ) // the puzzle had no good slots, it must be solved
        return 1;

    if( bmask == 0 ) // the puzzle is unsolvable
        return -1;

    int start_c = rand() % 9;
    int c = start_c;
    do{
        if( (bmask & (1<<c)) != 0 ){
            grid[by][bx] = (char) (c+1);
            if( Solve() == 1 ) return 1;
        }
        c = (c+1)%9;
    }while( c != start_c );

    grid[by][bx] = 0; // restore old value

    return 0;
}

They are almost identical, so I cannot figure out why the Java port is recursing infinitely! 它们几乎是相同的,所以我不知道为什么Java端口无限递归! The solver should always either 1. find a solution or 2. find that there is no solution. 求解器应该始终是1.找到解决方案或2.找到没有解决方案。 By my logic, I can't see a way it should recurse infinitely. 按照我的逻辑,我看不到它应该无限递归的方法。

Here is the GetMask Java code: 这是GetMask Java代码:

protected int GetMask(int x, int y)
{
    int mask = 0x1FF;
    for(int cx = 0; cx < 9; ++cx){
        mask &= (grid[y][cx] == 0 ? mask : ~(1 << (grid[y][cx]-1)));
    }
    for(int cy = 0; cy < 9; ++cy){
        mask &= (grid[cy][x] == 0 ? mask : ~(1 << (grid[cy][x]-1)));
    }
    int idx = squareIndex[y][x];
    int[] pt = null;
    for(int c = 0; c < 9; ++c){
        pt = squarePoint[idx][c];
        mask &= (grid[pt[1]][pt[0]] == 0 ? mask : ~(1 << (grid[pt[1]][pt[0]]-1)));
    }
    return mask;
}

Here is squareIndex, and squarePoint (just lookup tables for the sub-squares): 这是squareIndex和squarePoint(仅是子正方形的查找表):

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

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

I guess Mister Smith isn't going to submit an official answer (I figured I'd let him for the points). 我猜史密斯先生不会提交正式答复(我想我可以让他提出要点)。

the problem was that the std C function rand() returns integers in the range: [0,INT_MAX] and the Java function Randomizer.nextInt() is in the range [INT_MIN,INT_MAX]. 问题在于标准C函数rand()返回范围为[0,INT_MAX]的整数,而Java函数Randomizer.nextInt()则为[INT_MIN,INT_MAX]范围。 I had to replace "generator.nextInt() % 9" with "generator.randInt(9)" and it worked. 我必须将“ generator.nextInt()%9”替换为“ generator.randInt(9)”,它才有效。

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

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