首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swift BackTracking N皇后

Swift BackTracking N皇后
EN

Stack Overflow用户
提问于 2019-06-24 11:56:34
回答 1查看 746关注 0票数 1

我在努力解决N皇后的问题。您可以在https://leetcode.com/problems/n-queens/中找到问题。

对于回溯,我了解到我们可以用三个键来解决问题:

  1. 做出选择
  2. 约束条件
  3. 目标

所以我想出了一个解决方案:

代码语言:javascript
复制
func solveNQueens(_ n: Int) -> [[String]] {

    typealias ChessBoard = [[Int]]
    var result = Set<ChessBoard>()


    func getIndexsOfDiagonal(row:Int,column:Int) -> [(row:Int,col:Int)] {
        var indexs = [(Int,Int)]()

        var rowIndex = row
        var colIndex = column

        while rowIndex < n && colIndex < n {
            indexs.append((rowIndex,colIndex))
            rowIndex += 1
            colIndex += 1
        }

        rowIndex = row
        colIndex = column

        while rowIndex >= 0 && colIndex >= 0  {
            indexs.append((rowIndex,colIndex))
            rowIndex -= 1
            colIndex -= 1
        }

        rowIndex = row
        colIndex = column

        while rowIndex >= 0 && colIndex < n {
            indexs.append((rowIndex,colIndex))
            rowIndex -= 1
            colIndex += 1
        }

        rowIndex = row
        colIndex = column

        while rowIndex < n && colIndex >= 0 {
            indexs.append((rowIndex,colIndex))
            rowIndex += 1
            colIndex -= 1
        }
        return indexs
    }

    func placeQuees(chessboard:ChessBoard,row:Int,column:Int) ->ChessBoard {
        var newChessBorad = chessboard
        //set row
        for index in 0..<n {
            newChessBorad[row][index] = -1
        }
        //set column
        for index in 0..<n {
            newChessBorad[index][column] = -1
        }
        //set diagonal
        for index in getIndexsOfDiagonal(row:row,column:column) {
            newChessBorad[index.row][index.col] = -1
        }

        newChessBorad[row][column] = 1

        return newChessBorad
    }

    func solve(chessboard:ChessBoard, queens: Int) {

        if queens == 0 {
            //Goal
            result.insert(chessboard)
        }

        for row in 0..<n {
            for col in 0..<n {
                //Choices
                if chessboard[row][col] == 0 {
                    //Constraints
                    let new = placeQuees(chessboard: chessboard, row: row, column: col)
                    solve(chessboard: new, queens: queens - 1)
                }
            }
        }
    }

    solve(chessboard: Array(repeating: Array(repeating: 0, count: n), count: n), queens: n)


    return result.map {
        //chessboard
        $0.map {
            //row to string
            $0.reduce("") { string,value in
                if value == 1 {
                    return string + "Q"
                } else {
                    return string + "."
                }
            }
        }
    }
}

但时间有限。所以我想知道我的解决方案是否使用回溯?出了什么问题,如何改进解决方案,如何解决回溯问题?什么定义了回溯?

非常感谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-24 22:01:50

你的解决方案是回溯。当它无法再找到一个可用的空间(chessboard[row][col] == 0)来放置皇后时,它会回溯。因为它正在查找所有可能的解决方案,所以它在找到解决方案并将其插入result后也会返回。

您的解决方案只是在每次调用solve时尝试过多的试用位置。请注意,任何给定的行都只能有一个皇后。正因为如此,solve可以更有效地工作,只需在每次调用solve时将皇后放在一行上即可。在第一次调用solve时,尝试将皇后放在row 0上。然后,您将只考虑n可能的位置,而不是n * n。在第二次呼叫solve时,尝试将皇后放在row 1上。当前的row可以计算为n减去剩余的皇后数或n - queens

通过这个小小的修改,您的代码在提交到LeetCode时运行得更快,并且成功地通过了:

代码语言:javascript
复制
func solve(chessboard:ChessBoard, queens: Int) {

    if queens == 0 {
        //Goal
        result.insert(chessboard)
    }
    else {
        let row = n - queens
        for col in 0..<n {
            //Choices
            if chessboard[row][col] == 0 {
                //Constraints
                let new = placeQuees(chessboard: chessboard, row: row, column: col)
                solve(chessboard: new, queens: queens - 1)
            }
        }
    }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56736039

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档