UIKitとSpriteKit

<spritekit2

見栄えの良いGUIは常に歓迎されるものです。これは、あなたがゲームをプレイする場合には、特にそうです。なので、私はUIKitとSpriteKitを使おうとしています。私にとってはこれらは全然自明では無いのですが、iOSで動く簡単なブログラムが書けるようになればきっと楽しいことでしょう。

Number Place(数独)とSwift 3 (5)

backtrack3

もし、あなたが幸運であれば、あなたはバックトラック無しで、解答へ到達することができるかもしれません。これは、あなたが可能な動きが1つしかない空白の場所を多く見つけることができる場合に、特に真実です。

以下のリストでは、左側のテーブルは、現在の数字の配置を示しており、ドットが埋めるべき場所です。そして、右側のテーブルは、可能な動きの数を示しています。もし、あなたがeval=1である場所を見つければ、あなたはまずそのような場所を埋めるべきです。

nplaces = 52 

index=0, Position(row: 1, col: 7), eval=1
-----------------   -----------------
7 . 3 . . . . . .   . 4 . 4 4 5 3 3 4 
. 5 1 3 7 . 4 . .   3 . . . . 4 . 1 3 
6 . . . 1 . . 2 .   . 3 2 4 . 3 3 . 5 
. . 4 6 . . . . .   4 6 . . 2 3 4 4 4 
. . . . . 7 . 8 .   3 5 3 3 2 . 4 . 5 
5 . . 2 3 1 9 . .   . 3 2 . . . . 2 3 
3 . . . 2 4 . 9 1   . 2 3 3 . . 3 . . 
. . . . . . 8 4 .   3 4 4 3 2 3 . . 4 
. . 7 . 9 . 2 . .   3 4 . 3 . 4 . 3 3 
-----------------   -----------------
index=1, Position(row: 5, col: 7), eval=1
-----------------   -----------------
7 . 3 . . . . . .   . 4 . 4 4 5 2 2 3 
. 5 1 3 7 . 4 6 .   3 . . . . 3 . . 2 
6 . . . 1 . . 2 .   . 3 2 4 . 3 3 . 5 
. . 4 6 . . . . .   4 6 . . 2 3 4 4 4 
. . . . . 7 . 8 .   3 5 3 3 2 . 4 . 5 
5 . . 2 3 1 9 . .   . 3 2 . . . . 1 3 
3 . . . 2 4 . 9 1   . 2 3 3 . . 3 . . 
. . . . . . 8 4 .   3 4 4 3 2 3 . . 4 
. . 7 . 9 . 2 . .   3 4 . 3 . 4 . 2 3 
-----------------   -----------------
index=2, Position(row: 0, col: 6), eval=2
-----------------   -----------------
7 . 3 . . . . . .   . 4 . 4 4 5 2 2 3 
. 5 1 3 7 . 4 6 .   3 . . . . 3 . . 2 
6 . . . 1 . . 2 .   . 3 2 4 . 3 3 . 5 
. . 4 6 . . . . .   4 6 . . 2 3 3 3 3 
. . . . . 7 . 8 .   3 5 3 3 2 . 4 . 5 
5 . . 2 3 1 9 7 .   . 2 2 . . . . . 2 
3 . . . 2 4 . 9 1   . 2 3 3 . . 3 . . 
. . . . . . 8 4 .   3 4 4 3 2 3 . . 4 
. . 7 . 9 . 2 . .   3 4 . 3 . 4 . 2 3 
-----------------   -----------------
index=3, Position(row: 0, col: 7), eval=1
-----------------   -----------------
7 . 3 . . . 1 . .   . 4 . 4 4 5 . 1 3 
. 5 1 3 7 . 4 6 .   3 . . . . 3 . . 2 
6 . . . 1 . . 2 .   . 3 2 4 . 3 3 . 5 
. . 4 6 . . . . .   4 6 . . 2 3 2 3 3 
. . . . . 7 . 8 .   3 5 3 3 2 . 3 . 5 
5 . . 2 3 1 9 7 .   . 2 2 . . . . . 2 
3 . . . 2 4 . 9 1   . 2 3 3 . . 3 . . 
. . . . . . 8 4 .   3 4 4 3 2 3 . . 4 
. . 7 . 9 . 2 . .   3 4 . 3 . 4 . 2 3 
-----------------   -----------------
index=4, Position(row: 8, col: 7), eval=1
-----------------   -----------------
7 . 3 . . . 1 5 .   . 4 . 3 3 4 . . 2 
. 5 1 3 7 . 4 6 .   3 . . . . 3 . . 2 
6 . . . 1 . . 2 .   . 3 2 4 . 3 2 . 4 
. . 4 6 . . . . .   4 6 . . 2 3 2 2 3 
. . . . . 7 . 8 .   3 5 3 3 2 . 3 . 5 
5 . . 2 3 1 9 7 .   . 2 2 . . . . . 2 
3 . . . 2 4 . 9 1   . 2 3 3 . . 3 . . 
. . . . . . 8 4 .   3 4 4 3 2 3 . . 4 
. . 7 . 9 . 2 . .   3 4 . 3 . 4 . 1 3 
-----------------   -----------------
index=5, Position(row: 3, col: 7), eval=1
-----------------   -----------------
7 . 3 . . . 1 5 .   . 4 . 3 3 4 . . 2 
. 5 1 3 7 . 4 6 .   3 . . . . 3 . . 2 
6 . . . 1 . . 2 .   . 3 2 4 . 3 2 . 4 
. . 4 6 . . . . .   4 6 . . 2 3 2 1 3 
. . . . . 7 . 8 .   3 5 3 3 2 . 3 . 5 
5 . . 2 3 1 9 7 .   . 2 2 . . . . . 2 
3 . . . 2 4 . 9 1   . 2 3 3 . . 3 . . 
. . . . . . 8 4 .   3 4 4 3 2 3 . . 3 
. . 7 . 9 . 2 3 .   3 4 . 3 . 3 . . 2 
-----------------   -----------------
index=6, Position(row: 0, col: 8), eval=2
-----------------   -----------------
7 . 3 . . . 1 5 .   . 4 . 3 3 4 . . 2 
. 5 1 3 7 . 4 6 .   3 . . . . 3 . . 2 
6 . . . 1 . . 2 .   . 3 2 4 . 3 2 . 4 
. . 4 6 . . . 1 .   3 5 . . 2 3 2 . 3 
. . . . . 7 . 8 .   3 5 3 3 2 . 3 . 5 
5 . . 2 3 1 9 7 .   . 2 2 . . . . . 2 
3 . . . 2 4 . 9 1   . 2 3 3 . . 3 . . 
. . . . . . 8 4 .   3 4 4 3 2 3 . . 3 
. . 7 . 9 . 2 3 .   3 4 . 3 . 3 . . 2 
-----------------   -----------------
index=7, Position(row: 1, col: 8), eval=1
-----------------   -----------------
7 . 3 . . . 1 5 8   . 3 . 2 2 3 . . . 
. 5 1 3 7 . 4 6 .   3 . . . . 3 . . 1 
6 . . . 1 . . 2 .   . 3 2 4 . 3 2 . 3 
. . 4 6 . . . 1 .   3 5 . . 2 3 2 . 3 
. . . . . 7 . 8 .   3 5 3 3 2 . 3 . 5 
5 . . 2 3 1 9 7 .   . 2 2 . . . . . 2 
3 . . . 2 4 . 9 1   . 2 3 3 . . 3 . . 
. . . . . . 8 4 .   3 4 4 3 2 3 . . 3 
. . 7 . 9 . 2 3 .   3 4 . 3 . 3 . . 2 
-----------------   -----------------
index=8, Position(row: 0, col: 3), eval=2
-----------------   -----------------
7 . 3 . . . 1 5 8   . 3 . 2 2 3 . . . 
. 5 1 3 7 . 4 6 9   2 . . . . 2 . . . 
6 . . . 1 . . 2 .   . 3 2 4 . 3 2 . 2 
. . 4 6 . . . 1 .   3 5 . . 2 3 2 . 3 
. . . . . 7 . 8 .   3 5 3 3 2 . 3 . 5 
5 . . 2 3 1 9 7 .   . 2 2 . . . . . 2 
3 . . . 2 4 . 9 1   . 2 3 3 . . 3 . . 
. . . . . . 8 4 .   3 4 4 3 2 3 . . 3 
. . 7 . 9 . 2 3 .   3 4 . 3 . 3 . . 2 
-----------------   -----------------
index=9, Position(row: 0, col: 4), eval=1
-----------------   -----------------
7 . 3 4 . . 1 5 8   . 2 . . 1 3 . . . 
. 5 1 3 7 . 4 6 9   2 . . . . 2 . . . 
6 . . . 1 . . 2 .   . 3 2 3 . 3 2 . 2 
. . 4 6 . . . 1 .   3 5 . . 2 3 2 . 3 
. . . . . 7 . 8 .   3 5 3 2 2 . 3 . 5 
5 . . 2 3 1 9 7 .   . 2 2 . . . . . 2 
3 . . . 2 4 . 9 1   . 2 3 3 . . 3 . . 
. . . . . . 8 4 .   3 4 4 3 2 3 . . 3 
. . 7 . 9 . 2 3 .   3 4 . 3 . 3 . . 2 
-----------------   -----------------
index=10, Position(row: 7, col: 4), eval=1
-----------------   -----------------
7 . 3 4 6 . 1 5 8   . 2 . . . 2 . . . 
. 5 1 3 7 . 4 6 9   2 . . . . 2 . . . 
6 . . . 1 . . 2 .   . 3 2 3 . 3 2 . 2 
. . 4 6 . . . 1 .   3 5 . . 2 3 2 . 3 
. . . . . 7 . 8 .   3 5 3 2 2 . 3 . 5 
5 . . 2 3 1 9 7 .   . 2 2 . . . . . 2 
3 . . . 2 4 . 9 1   . 2 3 3 . . 3 . . 
. . . . . . 8 4 .   3 4 4 3 1 3 . . 3 
. . 7 . 9 . 2 3 .   3 4 . 3 . 3 . . 2 
-----------------   -----------------
index=11, Position(row: 3, col: 4), eval=1
-----------------   -----------------
7 . 3 4 6 . 1 5 8   . 2 . . . 2 . . . 
. 5 1 3 7 . 4 6 9   2 . . . . 2 . . . 
6 . . . 1 . . 2 .   . 3 2 3 . 3 2 . 2 
. . 4 6 . . . 1 .   3 5 . . 1 3 2 . 3 
. . . . . 7 . 8 .   3 5 3 2 1 . 3 . 5 
5 . . 2 3 1 9 7 .   . 2 2 . . . . . 2 
3 . . . 2 4 . 9 1   . 2 3 2 . . 3 . . 
. . . . 5 . 8 4 .   3 4 3 2 . 2 . . 2 
. . 7 . 9 . 2 3 .   3 4 . 2 . 2 . . 2 
-----------------   -----------------
index=12, Position(row: 4, col: 4), eval=1
-----------------   -----------------
7 . 3 4 6 . 1 5 8   . 2 . . . 2 . . . 
. 5 1 3 7 . 4 6 9   2 . . . . 2 . . . 
6 . . . 1 . . 2 .   . 3 2 3 . 3 2 . 2 
. . 4 6 8 . . 1 .   2 4 . . . 2 2 . 3 
. . . . . 7 . 8 .   3 5 3 2 1 . 3 . 5 
5 . . 2 3 1 9 7 .   . 2 2 . . . . . 2 
3 . . . 2 4 . 9 1   . 2 3 2 . . 3 . . 
. . . . 5 . 8 4 .   3 4 3 2 . 2 . . 2 
. . 7 . 9 . 2 3 .   3 4 . 2 . 2 . . 2 
-----------------   -----------------
index=13, Position(row: 0, col: 1), eval=2
-----------------   -----------------
7 . 3 4 6 . 1 5 8   . 2 . . . 2 . . . 
. 5 1 3 7 . 4 6 9   2 . . . . 2 . . . 
6 . . . 1 . . 2 .   . 3 2 3 . 3 2 . 2 
. . 4 6 8 . . 1 .   2 4 . . . 2 2 . 3 
. . . . 4 7 . 8 .   3 5 3 2 . . 3 . 4 
5 . . 2 3 1 9 7 .   . 2 2 . . . . . 2 
3 . . . 2 4 . 9 1   . 2 3 2 . . 3 . . 
. . . . 5 . 8 4 .   3 4 3 2 . 2 . . 2 
. . 7 . 9 . 2 3 .   3 4 . 2 . 2 . . 2 
-----------------   -----------------
index=14, Position(row: 0, col: 5), eval=1
-----------------   -----------------
7 2 3 4 6 . 1 5 8   . . . . . 1 . . . 
. 5 1 3 7 . 4 6 9   1 . . . . 2 . . . 
6 . . . 1 . . 2 .   . 3 2 3 . 3 2 . 2 
. . 4 6 8 . . 1 .   2 3 . . . 2 2 . 3 
. . . . 4 7 . 8 .   3 4 3 2 . . 3 . 4 
5 . . 2 3 1 9 7 .   . 2 2 . . . . . 2 
3 . . . 2 4 . 9 1   . 2 3 2 . . 3 . . 
. . . . 5 . 8 4 .   3 3 3 2 . 2 . . 2 
. . 7 . 9 . 2 3 .   3 4 . 2 . 2 . . 2 
-----------------   -----------------
index=15, Position(row: 1, col: 0), eval=1
-----------------   -----------------
7 2 3 4 6 9 1 5 8   . . . . . . . . . 
. 5 1 3 7 . 4 6 9   1 . . . . 2 . . . 
6 . . . 1 . . 2 .   . 3 2 2 . 2 2 . 2 
. . 4 6 8 . . 1 .   2 3 . . . 1 2 . 3 
. . . . 4 7 . 8 .   3 4 3 2 . . 3 . 4 
5 . . 2 3 1 9 7 .   . 2 2 . . . . . 2 
3 . . . 2 4 . 9 1   . 2 3 2 . . 3 . . 
. . . . 5 . 8 4 .   3 3 3 2 . 2 . . 2 
. . 7 . 9 . 2 3 .   3 4 . 2 . 2 . . 2 
-----------------   -----------------
index=16, Position(row: 1, col: 5), eval=1
-----------------   -----------------
7 2 3 4 6 9 1 5 8   . . . . . . . . . 
8 5 1 3 7 . 4 6 9   . . . . . 1 . . . 
6 . . . 1 . . 2 .   . 2 1 2 . 2 2 . 2 
. . 4 6 8 . . 1 .   2 3 . . . 1 2 . 3 
. . . . 4 7 . 8 .   3 4 3 2 . . 3 . 4 
5 . . 2 3 1 9 7 .   . 2 2 . . . . . 2 
3 . . . 2 4 . 9 1   . 2 3 2 . . 3 . . 
. . . . 5 . 8 4 .   3 3 3 2 . 2 . . 2 
. . 7 . 9 . 2 3 .   2 4 . 2 . 2 . . 2 
-----------------   -----------------
index=17, Position(row: 2, col: 2), eval=1
-----------------   -----------------
7 2 3 4 6 9 1 5 8   . . . . . . . . . 
8 5 1 3 7 2 4 6 9   . . . . . . . . . 
6 . . . 1 . . 2 .   . 2 1 2 . 2 2 . 2 
. . 4 6 8 . . 1 .   2 3 . . . 1 2 . 3 
. . . . 4 7 . 8 .   3 4 3 2 . . 3 . 4 
5 . . 2 3 1 9 7 .   . 2 2 . . . . . 2 
3 . . . 2 4 . 9 1   . 2 3 2 . . 3 . . 
. . . . 5 . 8 4 .   3 3 3 2 . 2 . . 2 
. . 7 . 9 . 2 3 .   2 4 . 2 . 2 . . 2 
-----------------   -----------------
index=18, Position(row: 2, col: 1), eval=1
-----------------   -----------------
7 2 3 4 6 9 1 5 8   . . . . . . . . . 
8 5 1 3 7 2 4 6 9   . . . . . . . . . 
6 . 9 . 1 . . 2 .   . 1 . 2 . 2 2 . 2 
. . 4 6 8 . . 1 .   2 3 . . . 1 2 . 3 
. . . . 4 7 . 8 .   3 4 2 2 . . 3 . 4 
5 . . 2 3 1 9 7 .   . 2 2 . . . . . 2 
3 . . . 2 4 . 9 1   . 2 3 2 . . 3 . . 
. . . . 5 . 8 4 .   3 3 2 2 . 2 . . 2 
. . 7 . 9 . 2 3 .   2 4 . 2 . 2 . . 2 
-----------------   -----------------
index=19, Position(row: 3, col: 5), eval=1
-----------------   -----------------
7 2 3 4 6 9 1 5 8   . . . . . . . . . 
8 5 1 3 7 2 4 6 9   . . . . . . . . . 
6 4 9 . 1 . . 2 .   . . . 2 . 2 2 . 2 
. . 4 6 8 . . 1 .   2 3 . . . 1 2 . 3 
. . . . 4 7 . 8 .   3 4 2 2 . . 3 . 4 
5 . . 2 3 1 9 7 .   . 2 2 . . . . . 2 
3 . . . 2 4 . 9 1   . 2 3 2 . . 3 . . 
. . . . 5 . 8 4 .   3 3 2 2 . 2 . . 2 
. . 7 . 9 . 2 3 .   2 3 . 2 . 2 . . 2 
-----------------   -----------------
index=20, Position(row: 2, col: 5), eval=1
-----------------   -----------------
7 2 3 4 6 9 1 5 8   . . . . . . . . . 
8 5 1 3 7 2 4 6 9   . . . . . . . . . 
6 4 9 . 1 . . 2 .   . . . 2 . 1 2 . 2 
. . 4 6 8 5 . 1 .   2 3 . . . . 1 . 2 
. . . . 4 7 . 8 .   3 4 2 1 . . 3 . 4 
5 . . 2 3 1 9 7 .   . 2 2 . . . . . 2 
3 . . . 2 4 . 9 1   . 2 3 2 . . 3 . . 
. . . . 5 . 8 4 .   3 3 2 2 . 2 . . 2 
. . 7 . 9 . 2 3 .   2 3 . 2 . 2 . . 2 
-----------------   -----------------
index=21, Position(row: 2, col: 3), eval=1
-----------------   -----------------
7 2 3 4 6 9 1 5 8   . . . . . . . . . 
8 5 1 3 7 2 4 6 9   . . . . . . . . . 
6 4 9 . 1 8 . 2 .   . . . 1 . . 2 . 2 
. . 4 6 8 5 . 1 .   2 3 . . . . 1 . 2 
. . . . 4 7 . 8 .   3 4 2 1 . . 3 . 4 
5 . . 2 3 1 9 7 .   . 2 2 . . . . . 2 
3 . . . 2 4 . 9 1   . 2 3 2 . . 3 . . 
. . . . 5 . 8 4 .   3 3 2 2 . 2 . . 2 
. . 7 . 9 . 2 3 .   2 3 . 2 . 1 . . 2 
-----------------   -----------------
index=22, Position(row: 3, col: 6), eval=1
-----------------   -----------------
7 2 3 4 6 9 1 5 8   . . . . . . . . . 
8 5 1 3 7 2 4 6 9   . . . . . . . . . 
6 4 9 5 1 8 . 2 .   . . . . . . 2 . 2 
. . 4 6 8 5 . 1 .   2 3 . . . . 1 . 2 
. . . . 4 7 . 8 .   3 4 2 1 . . 3 . 4 
5 . . 2 3 1 9 7 .   . 2 2 . . . . . 2 
3 . . . 2 4 . 9 1   . 2 3 2 . . 3 . . 
. . . . 5 . 8 4 .   3 3 2 2 . 2 . . 2 
. . 7 . 9 . 2 3 .   2 3 . 2 . 1 . . 2 
-----------------   -----------------
index=23, Position(row: 2, col: 6), eval=1
-----------------   -----------------
7 2 3 4 6 9 1 5 8   . . . . . . . . . 
8 5 1 3 7 2 4 6 9   . . . . . . . . . 
6 4 9 5 1 8 . 2 .   . . . . . . 1 . 2 
. . 4 6 8 5 3 1 .   2 2 . . . . . . 1 
. . . . 4 7 . 8 .   3 4 2 1 . . 2 . 3 
5 . . 2 3 1 9 7 .   . 2 2 . . . . . 2 
3 . . . 2 4 . 9 1   . 2 3 2 . . 3 . . 
. . . . 5 . 8 4 .   3 3 2 2 . 2 . . 2 
. . 7 . 9 . 2 3 .   2 3 . 2 . 1 . . 2 
-----------------   -----------------
index=24, Position(row: 2, col: 8), eval=1
-----------------   -----------------
7 2 3 4 6 9 1 5 8   . . . . . . . . . 
8 5 1 3 7 2 4 6 9   . . . . . . . . . 
6 4 9 5 1 8 7 2 .   . . . . . . . . 1 
. . 4 6 8 5 3 1 .   2 2 . . . . . . 1 
. . . . 4 7 . 8 .   3 4 2 1 . . 2 . 3 
5 . . 2 3 1 9 7 .   . 2 2 . . . . . 2 
3 . . . 2 4 . 9 1   . 2 3 2 . . 2 . . 
. . . . 5 . 8 4 .   3 3 2 2 . 2 . . 2 
. . 7 . 9 . 2 3 .   2 3 . 2 . 1 . . 2 
-----------------   -----------------
index=25, Position(row: 3, col: 8), eval=1
-----------------   -----------------
7 2 3 4 6 9 1 5 8   . . . . . . . . . 
8 5 1 3 7 2 4 6 9   . . . . . . . . . 
6 4 9 5 1 8 7 2 3   . . . . . . . . . 
. . 4 6 8 5 3 1 .   2 2 . . . . . . 1 
. . . . 4 7 . 8 .   3 4 2 1 . . 2 . 3 
5 . . 2 3 1 9 7 .   . 2 2 . . . . . 2 
3 . . . 2 4 . 9 1   . 2 3 2 . . 2 . . 
. . . . 5 . 8 4 .   3 3 2 2 . 2 . . 2 
. . 7 . 9 . 2 3 .   2 3 . 2 . 1 . . 2 
-----------------   -----------------
index=26, Position(row: 3, col: 0), eval=1
-----------------   -----------------
7 2 3 4 6 9 1 5 8   . . . . . . . . . 
8 5 1 3 7 2 4 6 9   . . . . . . . . . 
6 4 9 5 1 8 7 2 3   . . . . . . . . . 
. . 4 6 8 5 3 1 2   1 2 . . . . . . . 
. . . . 4 7 . 8 .   3 4 2 1 . . 2 . 2 
5 . . 2 3 1 9 7 .   . 2 2 . . . . . 2 
3 . . . 2 4 . 9 1   . 2 3 2 . . 2 . . 
. . . . 5 . 8 4 .   3 3 2 2 . 2 . . 2 
. . 7 . 9 . 2 3 .   2 3 . 2 . 1 . . 2 
-----------------   -----------------
index=27, Position(row: 3, col: 1), eval=1
-----------------   -----------------
7 2 3 4 6 9 1 5 8   . . . . . . . . . 
8 5 1 3 7 2 4 6 9   . . . . . . . . . 
6 4 9 5 1 8 7 2 3   . . . . . . . . . 
9 . 4 6 8 5 3 1 2   . 1 . . . . . . . 
. . . . 4 7 . 8 .   2 3 2 1 . . 2 . 2 
5 . . 2 3 1 9 7 .   . 2 2 . . . . . 2 
3 . . . 2 4 . 9 1   . 2 3 2 . . 2 . . 
. . . . 5 . 8 4 .   2 3 2 2 . 2 . . 2 
. . 7 . 9 . 2 3 .   2 3 . 2 . 1 . . 2 
-----------------   -----------------
index=28, Position(row: 4, col: 3), eval=1
-----------------   -----------------
7 2 3 4 6 9 1 5 8   . . . . . . . . . 
8 5 1 3 7 2 4 6 9   . . . . . . . . . 
6 4 9 5 1 8 7 2 3   . . . . . . . . . 
9 7 4 6 8 5 3 1 2   . . . . . . . . . 
. . . . 4 7 . 8 .   2 3 2 1 . . 2 . 2 
5 . . 2 3 1 9 7 .   . 2 2 . . . . . 2 
3 . . . 2 4 . 9 1   . 2 3 2 . . 2 . . 
. . . . 5 . 8 4 .   2 3 2 2 . 2 . . 2 
. . 7 . 9 . 2 3 .   2 3 . 2 . 1 . . 2 
-----------------   -----------------
index=29, Position(row: 8, col: 5), eval=1
-----------------   -----------------
7 2 3 4 6 9 1 5 8   . . . . . . . . . 
8 5 1 3 7 2 4 6 9   . . . . . . . . . 
6 4 9 5 1 8 7 2 3   . . . . . . . . . 
9 7 4 6 8 5 3 1 2   . . . . . . . . . 
. . . 9 4 7 . 8 .   2 3 2 . . . 2 . 2 
5 . . 2 3 1 9 7 .   . 2 2 . . . . . 2 
3 . . . 2 4 . 9 1   . 2 3 2 . . 2 . . 
. . . . 5 . 8 4 .   2 3 2 2 . 2 . . 2 
. . 7 . 9 . 2 3 .   2 3 . 2 . 1 . . 2 
-----------------   -----------------
index=30, Position(row: 7, col: 5), eval=1
-----------------   -----------------
7 2 3 4 6 9 1 5 8   . . . . . . . . . 
8 5 1 3 7 2 4 6 9   . . . . . . . . . 
6 4 9 5 1 8 7 2 3   . . . . . . . . . 
9 7 4 6 8 5 3 1 2   . . . . . . . . . 
. . . 9 4 7 . 8 .   2 3 2 . . . 2 . 2 
5 . . 2 3 1 9 7 .   . 2 2 . . . . . 2 
3 . . . 2 4 . 9 1   . 2 3 2 . . 2 . . 
. . . . 5 . 8 4 .   2 3 2 2 . 1 . . 2 
. . 7 . 9 6 2 3 .   2 2 . 2 . . . . 1 
-----------------   -----------------
index=31, Position(row: 8, col: 8), eval=1
-----------------   -----------------
7 2 3 4 6 9 1 5 8   . . . . . . . . . 
8 5 1 3 7 2 4 6 9   . . . . . . . . . 
6 4 9 5 1 8 7 2 3   . . . . . . . . . 
9 7 4 6 8 5 3 1 2   . . . . . . . . . 
. . . 9 4 7 . 8 .   2 3 2 . . . 2 . 2 
5 . . 2 3 1 9 7 .   . 2 2 . . . . . 2 
3 . . . 2 4 . 9 1   . 2 3 2 . . 2 . . 
. . . . 5 3 8 4 .   2 3 2 2 . . . . 2 
. . 7 . 9 6 2 3 .   2 2 . 2 . . . . 1 
-----------------   -----------------
index=32, Position(row: 4, col: 8), eval=1
-----------------   -----------------
7 2 3 4 6 9 1 5 8   . . . . . . . . . 
8 5 1 3 7 2 4 6 9   . . . . . . . . . 
6 4 9 5 1 8 7 2 3   . . . . . . . . . 
9 7 4 6 8 5 3 1 2   . . . . . . . . . 
. . . 9 4 7 . 8 .   2 3 2 . . . 2 . 1 
5 . . 2 3 1 9 7 .   . 2 2 . . . . . 2 
3 . . . 2 4 . 9 1   . 2 3 2 . . 1 . . 
. . . . 5 3 8 4 .   2 3 2 2 . . . . 2 
. . 7 . 9 6 2 3 5   2 2 . 2 . . . . . 
-----------------   -----------------
index=33, Position(row: 4, col: 2), eval=1
-----------------   -----------------
7 2 3 4 6 9 1 5 8   . . . . . . . . . 
8 5 1 3 7 2 4 6 9   . . . . . . . . . 
6 4 9 5 1 8 7 2 3   . . . . . . . . . 
9 7 4 6 8 5 3 1 2   . . . . . . . . . 
. . . 9 4 7 . 8 6   2 2 1 . . . 1 . . 
5 . . 2 3 1 9 7 .   . 2 2 . . . . . 1 
3 . . . 2 4 . 9 1   . 2 3 2 . . 1 . . 
. . . . 5 3 8 4 .   2 3 2 2 . . . . 1 
. . 7 . 9 6 2 3 5   2 2 . 2 . . . . . 
-----------------   -----------------
index=34, Position(row: 4, col: 0), eval=1
-----------------   -----------------
7 2 3 4 6 9 1 5 8   . . . . . . . . . 
8 5 1 3 7 2 4 6 9   . . . . . . . . . 
6 4 9 5 1 8 7 2 3   . . . . . . . . . 
9 7 4 6 8 5 3 1 2   . . . . . . . . . 
. . 2 9 4 7 . 8 6   1 2 . . . . 1 . . 
5 . . 2 3 1 9 7 .   . 2 2 . . . . . 1 
3 . . . 2 4 . 9 1   . 2 3 2 . . 1 . . 
. . . . 5 3 8 4 .   2 3 1 2 . . . . 1 
. . 7 . 9 6 2 3 5   2 2 . 2 . . . . . 
-----------------   -----------------
index=35, Position(row: 4, col: 1), eval=1
-----------------   -----------------
7 2 3 4 6 9 1 5 8   . . . . . . . . . 
8 5 1 3 7 2 4 6 9   . . . . . . . . . 
6 4 9 5 1 8 7 2 3   . . . . . . . . . 
9 7 4 6 8 5 3 1 2   . . . . . . . . . 
1 . 2 9 4 7 . 8 6   . 1 . . . . 1 . . 
5 . . 2 3 1 9 7 .   . 2 2 . . . . . 1 
3 . . . 2 4 . 9 1   . 2 3 2 . . 1 . . 
. . . . 5 3 8 4 .   1 3 1 2 . . . . 1 
. . 7 . 9 6 2 3 5   1 2 . 2 . . . . . 
-----------------   -----------------
index=36, Position(row: 4, col: 6), eval=1
-----------------   -----------------
7 2 3 4 6 9 1 5 8   . . . . . . . . . 
8 5 1 3 7 2 4 6 9   . . . . . . . . . 
6 4 9 5 1 8 7 2 3   . . . . . . . . . 
9 7 4 6 8 5 3 1 2   . . . . . . . . . 
1 3 2 9 4 7 . 8 6   . . . . . . 1 . . 
5 . . 2 3 1 9 7 .   . 2 2 . . . . . 1 
3 . . . 2 4 . 9 1   . 2 3 2 . . 1 . . 
. . . . 5 3 8 4 .   1 3 1 2 . . . . 1 
. . 7 . 9 6 2 3 5   1 2 . 2 . . . . . 
-----------------   -----------------
index=37, Position(row: 5, col: 8), eval=1
-----------------   -----------------
7 2 3 4 6 9 1 5 8   . . . . . . . . . 
8 5 1 3 7 2 4 6 9   . . . . . . . . . 
6 4 9 5 1 8 7 2 3   . . . . . . . . . 
9 7 4 6 8 5 3 1 2   . . . . . . . . . 
1 3 2 9 4 7 5 8 6   . . . . . . . . . 
5 . . 2 3 1 9 7 .   . 2 2 . . . . . 1 
3 . . . 2 4 . 9 1   . 2 3 2 . . 1 . . 
. . . . 5 3 8 4 .   1 3 1 2 . . . . 1 
. . 7 . 9 6 2 3 5   1 2 . 2 . . . . . 
-----------------   -----------------
index=38, Position(row: 6, col: 6), eval=1
-----------------   -----------------
7 2 3 4 6 9 1 5 8   . . . . . . . . . 
8 5 1 3 7 2 4 6 9   . . . . . . . . . 
6 4 9 5 1 8 7 2 3   . . . . . . . . . 
9 7 4 6 8 5 3 1 2   . . . . . . . . . 
1 3 2 9 4 7 5 8 6   . . . . . . . . . 
5 . . 2 3 1 9 7 4   . 2 2 . . . . . . 
3 . . . 2 4 . 9 1   . 2 3 2 . . 1 . . 
. . . . 5 3 8 4 .   1 3 1 2 . . . . 1 
. . 7 . 9 6 2 3 5   1 2 . 2 . . . . . 
-----------------   -----------------
index=39, Position(row: 6, col: 1), eval=1
-----------------   -----------------
7 2 3 4 6 9 1 5 8   . . . . . . . . . 
8 5 1 3 7 2 4 6 9   . . . . . . . . . 
6 4 9 5 1 8 7 2 3   . . . . . . . . . 
9 7 4 6 8 5 3 1 2   . . . . . . . . . 
1 3 2 9 4 7 5 8 6   . . . . . . . . . 
5 . . 2 3 1 9 7 4   . 2 2 . . . . . . 
3 . . . 2 4 6 9 1   . 1 2 2 . . . . . 
. . . . 5 3 8 4 .   1 3 1 2 . . . . 1 
. . 7 . 9 6 2 3 5   1 2 . 2 . . . . . 
-----------------   -----------------
index=40, Position(row: 5, col: 1), eval=1
-----------------   -----------------
7 2 3 4 6 9 1 5 8   . . . . . . . . . 
8 5 1 3 7 2 4 6 9   . . . . . . . . . 
6 4 9 5 1 8 7 2 3   . . . . . . . . . 
9 7 4 6 8 5 3 1 2   . . . . . . . . . 
1 3 2 9 4 7 5 8 6   . . . . . . . . . 
5 . . 2 3 1 9 7 4   . 1 2 . . . . . . 
3 8 . . 2 4 6 9 1   . . 1 1 . . . . . 
. . . . 5 3 8 4 .   1 3 1 2 . . . . 1 
. . 7 . 9 6 2 3 5   1 1 . 2 . . . . . 
-----------------   -----------------
index=41, Position(row: 5, col: 2), eval=1
-----------------   -----------------
7 2 3 4 6 9 1 5 8   . . . . . . . . . 
8 5 1 3 7 2 4 6 9   . . . . . . . . . 
6 4 9 5 1 8 7 2 3   . . . . . . . . . 
9 7 4 6 8 5 3 1 2   . . . . . . . . . 
1 3 2 9 4 7 5 8 6   . . . . . . . . . 
5 6 . 2 3 1 9 7 4   . . 1 . . . . . . 
3 8 . . 2 4 6 9 1   . . 1 1 . . . . . 
. . . . 5 3 8 4 .   1 2 1 2 . . . . 1 
. . 7 . 9 6 2 3 5   1 1 . 2 . . . . . 
-----------------   -----------------
index=42, Position(row: 6, col: 2), eval=1
-----------------   -----------------
7 2 3 4 6 9 1 5 8   . . . . . . . . . 
8 5 1 3 7 2 4 6 9   . . . . . . . . . 
6 4 9 5 1 8 7 2 3   . . . . . . . . . 
9 7 4 6 8 5 3 1 2   . . . . . . . . . 
1 3 2 9 4 7 5 8 6   . . . . . . . . . 
5 6 8 2 3 1 9 7 4   . . . . . . . . . 
3 8 . . 2 4 6 9 1   . . 1 1 . . . . . 
. . . . 5 3 8 4 .   1 2 1 2 . . . . 1 
. . 7 . 9 6 2 3 5   1 1 . 2 . . . . . 
-----------------   -----------------
index=43, Position(row: 6, col: 3), eval=1
-----------------   -----------------
7 2 3 4 6 9 1 5 8   . . . . . . . . . 
8 5 1 3 7 2 4 6 9   . . . . . . . . . 
6 4 9 5 1 8 7 2 3   . . . . . . . . . 
9 7 4 6 8 5 3 1 2   . . . . . . . . . 
1 3 2 9 4 7 5 8 6   . . . . . . . . . 
5 6 8 2 3 1 9 7 4   . . . . . . . . . 
3 8 5 . 2 4 6 9 1   . . . 1 . . . . . 
. . . . 5 3 8 4 .   1 2 1 2 . . . . 1 
. . 7 . 9 6 2 3 5   1 1 . 2 . . . . . 
-----------------   -----------------
index=44, Position(row: 7, col: 0), eval=1
-----------------   -----------------
7 2 3 4 6 9 1 5 8   . . . . . . . . . 
8 5 1 3 7 2 4 6 9   . . . . . . . . . 
6 4 9 5 1 8 7 2 3   . . . . . . . . . 
9 7 4 6 8 5 3 1 2   . . . . . . . . . 
1 3 2 9 4 7 5 8 6   . . . . . . . . . 
5 6 8 2 3 1 9 7 4   . . . . . . . . . 
3 8 5 7 2 4 6 9 1   . . . . . . . . . 
. . . . 5 3 8 4 .   1 2 1 1 . . . . 1 
. . 7 . 9 6 2 3 5   1 1 . 2 . . . . . 
-----------------   -----------------
index=45, Position(row: 7, col: 2), eval=1
-----------------   -----------------
7 2 3 4 6 9 1 5 8   . . . . . . . . . 
8 5 1 3 7 2 4 6 9   . . . . . . . . . 
6 4 9 5 1 8 7 2 3   . . . . . . . . . 
9 7 4 6 8 5 3 1 2   . . . . . . . . . 
1 3 2 9 4 7 5 8 6   . . . . . . . . . 
5 6 8 2 3 1 9 7 4   . . . . . . . . . 
3 8 5 7 2 4 6 9 1   . . . . . . . . . 
2 . . . 5 3 8 4 .   . 2 1 1 . . . . 1 
. . 7 . 9 6 2 3 5   1 1 . 2 . . . . . 
-----------------   -----------------
index=46, Position(row: 7, col: 3), eval=1
-----------------   -----------------
7 2 3 4 6 9 1 5 8   . . . . . . . . . 
8 5 1 3 7 2 4 6 9   . . . . . . . . . 
6 4 9 5 1 8 7 2 3   . . . . . . . . . 
9 7 4 6 8 5 3 1 2   . . . . . . . . . 
1 3 2 9 4 7 5 8 6   . . . . . . . . . 
5 6 8 2 3 1 9 7 4   . . . . . . . . . 
3 8 5 7 2 4 6 9 1   . . . . . . . . . 
2 . 6 . 5 3 8 4 .   . 2 . 1 . . . . 1 
. . 7 . 9 6 2 3 5   1 1 . 2 . . . . . 
-----------------   -----------------
index=47, Position(row: 7, col: 1), eval=1
-----------------   -----------------
7 2 3 4 6 9 1 5 8   . . . . . . . . . 
8 5 1 3 7 2 4 6 9   . . . . . . . . . 
6 4 9 5 1 8 7 2 3   . . . . . . . . . 
9 7 4 6 8 5 3 1 2   . . . . . . . . . 
1 3 2 9 4 7 5 8 6   . . . . . . . . . 
5 6 8 2 3 1 9 7 4   . . . . . . . . . 
3 8 5 7 2 4 6 9 1   . . . . . . . . . 
2 . 6 1 5 3 8 4 .   . 1 . . . . . . 1 
. . 7 . 9 6 2 3 5   1 1 . 1 . . . . . 
-----------------   -----------------
index=48, Position(row: 7, col: 8), eval=1
-----------------   -----------------
7 2 3 4 6 9 1 5 8   . . . . . . . . . 
8 5 1 3 7 2 4 6 9   . . . . . . . . . 
6 4 9 5 1 8 7 2 3   . . . . . . . . . 
9 7 4 6 8 5 3 1 2   . . . . . . . . . 
1 3 2 9 4 7 5 8 6   . . . . . . . . . 
5 6 8 2 3 1 9 7 4   . . . . . . . . . 
3 8 5 7 2 4 6 9 1   . . . . . . . . . 
2 9 6 1 5 3 8 4 .   . . . . . . . . 1 
. . 7 . 9 6 2 3 5   1 1 . 1 . . . . . 
-----------------   -----------------
index=49, Position(row: 8, col: 0), eval=1
-----------------   -----------------
7 2 3 4 6 9 1 5 8   . . . . . . . . . 
8 5 1 3 7 2 4 6 9   . . . . . . . . . 
6 4 9 5 1 8 7 2 3   . . . . . . . . . 
9 7 4 6 8 5 3 1 2   . . . . . . . . . 
1 3 2 9 4 7 5 8 6   . . . . . . . . . 
5 6 8 2 3 1 9 7 4   . . . . . . . . . 
3 8 5 7 2 4 6 9 1   . . . . . . . . . 
2 9 6 1 5 3 8 4 7   . . . . . . . . . 
. . 7 . 9 6 2 3 5   1 1 . 1 . . . . . 
-----------------   -----------------
index=50, Position(row: 8, col: 1), eval=1
-----------------   -----------------
7 2 3 4 6 9 1 5 8   . . . . . . . . . 
8 5 1 3 7 2 4 6 9   . . . . . . . . . 
6 4 9 5 1 8 7 2 3   . . . . . . . . . 
9 7 4 6 8 5 3 1 2   . . . . . . . . . 
1 3 2 9 4 7 5 8 6   . . . . . . . . . 
5 6 8 2 3 1 9 7 4   . . . . . . . . . 
3 8 5 7 2 4 6 9 1   . . . . . . . . . 
2 9 6 1 5 3 8 4 7   . . . . . . . . . 
4 . 7 . 9 6 2 3 5   . 1 . 1 . . . . . 
-----------------   -----------------
index=51, Position(row: 8, col: 3), eval=1
-----------------   -----------------
7 2 3 4 6 9 1 5 8   . . . . . . . . . 
8 5 1 3 7 2 4 6 9   . . . . . . . . . 
6 4 9 5 1 8 7 2 3   . . . . . . . . . 
9 7 4 6 8 5 3 1 2   . . . . . . . . . 
1 3 2 9 4 7 5 8 6   . . . . . . . . . 
5 6 8 2 3 1 9 7 4   . . . . . . . . . 
3 8 5 7 2 4 6 9 1   . . . . . . . . . 
2 9 6 1 5 3 8 4 7   . . . . . . . . . 
4 1 7 . 9 6 2 3 5   . . . 1 . . . . . 
-----------------   -----------------
done
-----------------   -----------------
7 2 3 4 6 9 1 5 8   . . . . . . . . . 
8 5 1 3 7 2 4 6 9   . . . . . . . . . 
6 4 9 5 1 8 7 2 3   . . . . . . . . . 
9 7 4 6 8 5 3 1 2   . . . . . . . . . 
1 3 2 9 4 7 5 8 6   . . . . . . . . . 
5 6 8 2 3 1 9 7 4   . . . . . . . . . 
3 8 5 7 2 4 6 9 1   . . . . . . . . . 
2 9 6 1 5 3 8 4 7   . . . . . . . . . 
4 1 7 8 9 6 2 3 5   . . . . . . . . . 
-----------------   -----------------

