简体   繁体   中英

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). 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:

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. 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:

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! The solver should always either 1. find a solution or 2. find that there is no solution. By my logic, I can't see a way it should recurse infinitely.

Here is the GetMask Java code:

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):

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]. I had to replace "generator.nextInt() % 9" with "generator.randInt(9)" and it worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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