まず第一に、これは大学の課題であるため、誰かにコードを書いてもらうように頼んでいるわけではありません。正しい方向に向ける必要があるだけです。:)
では、任意のサイズの (解ける) 数独ボードを解くアルゴリズムを作成する必要があります。任意の 9x9 ボードをすばやく (~1ms) 解決できる再帰関数を作成しましたが、解決するのが難しい大きなボード (16x16) を実行すると苦労します.解決しそうにない。簡単な 16x16 のパズルや空白の 16x16 のボードを解くことができるので、問題は寸法ではないと思います.. 問題はアルゴリズムである可能性が高いと思います.
とにかく、これは私のプログラムの基本的なロジックです..
- すべての正方形の可能な値を格納する 3D ベクトルがあります
- 値が正方形に配置されると、周囲の正方形、行、および列の可能な値から削除されます。
次に、私の解決機能は基本的に次のとおりです。
bool solve() {
if (there are no unfilled squares)
return true
if (the board is unsolvable - there are empty squares that have no possible values)
return false
while (there are empty squares)
{
int squaresFilled = fillSquaresWithOnlyOneChoice(); //this method updates the possible results vector whenever it fills a square
if (squaresFilled == 0)
break;
}
//exhausted all of the 'easy' squares (squares with only one possible choice), need to make a guess
while (there are empty squares that have choices left) {
find the square with the least number of choices
if (the square with the least number of choices has 0 choices)
return false; //not solvable.
remove that choice from the 3D vector (vector that has the choices for each square)
make a copy of the board and the 3D choices vector
fill the square with the choice
if (solve())
return true; //we're done
restore the board and choices vector
//the guess didn't work so keep looping and make a new guess with the restored board and choices -- the choice we just made has been removed though so it won't get made again.
}
return false; //can't go any further
}
これについて何か非効率的なことはありますか?うまく機能させる方法はありますか?16x16 のボードに非常に時間がかかるのは、ボードの決定木が非常に大きく、あまり埋められていないためだと思います。奇妙なことに、9x9 ボードは非常に速く解けるからです。
アイデアや提案は絶対に素晴らしいでしょう。私が見逃した情報があれば、私にも知らせてください!