あなたは、IBM Swift Sandboxで、プログラムを自分で試すことができます。

struct Position {
    var row: Int = -1
    var col: Int = -1
}
struct Place {
    var position = Position()
    var val: Int = 0
}

class NumBoard {
    var table         = [[Int]]()
    var tableEval     = [[Int]](repeating:[Int](repeating:99, count:9), count:9)
    var emptyPlaces   = [Place]()
    var sortedPlaces  = [Place]()
    var putPositions  = [Position]()
    var position      = Position()
    var place         = Place()
    var nplaces : Int = 0
    
    init() {
        table = [[7,0,3, 0,0,0, 0,0,0],
                 [0,5,1, 3,7,0, 4,0,0],
                 [6,0,0, 0,1,0, 0,2,0],
                 [0,0,4, 6,0,0, 0,0,0],
                 [0,0,0, 0,0,7, 0,8,0],
                 [5,0,0, 2,3,1, 9,0,0],
                 [3,0,0, 0,2,4, 0,9,1],
                 [0,0,0, 0,0,0, 8,4,0],
                 [0,0,7, 0,9,0, 2,0,0]]

        for rr in 0...8 {
            for cc in 0...8 {
                if table[rr][cc] == 0 {
                    nplaces += 1
                    position.row = rr
                    position.col = cc
                    tableEval[rr][cc] = eval(position: position)
                }
            }
        }
        print(&quot;nplaces = \(nplaces) \n&quot;)
    }

