leetcode刷题笔记五十一 与五十二 N皇后问题

leetcode刷题笔记五十一 与五十二 N皇后问题

源地址:51. N皇后

问题描述:

n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。

给定一个整数 n,返回所有不同的 n 皇后问题的解决方案。

每一种解法包含一个明确的 n 皇后问题的棋子放置方案,该方案中 'Q' 和 '.' 分别代表了皇后和空位。

示例:

输入: 4
输出: [
[".Q..", // 解法 1
"...Q",
"Q...",
"..Q."],

["..Q.", // 解法 2
"Q...",
"...Q",
".Q.."]
]
解释: 4 皇后问题存在两个不同的解法。

提示:

皇后,是国际象棋中的棋子,意味着国王的妻子。皇后只做一件事,那就是“吃子”。当她遇见可以吃的棋子时,就迅速冲上去吃掉棋子。当然,她横、竖、斜都可走一到七步,可进可退。(引用自 百度百科 - 皇后 )。

代码补充:

//五十一题与五十二题基本一致,主要是N皇后问题
//五十一题
import scala.collection.mutable
object Solution {
    //主函数 入参n:皇后问题规模 
    def solveNQueens(n: Int): List[List[String]] = {
        val res = new mutable.ListBuffer[List[String]]

        //回溯过程
        def placeQueen(placement:Array[Int], row:Int):Unit = {
            //在最后一行成功放入后,当前placement成功放入res
            if (row == n) res += transform(placement)
            else{
                //在当前行的各个位置尝试放入皇后
                //当前行放入失败,回退到上一行放入位置的后一位
                for(col <- 0 to n-1){
                    //判断皇后是否能够成功放入
                    if (isVaild(placement, row, col)) {
                       	//在placement中标注当前行皇后的col值 
                        placement(row) = col
                        //当前行成功放入后,继续放入下一行
                        //当前行放入失败后,继续放本行的后一位
                        placeQueen(placement, row+1)
                    }
                }
            }
        }

        //isVaild
        def isVaild(placement:Array[Int], row:Int, col:Int):Boolean = {
            //判断之前放入的皇后与准备放入的皇后是否冲突
            for(i <- 0 to row-1){
                val j = placement(i)
                if(j == col || math.abs(j - col) == row - i) return false
            }
            return true
        }

        def transform(placement:Array[Int]):List[String] = {
            val ans = mutable.ListBuffer[String]()
            val rowPlacement = new StringBuilder()
            for (i <- 0 to n-1){
                rowPlacement.clear()
                for(j <- 0 to n-1){
                    if ( j == placement(i)) rowPlacement += 'Q'
                    else rowPlacement += '.'
                }
                ans += rowPlacement.toString()
            }
            return ans.toList
        }

        placeQueen(new Array[Int](n), 0)
        return res.toList
    }
}

//五十二题
object Solution {
    def totalNQueens(n: Int): Int = {
        var ans = 0

        def placeQueen(placement:Array[Int], row:Int) : Unit = {
            if (row == n) ans +=1
            for(col <- 0 to n-1){
                if(isVaild(placement, row, col)){
                    placement(row) = col
                    placeQueen(placement, row+1)
                }
            }
        }

        def isVaild(placement:Array[Int], row:Int, col:Int) : Boolean = {
            for (i <- 0 to row-1){
                val j = placement(i)
                if (j == col || math.abs(j -col) == row - i) return false
            }
            return true
        }

        placeQueen(new Array[Int](n), 0)
        return ans
    }
}
原文地址:https://www.cnblogs.com/ganshuoos/p/13275393.html