LeetCode: N-Queens [050]

【题目】

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 configuration of the n-queens‘ placement, where ‘Q‘ and ‘.‘ both
indicate a queen and an empty space respectively.

For example,

There exist two distinct solutions to the 4-queens puzzle:

[
 [".Q..",  // Solution 1
  "...Q",
  "Q...",
  "..Q."],

 ["..Q.",  // Solution 2
  "Q...",
  "...Q",
  ".Q.."]
]

【题意】

解N皇后问题。在nXn的矩阵上放置n个皇后,每行放一个,其所在列和两个斜线上没有其他皇后放置。

该题要求得到所有可行解。‘Q’表示皇后,‘.‘表示空位置

【思路】

和解Sudoku Solver的方法相同,用DFS求解

【代码】

class Solution {
public:
    bool isValid(vector<string>&matrix, int x, int y, int n){
        //考虑纵向
        int i=0;
        int 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(vector<vector<string> >&result, vector<string>matrix, int lineNo, int n){
        //matrix--已经完成部分皇后放置的二维矩阵
        //lineNo--当前正在处理的行的行号,从1开始
        //n--总行数,也就是皇后数
        if(lineNo>n){
            result.push_back(matrix);
            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]='.';
        }
    }
    vector<vector<string> > solveNQueens(int n) {
        vector<vector<string> >result;
        vector<string>matrix(n, string(n, '.'));
        dfs(result, matrix, 1, n);
        return result;
    }
};

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

时间: 2024-12-30 01:45:55

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

[Leetcode] n queens n皇后问题

The n-queens puzzle is the problem of placing n queens on an n×nchessboard 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 configuration of t

LeetCode 050 Pow(x, n)

题目要求:Pow(x, n) Implement pow(x, n). 代码如下: class Solution { public: //采用二分法 //时间复杂度 O(logn),空间复杂度 O(1) double pow(double x, int n) { //要考虑n < 0的情况! if(n < 0) return 1.0 / power(x, -n); else return power(x, n); } double power(double x, int n){ if(n ==

Java for LeetCode 050 Pow(x, n)

Implement pow(x, n). 解题思路: 直接使用乘法实现即可,注意下,如果n很大的话,递归次数会太多,因此在n=10和n=-10的地方设置一个检查点,JAVA实现如下: static public double myPow(double x, int n) { if(n==1) return x; else if(n>1&&n<=10) return myPow(x,n-1)*x; if(n>10) return myPow(myPow(x,10),n/10

leetcode#52 N queensⅡ

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

[Leetcode][Python]52: N-Queens II

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 52: N-Queens IIhttps://oj.leetcode.com/problems/n-queens-ii/ Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions. ===Comm

【leetcode】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 configuration

[LeetCode] “全排列”问题系列(一) - 用交换元素法生成全排列及其应用,例题: Permutations I 和 II, N-Queens I 和 II,数独问题

转:http://www.cnblogs.com/felixfang/p/3705754.html 一.开篇 Permutation,排列问题.这篇博文以几道LeetCode的题目和引用剑指offer上的一道例题入手,小谈一下这种类型题目的解法. 二.上手 最典型的permutation题目是这样的: Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the foll

【leetcode刷题笔记】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 configuration of

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