    func myprint2(table: [[Int]], table2: [[Int]]) {
        print(&quot;-----------------   -----------------&quot;)
        for rr in 0...8 {
            for cc in 0...8 {
                if table[rr][cc] &gt;= 1 &amp;&amp; table[rr][cc] &lt;= 9 {
                    print(table[rr][cc], terminator: &quot; &quot;)
                } else {
                    print(&quot;.&quot;, terminator: &quot; &quot;)
                }
            }
            print(&quot;  &quot;, terminator: &quot;&quot;)
            for cc in 0...8 {
                if table2[rr][cc] &gt;= 1 &amp;&amp; table2[rr][cc] &lt;= 9 {
                    print(table2[rr][cc], terminator: &quot; &quot;)
                } else {
                    print(&quot;.&quot;, terminator: &quot; &quot;)
                }
            }
            print(&quot;&quot;)
        }
        print(&quot;-----------------   -----------------&quot;)
    }

    func eval(position: Position) -&gt; Int {
        var count = 0
        for num in 1...9 {
            if isSafeForNum(position: position, num: num) {
                count += 1
            }
        }
        return count
    }
    
    func isSafeForNum(position: Position, num: Int) -&gt; Bool {
        for rr in 0...8 {
            if num == table[rr][position.col] { return false }
        }
        for cc in 0...8 {
            if num == table[position.row][cc] { return false }
        }
        let rowBase = (position.row / 3) * 3
        let colBase = (position.col / 3) * 3
        for rr in 0...2 {
            for cc in 0...2 {
                if num == table[rowBase+rr][colBase+cc] { return false }
            }
        }
        return true
    }

