leetcode#52 N queensⅡ

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

上图为 8 皇后问题的一种解法。

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

示例:

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

 ["..Q.",  // 解法 2
  "Q...",
  "...Q",
  ".Q.."]
]
class Solution {
public:
    int totalNQueens(int n)
    {
        return queen(0,0,0,0,0,n);
    }
    //l,r,d是左右竖线的控制范围
    int queen(int answer, int deep, int l, int d,int r,int n)
    {
        if (deep == n)//deep当前深度==皇后数,我们按行填皇后,所以deep其实还代表行数0-7.8说明已填满n皇后
            return answer + 1;
        for (int i = 0; i < n; ++i)//再当前deep依次放入皇后
        {
            if (((1 << i) & l) || ((1 << i)&d) || ((1 << i)&r))
                continue;
            answer = queen(answer, deep + 1, (l << 1) | (1 << (i + 1)), d | (1 << i), (r >> 1) | (1 <<( i - 1)),n);
        }
        return answer;
    }
};

原文地址:https://www.cnblogs.com/lsaejn/p/9755584.html

时间: 2024-10-20 04:28:59

leetcode#52 N queensⅡ的相关文章

8.18 [LeetCode 52] N-Queens II

[LeetCode 52] N-Queens II | COMMENTS Question link Follow up for N-Queens problem. Now, instead outputting board configurations, return the total number of distinct solutions. Stats Frequency 3 Difficulty 4 Adjusted Difficulty 2 Time to use ——– Ratin

Leetcode #52 N-Queens II

题目链接:https://leetcode.com/problems/n-queens-ii/ N皇后问题的位运算实现. 1 class Solution 2 { 3 public: 4 int totalNQueens(int n) 5 { 6 upperLimit = (1 << n) - 1; //如果n为8,则upperLimit为"11111111". 7 placeQueen(0, 0, 0); 8 9 return count; 10 } 11 12 void

leetCode 52.N-Queens II (n皇后问题II) 解题思路和方法

N-Queens II Follow up for N-Queens problem. Now, instead outputting board configurations, return the total number of distinct solutions. 思路:解决了上题,这题也就迎刃而解,或者说这题要不上题还要简单一些. 具体代码如下: public class Solution { int count = 0; public int totalNQueens(int n)

[leetcode] 52. N皇后 II

52. N皇后 II 跟上个题一模一样,现在只需输出个数即可 class Solution { public int totalNQueens(int n) { boolean[] row = new boolean[n]; boolean[] h = new boolean[2 * n]; boolean[] r = new boolean[2 * n]; List<List<String>> ans = new ArrayList<>(); dfs(n, row,

Leetcode 52 N-Queens II 回溯搜索

对于N-Queens的每种情况,回答出每种情况的N-Queens的排列数. l,r和c是每种类型的格子是否有棋子. l判断的是这样的对角线的格子                   r判断的是这样的对角线的格子              c   判断的是这样的竖线格子                                                               枚举每行放一颗棋子,判断是否与前面冲突,如果不冲突一直到底就让答案+1 1 class Solution

[leetcode 52] Next Permutation Report

Question: Given a list of integers, which denote a permutation. Find the next permutation in ascending order. Example For [1,3,2,3], the next permutation is [1,3,3,2] For [4,3,2,1], the next permutation is [1,2,3,4] Note The list may contains duplica

leetCode(52):Add Binary

Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 题目并不复杂,以下是我的程序,后面是别人的程序,瞬间low了好大一截! 冗长!!!!!!! class Solution { public: string addBinary(string a, string b) { stri

[C++]LeetCode: 52 Climbing Stairs

题目: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? 思路解析: 无法一下子判断是 Fibonacci number,于是开始分析问题. the number of solutions for N steps st

leetcode 52 N皇后问题 II

51的简化版,省去根据排列话棋盘的工作,直接计数,代码: class Solution { public: int totalNQueens(int n) { int res=0; vector<int> pos(n,-1); dfs(n,0,pos,res); return res; } void dfs(int n,int row,vector<int>& pos,int &res){ if(row==n){ res++;return; } for(int co