gpt4 book ai didi

java - 数独生成器 : Recursion not working

转载 作者:行者123 更新时间:2023-11-30 07:40:21 25 4
gpt4 key购买 nike

我想要实现的目标:

while(there are empty squares)
get a random number of the remaining numbers
if(the number hasn't been tried before)
if(it fits)
place it and remove from available
next square
if(all numbers have been tried)
forget it
move back a square and try a different number

这个循环在 availableNumbers = 64 处停止,有时是 37,有时是 81 等。是什么阻止了它一直进行下去?为什么有时会卡住,有时又走回头路?

while(availableNumbers.size()>0){
rand = rn.nextInt(availableNumbers.size());
number = availableNumbers.get(rand);
if(number != triedNumbers[y][x][number-1]){ // If the number hasn't been tried in this square
if(!checkConflict(number)){
squares[y][x].setText(""+number);
availableNumbers.remove(new Integer(number));
addedNumbers[y][x] = number;
triedNumbers[y][x][number-1] = number;
moveAhead();
}
}
if(Arrays.equals(triedNumbers[y][x],refArray)){ // If all numbers have been tried in that square
emptySquare(); // Forget all numbers tried in current square
moveBack();
}
}

emptySquare、moveBack、moveAhead 和 checkConflicts 都按预期工作(根据我的测试)。以下是他们的要点:https://gist.github.com/WQvist/ac4296fb3a86fdc3d713

编辑:我终于解决了。感谢您的所有帮助。看我的回答。

最佳答案

您的伪代码不正确。应该是:

while(there are empty squares)
get a random number of the remaining numbers
if(the number hasn't been tried before)
if(it fits)
place it and remove from available
next square
while(all numbers have been tried)
forget it
move back a square
try a different number

有时您必须返回多个方格才能到达尚未尝试所有数字的方格。

关于java - 数独生成器 : Recursion not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34783551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com