    func findPosition() -&gt; Position {
        var minVal = 99
        var position = Position()
        for rr in 0...8 {
            for cc in 0...8 {
                if tableEval[rr][cc] &lt; minVal {
                    minVal = tableEval[rr][cc]
                    position.row = rr
                    position.col = cc
                }
            }
        }
        return position
    }
    
    func update(position: Position) {
        let row = position.row
        let col = position.col
        var pos = Position()
        tableEval[row][col] = 99
        for rr in 0...8 {
            if rr != row &amp;&amp; table[rr][col] == 0 {
                pos.row = rr
                pos.col = col
                tableEval[rr][col] = eval(position: pos)
            }
        }
        for cc in 0...8 {
            if cc != col &amp;&amp; table[row][cc] == 0 {
                pos.row = row
                pos.col = cc
                tableEval[row][cc] = eval(position: pos)
            }
        }
        let rowBase = (row / 3) * 3
        let colBase = (col / 3) * 3
        let rowOffset = row - rowBase
        let colOffset = col - colBase
        for rr in 0...2 {
            for cc in 0...2 {
                if rr != rowOffset &amp;&amp; cc != colOffset &amp;&amp; table[rr+rowBase][cc+colBase] == 0 {
                    pos.row = rr + rowBase
                    pos.col = cc + colBase
                    tableEval[pos.row][pos.col] = eval (position: pos)
                }
            }
        }
    }

