LeetCode: N-Queens II [051]

【题目】

Follow up for N-Queens problem.

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

【题意】

解N皇后问题,N-Queens要求返回所有的解,而本题只需要返回可行解的数目

【思路】

DFS,参考N-Queens

【代码】

class Solution {
public:

    bool isValid(vector<string>matrix, int x, int y, int n){
        //所在列
        int i=0, j=y;
        for(;i<n;i++){
            if(i!=x&&matrix[i][j]=='Q')return false;
        }
        //指向左上角的斜线
        i=x-1;
        j=y-1;
        while(i>=0&&j>=0){
            if(matrix[i][j]=='Q')return false;
            i--;j--;
        }
        //指向右上角的斜线
        i=x-1;
        j=y+1;
        while(i>=0&&j<n){
            if(matrix[i][j]=='Q')return false;
            i--;j++;
        }
        return true;
    }
    void dfs(int&result, vector<string>matrix, int lineNo, int n){
        if(lineNo>n){
            result++;
            return;
        }
        for(int i=0; i<n; i++){
            matrix[lineNo-1][i]='Q';
            if(isValid(matrix, lineNo-1, i, n)){
                dfs(result, matrix, lineNo+1, n);
            }
            matrix[lineNo-1][i]='.';
        }
    }
    int totalNQueens(int n) {
        int result=0;
        vector<string>matrix(n, string(n,'.'));
        dfs(result, matrix, 1, n);
        return result;
    }
};

LeetCode: N-Queens II [051],布布扣,bubuko.com

时间: 2024-10-10 20:53:47

LeetCode: N-Queens II [051]的相关文章

LeetCode:N-Queens I II(n皇后问题)

N-Queens The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a distinct board configur

LeetCode --- 90. Subsets II

题目链接:Subsets II Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,2]

【leetcode】N-queens II

问题: 返回N皇后问题解的个数. 分析: 详见 N-queens 实现: bool nextPermutation(vector<int> &num) { int i = num.size() - 1; while (i >= 1) { if(num[i] > num[i - 1]) { --i; int ii = num.size() - 1; while (ii > i && num[ii] <= num[i]) --ii; if(ii &g

leetcode: Subsets &amp; Subsets II

SubsetsGiven a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example,If S = [1,2,3], a solution is: [ [3], [1], [2], [1,2

[LeetCode] Jump Game II(贪婪算法)

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 minimum number of jumps

leetCode: Single Number II [137]

[题目] 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 complexity. Could you implement it without using extra memory? [题意] 给定一个整数以外,其中除了一个整数只出现一次以外,其他

LeetCode: Jump Game II [044]

Perface 如果让你实现这个页面和一些操作的,比如点击1.2.3等就在那个input text中显示,还有删除功能,拨打我们先不要管它,只是模拟而已.要是我刚开始做的话,我会这样做: 用css.HTML布局那个界面 用javascript的事件委托监听那个按钮的父节点的点击事件 但是如果我想用面向对象的思想做呢?我是用Ext做的,所以我想说的是它帮我封装了很多.可能一些没用过Ext的人不太了解我下面贴的代码,但是我会尽量解释清楚的! Description ContactTelPanel =

LeetCode: Combination Sum II [039]

[题目] Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will be

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