正直に言って、一々全てに反応するのはとっくに止めた

macain

Republican Sen. John McCain at time 00:40.


“A long time ago, honestly, I’ve stopped reacting to everything that the President has stated and try to work on the issues and the people who he is going to surround himself with, who I am very pleased with as far as national security is concerned.”

http://edition.cnn.com/2017/01/24/politics/mccain-no-evidence-of-illegal-voting-cnntv/index.html

代替的な事実

conway
http://www.nbcnews.com/meet-the-press/video/conway-press-secretary-gave-alternative-facts-860142147643

Washington (CNN) — White House press secretary Sean Spicer’s false claims about the size of the crowd at President Donald Trump’s inauguration were “alternative facts,” a top Trump aide said Sunday.

atlantic
https://www.theatlantic.com/politics/archive/2017/01/inauguration-crowd-size/514058/

まあ、予想されたことではありますが。。

COCA: Vibroplex

vibroplex

私たちにとって馴染み深い単語や語句でCOCAを試してみましょう。

vibroplex2

検索結果は3つしかありませんが、そのうちの2つは非常に興味深いです。

I didn’t like this guy because he used a Vibroplex semi-automatic speed key and sent his messages too fast. He had a lousy ” fist. ” His dashes were too short — you could easily take them for dots — and his spacing between word groups was erratic. He also bragged too much about his rig. A thousand watts generated by a pair of big Eimac tetrodes in the final amplifier. Anybody could do that if they had the money.

He told me that since his stroke, he’d felt as if he were trapped in the radio room with the door locked from the outside. The receiver was still working, but the transmitter was broken. He couldn’t talk back to anyone. He’d been tapping away with a spoon for days, even though he’d given up hope of being ” rescued. ” Paralysis of his right side had prevented him from writing any messages. # I brought to the hospital an old Vibroplex Sidewinder telegraph key, which was Harry’s favorite for sending Morse code. Harry had spent a good part of his life on oceangoing freighters as a radiotelegraph operator. He told me a story of something that had happened at sea several days before the attack on Pearl Harbor.

COCA

coca1

COCA, the Corpus of Contemporary American English,は無料で得られる最大の英語のコーパスです。

coca2

あなたの探したいフレーズを入力すると、直ちに結果が得られます。

coca3

UIKitとSpriteKit(3)

素敵なアニメーションはまだありませんが、プレイをすることが可能です。

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard let touch = touches.first else { return }
        let location = touch.location(in: cookiesLayer)
        let (success, col, row) = convertPoint(location)
        if success {
            if level.getTableAt(col: col, row: row) == 0 { // an empty place is selected, so highlight it.
                rowSelected = row
                colSelected = col
                if selectedSprite != nil {
                    selectedSprite.run(SKAction.removeFromParent())
                }
                selectedSprite = SKSpriteNode(imageNamed: "Undef-Highlighted")
                selectedSprite.size = CGSize(width: TileWidth, height: TileHeight)
                selectedSprite.position = pointFor(col: colSelected, row: rowSelected)
                selectedLayer.addChild(selectedSprite)
                showCandidate(col: colSelected, row: rowSelected)
            } else if row == 10 { // number selected
                numberSelected = true
                numberToMove = col + 1
                if level.isSafeForNum(col: colSelected, row: rowSelected, num: numberToMove) {
                    level.setTableAt(col: colSelected, row: rowSelected, val: numberToMove)
                    let sprite = SKSpriteNode(imageNamed: spriteNames[numberToMove]+"-Highlighted")
                    sprite.size = CGSize(width: TileWidth, height: TileHeight)
                    sprite.position = pointFor(col: colSelected, row: rowSelected)
                    userLayer.addChild(sprite)
                    level.setTableAt(col: colSelected, row: rowSelected, val: -numberToMove)
                    if selectedSprite != nil {
                        selectedSprite.run(SKAction.removeFromParent())
                    }
                }
            } else if level.getTableAt(col: col, row: row ) < 0 { // cancel move
                rowSelected = row
                colSelected = col
                level.setTableAt(col: col, row: row, val: 0)
                let location = touch.location(in: self)
                let sprite: SKNode? = atPoint(location)
                sprite?.run(SKAction.removeFromParent())
                if selectedSprite != nil {
                    selectedSprite.run(SKAction.removeFromParent())
                }
                selectedSprite = SKSpriteNode(imageNamed: "Undef-Highlighted")
                selectedSprite.size = CGSize(width: TileWidth, height: TileHeight)
                selectedSprite.position = pointFor(col: col, row: row)
                selectedLayer.addChild(selectedSprite)
                showCandidate(col: col, row: row)
            }
        }
    }

プログラムは、非常に美しくありません。

UIKitとSpriteKit(2)

空白の場所をクリックすると、そこに入れることのできる数字が表示されます。

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard let touch = touches.first else { return }
        let location = touch.location(in: cookiesLayer)
        let (success, column, row) = convertPoint(location)
        if success && level.tableAt(column: column, row: row) == 0 {
            if row_touchedb4 >= 0 { // not the first time, recover to the original state
                let num = level.tableAt(column: column_touchedb4, row: row_touchedb4)
                print(row_touchedb4, column_touchedb4, num)
                let sprite = SKSpriteNode(imageNamed: spriteNames[num])
                sprite.size = CGSize(width: TileWidth, height: TileHeight)
                sprite.position = pointFor(column: column_touchedb4, row: row_touchedb4)
                cookiesLayer.addChild(sprite)
                selectionSprite.run(SKAction.removeFromParent())
            }
            column_touchedb4 = column
            row_touchedb4    = row
            let location = touch.location(in: self)
            let sprite: SKNode? = atPoint(location)
            sprite?.run(SKAction.removeFromParent())
            selectionSprite = SKSpriteNode(imageNamed: "Undef-Highlighted")
            selectionSprite.size = CGSize(width: TileWidth, height: TileHeight)
            selectionSprite.position = pointFor(column: column, row: row)
            cookiesLayer.addChild(selectionSprite)
            showCandidate(column: column, row: row)
 }

プログラムは、まだまだ改良の余地があります。

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