    func dropNum(index: Int) {
        position = findPosition()
        print(&quot;index=\(index), \(position), eval=\(tableEval[position.row][position.col])&quot;)
        myprint2(table: table, table2: tableEval)
        for num in 1...9 {
            if isSafeForNum(position: position, num: num) {
                table[position.row][position.col] = num
                putPositions.append(position)
                update(position: position)
                if putPositions.count == nplaces { return }
                dropNum(index: index+1)
                if putPositions.count != nplaces {
                    position = putPositions[putPositions.count - 1]
                    table[position.row][position.col] = 0
                    putPositions.removeLast()
                    update(position: position)
                }
            }
        }
    }
    
    func play() {
        dropNum(index: 0)
        print(&quot;done&quot;)
        myprint2(table: table, table2: tableEval)
    }
}

// main program
let myboard = NumBoard()
myboard.play()

プログラムは多少冗長ですが、その分読み易くなっているつもりです。

Number Place(数独)とSwift 3 (4)

backtrack2

もちろん、私たちは次に埋めるべき場所をランダムにもしくは順番に選んだりはしません。手間を節約するためにもっと工夫をします。1つの方法は、可能性が最も少ない場所を選択することです。

    func findPosition(table: [[Int]]) -&gt; Position {
        var workPlaces   = [Place]()
        var sortedPlaces = [Place]()
        var position = Position()
        var place = Place()
            for cc in 0...8 {
                for rr in 0...8 {
                if table[rr][cc] == 0 {
                    position.row = rr
                    position.col = cc
                    place.position = position
                    place.val    = eval(position: position, table: table)
                    workPlaces.append(place)
                }
            }
        }
        sortedPlaces = workPlaces.sorted( by: { $0.val &lt; $1.val } )
        place = sortedPlaces[0]
        position = place.position
        return position
    }

