【一天一道LeetCode】#52. N-Queens II

一天一道LeetCode系列

(一)题目

Follow up for N-Queens problem.

Now, instead outputting board configurations, return the total number of distinct solutions.

(二)解题

具体思路参考【一天一道LeetCode】#51. N-Queens

/*
与N-Queens不同的事,这题只要求输出摆放方式的个数,因此对程序只需要做小的改动
*/
class Solution {
public:
    int count = 0;//记录次数
    vector<pair<int, int>> queens;//存放已摆放的皇后的坐标值
    int totalNQueens(int n) {
        int *a = new int[n];//确保每一列只有一个皇后
        memset(a,0,n*sizeof(int));
        backtrc(a, 0, n);
        return count;
    }
    bool isValid(vector<pair<int,int>> queens , int row,int col)//
    {
        if (queens.empty()) return true;
        for (int i = 0; i < queens.size();i++)
        {
            if (abs(row- queens[i].first) == abs(col-queens[i].second))
            {
                return false;
            }
        }
        return true;
    }
    void backtrc(int a[], int row, int n)//row确保每一行只有一个皇后
    {
        if (row == n)//如果摆放完n行,则退出
        {
            count++;//次数加1
            return;
        }
        for (int i = 0; i < n; i++)
        {
            if (a[i] == 0&&isValid(queens, row, i))//保证了同一行,同一列,同一对角线只有一个Q
            {
                a[i] = 1;
                queens.push_back(pair<int, int>(row, i));
                backtrc(a, row + 1, n);
                //回溯
                a[i] = 0;
                queens.pop_back();
            }
        }
    }
};
时间: 2024-11-06 09:33:41

【一天一道LeetCode】#52. N-Queens II的相关文章

[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Ⅱ

n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回 n 皇后不同的解决方案的数量. 示例: 输入: 4 输出: 2 解释: 4 皇后问题存在如下两个不同的解法. [  [".Q..",  // 解法 1   "...Q",   "Q...",   "..Q."],  ["..Q.",  // 解法 2  

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】#219. Contains Duplicate II

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j]and the differen

【一天一道LeetCode】#82. Remove Duplicates from Sorted List II

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example, Given 1->2->3-&

【一天一道LeetCode】#63. Unique Paths II

一天一道LeetCode (一)题目 Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one ob

【一天一道LeetCode】#113. Path Sum II

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22,

【一天一道LeetCode】#137. Single Number II

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complex

【一天一道LeetCode】#45. Jump Game II

一天一道LeetCode系列 (一)题目 Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the mi