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皇后问题,对每行元素进行搜索即可
class Solution {
public:
int n;
vector<vector<string> > res;
vector<int> queenIndex;
bool judgePlaceQueen(int x,int y){
for(int i = ; i < x; ++ i){
if(abs(i-x) == abs(queenIndex[i]-queenIndex[x]) || queenIndex[i] == y) return false;
}
return true;
} void dfs(int index){
if(index == n){
vector<string> board(n,string(n,'.'));
for(int i = ; i < n ; ++ i){
board[i][queenIndex[i]] = 'Q';
}
res.push_back(board);
return;
}
for(int i = ; i < n; ++ i){
queenIndex[index] = i;
if(judgePlaceQueen(index,i)) dfs(index+);
}
} vector<vector<string> > solveNQueens(int n) {
this->n = n;
for(int i = ;i < n; ++ i) queenIndex.push_back();
dfs();
return res;
}
};

下面用排列组合的方法解决。

N个皇后放置在NxN的棋盘上,必定每行一个皇后,将每行中的皇后的位置用0~N-1的数字来表示,总共N行,

这样就是0~N-1数字的排列。这样的排列只满足了任意两个皇后不能处在同一行或者一列上,并不能保证它们在一斜线上。

在加入数字排列前,判断该数字是否和排列里所有的数字在斜线上:

  如果两个数字在斜线上,那么两数之差的绝对值等于其下标差得绝对值。

class Solution {
public:
vector<vector<string> > solveNQueens(int n) {
vector<vector<string> > res;
vector<int> queen(n,);
for(int i = ; i < n ; ++ i) queen[i] = i;
do{
bool flag = false;
for(int i = ; i< n && !flag; ++ i){
for(int j = ; j < i && !flag; ++ j){
if(abs(j-i) == abs(queen[j]-queen[i]) || queen[j] == queen[i]) flag = true;
}
}
if(!flag){
vector<string> board(n,string(n,'.'));
for(int i = ; i < n; ++ i) board[i][queen[i]]='Q';
res.push_back(board);
}
}while(next_permutation(queen.begin(),queen.end()));
return res;
}
};

Leetcode N-Queens的更多相关文章

  1. [Leetcode] n queens ii n皇后问题

    Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...

  2. 【LeetCode】1222. Queens That Can Attack the King 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...

  3. 【leetcode】1222. Queens That Can Attack the King

    题目如下: On an 8x8 chessboard, there can be multiple Black Queens and one White King. Given an array of ...

  4. [LeetCode] N-Queens II N皇后问题之二

    Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...

  5. [LeetCode] N-Queens N皇后问题

    The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...

  6. [CareerCup] 9.9 Eight Queens 八皇后问题

    9.9 Write an algorithm to print all ways of arranging eight queens on an 8x8 chess board so that non ...

  7. Leetcode | N-Queens I & II

    N-Queens I The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no ...

  8. [LeetCode]题解(python):051-N-Queens

    题目来源 https://leetcode.com/problems/n-queens/ The n-queens puzzle is the problem of placing n queens ...

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

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 52: N-Queens IIhttps://oj.leetcode.com/ ...

  10. [Leetcode][Python]51: N-Queens

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 51: N-Queenshttps://oj.leetcode.com/pro ...

随机推荐

  1. 【转】Controllers and Routers in ASP.NET MVC 3

    Controllers and Routers in ASP.NET MVC 3 ambilykk, 3 May 2011 CPOL 4.79 (23 votes) Rate: vote 1vote ...

  2. .Net创建windows服务入门

    本文主要记录学习.net 如何创建windows服务. 1.创建一个Windows服务程序 2.新建安装程序 3.修改service文件 代码如下 protected override void On ...

  3. “基础提供程序在Open上失败”

    本来布置在IP为[x.x.x.x]的WCF服务好好的,但是今天突然就有问题了,一调用报错"基础提供程序在Open上失败"... 服务器上的有问题,先试试本地的服务能不能用吧,连的都 ...

  4. css3径向渐变详解-遁地龙卷风

    (-1)写在前面 我用的是chrome49,如果你用的不是.可以尝试换下浏览器前缀.IE在这方面的实现又特例独行了.不想提及-,这篇是为后续做准备. (0)快速使用 background-image: ...

  5. https://zeroc.com/index.html

    https://zeroc.com/index.html http://blog.shutupandcode.net/?p=1085

  6. Linux 网络子系统

    今天记录一下Linux网络子系统相关的东西. 因为感觉对这一块还是有一个很大的空白,这件事情太可怕了. 摘抄多份博客进行总结一下Linux网络子系统的相关东西. 一. Linux网络子系统体系结构 L ...

  7. 使用python列表推导式进行99乘法表

    首先这很python for i in range(1, 10): print(" ".join(["%d*%d=%d" % (j, i, i*j) for j ...

  8. start WampServer如何关闭浏览目录

    打开Httpd.conf IncludesNOEXEC Indexes 去掉这个代码就可以了

  9. POJ 2337 Catenyms(有向图的欧拉通路)

    题意:给n个字符串(3<=n<=1000),当字符串str[i]的尾字符与str[j]的首字符一样时,可用dot连接.判断用所有字符串一次且仅一次,连接成一串.若可以,输出答案的最小字典序 ...

  10. js 中 Math对象

    Math 对象是一个固有的对象,无需创建它,直接把 Math 作为对象使用就可以调用其所有属性和方法.这是它与Date,String对象的区别. Math 对象属性 Math 对象方法