実際には、毎回全ての場所を再評価する必要はありません。直前の動きで影響を受ける場所だけを考慮すれば良いのです。

backtrac3

再帰的関数dropNumは、同じ問題に対して、今や139回しか呼ばれません。

struct Position {
    var row: Int = -1
    var col: Int = -1
}
struct Place {
    var position = Position()
    var val: Int = 0
}

class NumBoard {
    var table         = [[Int]]()
    var emptyPlaces   = [Place]()
    var sortedPlaces  = [Place]()
    var putPositions  = [Position]()
    var position      = Position()
    var place         = Place()
    var nplaces : Int = 0
    
    init() {
        table = [[7,0,3, 0,0,0, 0,0,0],
                 [0,5,1, 3,7,0, 4,0,0],
                 [6,0,0, 0,1,0, 0,2,0],
                 [0,0,4, 6,0,0, 0,0,0],
                 [0,0,0, 0,0,7, 0,8,0],
                 [5,0,0, 2,3,1, 9,0,0],
                 [3,0,0, 0,2,4, 0,9,1],
                 [0,0,0, 0,0,0, 8,4,0],
                 [0,0,7, 0,9,0, 2,0,0]]

        for rr in 0...8 {
            for cc in 0...8 {
                if table[rr][cc] == 0 {
                    nplaces += 1
                }
            }
        }
        myprint(table: table)
        print(&quot;nplaces = \(nplaces)&quot;)
    }
    
