我曾经写过一个包含C语言求解器的数独游戏,并希望用Java进行尝试,以便人们可以更轻松地使用它(可移植性)。我认为由于语言之间存在巨大的相似性,端口会相当简单,但似乎有点痛苦。
我的求解器无限递归,这在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;
}
我知道这不一定是最快或最好的书面解决方案,但它有效。它只是找到具有最小可能值的图块,然后从随机值开始并尝试所有可能的值,直到一个结果产生可解决的拼图(使用递归)。 sudoku_get_mask返回一个整数,其前9位设置为相应的值。它检查已经使用过的值的水平,垂直和子方块,并从掩码中删除它们。
现在,这是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;
}
它们几乎相同,所以我无法弄清楚为什么Java端口无限递归!求解器应始终为1.找到解决方案或2.发现没有解决方案。按照我的逻辑,我看不出它应该无限地递归的方式。
以下是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;
}
这是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} }
};
答案 0 :(得分:1)
我想史密斯先生不会提交官方回答(我想我会让他获得积分)。
问题是std C函数rand()返回范围内的整数:[0,INT_MAX]和Java函数Randomizer.nextInt()在[INT_MIN,INT_MAX]范围内。我不得不用“generator.randInt(9)”替换“generator.nextInt()%9”并且它有效。