    func myprint(table: [[Int]]) {
        print(&quot;-----------------&quot;)
        for rr in 0...8 {
            for cc in 0...8 {
                if table[rr][cc] == 0 {
                    print(&quot;.&quot;, terminator: &quot; &quot;)
                } else {
                    print(table[rr][cc], terminator: &quot; &quot;)
                }
            }
            print(&quot;&quot;)
        }
        print(&quot;-----------------&quot;)
    }
    
    func eval(position: Position, table: [[Int]]) -&gt; Int {
        var count = 0
        for num in 1...9 {
            if isSafeForNum(position: position, num: num) {
                count += 1
            }
        }
        return count
    }
    
    func isSafeForNum(position: Position, num: Int) -&gt; Bool {
        for rr in 0...8 {
            if num == table[rr][position.col] { return false }
        }
        for cc in 0...8 {
            if num == table[position.row][cc] { return false }
        }
        let rowBase = (position.row / 3) * 3
        let colBase = (position.col / 3) * 3
        for rr in 0...2 {
            for cc in 0...2 {
                if num == table[rowBase+rr][colBase+cc] { return false }
            }
        }
        return true
    }

    func findPosition(table: [[Int]]) -&gt; Position {
        var workPlaces   = [Place]()
        var sortedPlaces = [Place]()
        var position = Position()
        var place = Place()
            for cc in 0...8 {
                for rr in 0...8 {
                if table[rr][cc] == 0 {
                    position.row = rr
                    position.col = cc
                    place.position = position
                    place.val    = eval(position: position, table: table)
                    workPlaces.append(place)
                }
            }
        }
        sortedPlaces = workPlaces.sorted( by: { $0.val &lt; $1.val } )
        place = sortedPlaces[0]
        position = place.position
        return position
    }
    
    func dropNum(index: Int) {
        position = findPosition(table: table)
        for num in 1...9 {
            if isSafeForNum(position: position, num: num) {
                table[position.row][position.col] = num
                putPositions.append(position)
                if putPositions.count == nplaces { return }
                dropNum(index: index+1)
                if putPositions.count != nplaces {
                    position = putPositions[putPositions.count - 1]
                    table[position.row][position.col] = 0
                    putPositions.removeLast()
                }
            }
        }
    }
    
    func play() {
        dropNum(index: 0)
        myprint(table: table)
    }
}

// main program
let myboard = NumBoard()
myboard.play()

Number Place(数独)とSwift 3 (3)

backtrack

グラフは、再帰的関数dropNum(index: Int)が呼ばれたときのindexの値を示しています。初期状態では、52個の埋めるべき空白があることに注意して下さい。

    func dropNum(index: Int) {
        var place = emptyPlaces[index]
        for num in 1...9 {
            if isSafeForNum(place: place, num: num) {
                table[place.row][place.col] = num
                putCount += 1
                if putCount == nplaces { return }
                dropNum(index: index+1)
                if putCount != nplaces {
                    place = emptyPlaces[index]
                    table[place.row][place.col] = 0
                    putCount -= 1
                }
            }
        }
    }

あなたは以下の95行をコピー・ペーストして、IBM Swift Sandboxというサイトで、このプログラムを試してみることができます。

backtrack2

struct Place {
    var row: Int = -1
    var col: Int = -1
}

class NumBoard {
    var table       = [[Int]]()
    var emptyPlaces = [Place]()
    var place       = Place()
    let nplaces : Int
    var putCount: Int

    init() {
        table = [[7,0,3, 0,0,0, 0,0,0],
                 [0,5,1, 3,7,0, 4,0,0],
                 [6,0,0, 0,1,0, 0,2,0],
                 [0,0,4, 6,0,0, 0,0,0],
                 [0,0,0, 0,0,7, 0,8,0],
                 [5,0,0, 2,3,1, 9,0,0],
                 [3,0,0, 0,2,4, 0,9,1],
                 [0,0,0, 0,0,0, 8,4,0],
                 [0,0,7, 0,9,0, 2,0,0]]
        for rr in 0...8 {
            for cc in 0...8 {
                if table[rr][cc] == 0 {
                    place.row = rr
                    place.col = cc
                    emptyPlaces.append(place)
                 }
            }
        }
        putCount = 0
        nplaces  = emptyPlaces.count
        myprint(table: table)
        print(&quot;nplaces = \(nplaces)&quot;)
    }

    func myprint(table: [[Int]]) {
        print(&quot;-----------------&quot;)
        for rr in 0...8 {
            for cc in 0...8 {
                if table[rr][cc] == 0 {
                    print(&quot;.&quot;, terminator: &quot; &quot;)
                } else {
                    print(table[rr][cc], terminator: &quot; &quot;)
                }
            }
            print(&quot;&quot;)
        }
        print(&quot;-----------------&quot;)
    }
    
    func isSafeForNum(place: Place, num: Int) -&gt; Bool {
        for rr in 0...8 {
            if num == table[rr][place.col] { return false }
        }
        for cc in 0...8 {
            if num == table[place.row][cc] { return false }
        }
        let rowBase = (place.row / 3) * 3
        let colBase = (place.col / 3) * 3
        for rr in 0...2 {
            for cc in 0...2 {
                if num == table[rowBase+rr][colBase+cc] { return false }
            }
        }
        return true
    }

    func dropNum(index: Int) {
        var place = emptyPlaces[index]
        for num in 1...9 {
            if isSafeForNum(place: place, num: num) {
                table[place.row][place.col] = num
                putCount += 1
                if putCount == nplaces { return }
                dropNum(index: index+1)
                if putCount != nplaces {
                    place = emptyPlaces[index]
                    table[place.row][place.col] = 0
                    putCount -= 1
                }
            }
        }
    }

    func play() {
        dropNum(index: 0)
        myprint(table: table)
    }
}

// main program
let myboard = NumBoard()
myboard.play()

Number Place(数独)とSwift 3 (2)

numberplace2

プログラムは常にシンプルであるべきなのですが、まだその段階に達していません。

struct Place {
    var row: Int = -1
    var col: Int = -1
}

class NumBoard {
    var table       = [[Int]]()
    var emptyPlaces = [Place]()
    var place       = Place()
    let nplaces : Int
    var putCount: Int

    init() {
        table = [[7,0,3, 0,0,0, 0,0,0],
                 [0,5,1, 3,7,0, 4,0,0],
                 [6,0,0, 0,1,0, 0,2,0],
                 [0,0,4, 6,0,0, 0,0,0],
                 [0,0,0, 0,0,7, 0,8,0],
                 [5,0,0, 2,3,1, 9,0,0],
                 [3,0,0, 0,2,4, 0,9,1],
                 [0,0,0, 0,0,0, 8,4,0],
                 [0,0,7, 0,9,0, 2,0,0]]

        for rr in 0...8 {
            for cc in 0...8 {
                if table[rr][cc] == 0 {
                    place.row = rr
                    place.col = cc
                    emptyPlaces.append(place)
                 }
            }
        }
        putCount = 0
        nplaces = emptyPlaces.count
        myprint(table: table)
    }

    func myprint(table: [[Int]]) {
        print(&quot;-----------------&quot;)
        for rr in 0...8 {
            for cc in 0...8 {
                print(table[rr][cc], terminator: &quot; &quot;)
            }
            print(&quot;&quot;)
        }
        print(&quot;-----------------&quot;)
    }
    
    func isSafeForNum(place: Place, num: Int) -&gt; Bool {
        for rr in 0...8 {
            if num == table[rr][place.col] { return false }
        }
        for cc in 0...8 {
            if num == table[place.row][cc] { return false }
        }
        let rowBase = (place.row / 3) * 3
        let colBase = (place.col / 3) * 3
        for rr in 0...2 {
            for cc in 0...2 {
                if num == table[rowBase+rr][colBase+cc] { return false }
            }
        }
        return true
    }

    func dropNum(index: Int) {
        /* could be better */
    }

    func play() {
        dropNum(index: 0)
        myprint(table: table)
    }
}

let myboard = NumBoard()
myboard.play()

ところで、あなたは以下のような行が気に入らないかもしれません。

let rowBase = (place.row / 3) * 3
let colBase = (place.col / 3) * 3
  for rr in 0...2 {
    for cc in 0...2 {
      if num == table[rowBase+rr][colBase+cc] { return false }
    }
  }

私の意見では、この方がずっと目に優しいと思います。3blockSizeに、そして、2blockSize-1に置き換えるよりは。

日は上り、日は沈む

Swift 3で、Number Placeを解くプログラムを書こうとしていたら、音楽が聞こえてきて私の集中力を奪いました。

Sunrise, sunset
Sunrise, sunset
Swiftly fly the years
One season following another
Laden with happiness and tears

swift3

プログラムは未だ完成していません。

Gmsh

gmsh

Gmshは、3次元の有限要素メッシュジェネレータです。

Gmshのフォーマットから、ElmerSolverのメッシュフォーマットに変換するのにElmerGridが用いられます。

% ElmerGrid 14 2 mesh.msh -autoclean

elmer4

solver input fileの一部です:

Equation 1
  Name = &quot;Equation 1&quot;
  Active Solvers(1) = 1
End

Material 1
  Name = &quot;Aluminium (generic)&quot;
  Heat expansion Coefficient = 23.1e-6
  Heat Conductivity = 237.0
  Sound speed = 5000.0
  Heat Capacity = 897.0
  Mesh Poisson ratio = 0.35
  Density = 2700.0
  Poisson ratio = 0.35
  Youngs modulus = 70.0e9
End

Boundary Condition 1
  Target Boundaries(1) = 1 
  Name = &quot;BoundaryCondition 1&quot;
  Temperature = 100.0
End

Boundary Condition 2
  Target Boundaries(1) = 2 
  Name = &quot;BoundaryCondition 2&quot;
  Temperature = 200.0
End

Boundary Condition 3
  Target Boundaries(1) = 3 
  Name = &quot;BoundaryCondition 3&quot;
  Temperature = 300.0
End