题目:

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.

Example:

Input: 4
Output: [
[".Q..", // Solution 1
"...Q",
"Q...",
"..Q."], ["..Q.", // Solution 2
"Q...",
"...Q",
".Q.."]
]
Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above.

分析:

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

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

每一种解法包含一个明确的 n 皇后问题的棋子放置方案,该方案中 'Q' 和 '.' 分别代表了皇后和空位。

非常经典的问题,首先可以想到的是穷举所有皇后摆放的情况,来判断棋盘是否符合要求。但这样做肯定是会超时的。

以n=4为例,我们构建一个二维数组sta,用来标识皇后能否放置,最开始皇后可以放置再任何地方,我们从第一行开始放置皇后,根据放置的位置,我们来修改sta数组(1为不能放置,0为可以放置),将放置位置的行列以及两条对角线上的元素全部置1,之后在第二行放置皇后,并根据sta数组来判断当前位置皇后是否可以被放置,如果当前行搜索完毕后,都没有位置放置皇后,那么我们就要回到上一步放置皇后的状态,再去搜索下一个位置了。每次放置前要保存当前结果和当前判断数组(sta)以便回溯。

对于判断皇后放置的二维数组,可以换成一个列数组加上两个对角线数组来标记一个位置是否可以放置皇后,因为我们是一行一行来放置的,所以不存在某一行出现两个皇后,所以当一列放置了一个皇后时,该列标记为不可放置(col[i]=1,即第i列已经有皇后了),对角线可以对照下图:

[x,y]位置对应从左下角至右上角的对角线diag1[x+y],而对应从左上角至右下角的对角线diag2[y-x+n-1],这样每一个位置能否放置皇后判断的条件都有了,不用再使用一个棋盘大小的二维数组来标识了。

程序:

class Solution {
public:
void update(int n, vector<vector<int>> &vec, int x, int y){
for(int i = ; i < n; ++i){
vec[i][y] = ;
vec[x][i] = ;
}
int i = ,j = ;
for(i = x, j = y; i < n && j < n; ++i, ++j){
vec[i][j] = ;
}
for(i = x, j = y; i >= && j >= ; --i, --j){
vec[i][j] = ;
}
for(i = x, j = y; i < n && j >= ; ++i, --j){
vec[i][j] = ;
}
for(i = x, j = y; i >= && j < n; --i, ++j){
vec[i][j] = ;
}
return;
}
void queen(int n, int y, vector<string> &temp_res, vector<vector<int>> &sta){
if(y == n){
res.push_back(temp_res);
return;
}
for(int j = ; j < n; ++j){
if(sta[y][j] == ){
vector<string> temp2 = temp_res;
temp_res[y][j] = 'Q';
vector<vector<int>> temp = sta;
update(n, sta, y, j);
queen(n, y+, temp_res, sta);
sta = temp;
temp_res = temp2;
}
else
continue;
}
return;
}
vector<vector<string>> solveNQueens(int n) {
vector<string> temp_res(n, string(n, '.'));
vector<vector<int>> sta(n, vector<int>(n,));
queen(n, , temp_res, sta);
return res;
}
private:
vector<vector<string>> res;
};
class Solution {
public:
void queen(int n, int x, vector<string> &temp_res){
if(x == n){
res.push_back(temp_res);
return;
}
for(int y = ; y < n; ++y){
if(col[y] == && diag1[x+y] == && diag2[y-x+n-] == ){
temp_res[x][y] = 'Q';
col[y] = ;
diag1[x+y] = ;
diag2[y-x+n-] = ;
queen(n, x+, temp_res);
temp_res[x][y] = '.';
col[y] = ;
diag1[x+y] = ;
diag2[y-x+n-] = ;
}
else
continue;
}
return;
}
vector<vector<string>> solveNQueens(int n) {
vector<string> temp_res(n, string(n, '.'));
col = vector<int>(n, );
diag1 = vector<int>( * n - , );
diag2 = vector<int>( * n - , );
queen(n, , temp_res);
return res;
}
private:
vector<vector<string>> res;
vector<int> col;
vector<int> diag1;
vector<int> diag2;
};

LeetCode 51. N-QueensN皇后 (C++)(八皇后问题)的更多相关文章

  1. LeetCode 回溯法 别人的小结 八皇后 递归

    #include <iostream> #include <algorithm> #include <iterator> #include <vector&g ...

  2. USACO 1.5.4 Checker Challenge跳棋的挑战(回溯法求解N皇后问题+八皇后问题说明)

    Description 检查一个如下的6 x 6的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行,每列,每条对角线(包括两条主对角线的所有对角线)上都至多有一个棋子. 列号 0 1 2 3 4 5 6 ...

  3. 九度OJ 1140:八皇后 (八皇后问题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:795 解决:494 题目描述: 会下国际象棋的人都很清楚:皇后可以在横.竖.斜线上不限步数地吃掉其他棋子.如何将8个皇后放在棋盘上(有8 * ...

  4. LeetCode 31:递归、回溯、八皇后、全排列一篇文章全讲清楚

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天我们讲的是LeetCode的31题,这是一道非常经典的问题,经常会在面试当中遇到.在今天的文章当中除了关于题目的分析和解答之外,我们还会 ...

  5. C#中八皇后问题的递归解法——N皇后

    百度测试部2015年10月份的面试题之——八皇后. 八皇后问题的介绍在此.以下是用递归思想实现八皇后-N皇后. 代码如下: using System;using System.Collections. ...

  6. 【剑指offer】八皇后问题

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/26614999 剑指offer上解决八皇后问题,没实用传统的递归或非递归回溯法,而是用了非常 ...

  7. 带你轻而易举的学习python——八皇后问题

    首先我们来看一下这个著名的八皇后问题 八皇后问题:在8×8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行.同一列或同一斜线上,问有多少种摆法. 在这个问题提出之后人们又将 ...

  8. java递归求八皇后问题解法

    八皇后问题 八皇后问题,是一个古老而著名的问题,是回溯算法的典型案例.该问题是国际西洋棋棋手马克斯·贝瑟尔于1848年提出:在8×8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处 ...

  9. 数据结构之递归Demo(走迷宫)(八皇后)(汉诺塔)

    递归 顾名思义,递归就是递归就是递归就是递归就是递归......就是递归 Google递归:

随机推荐

  1. <Tree> 298 250 366 199(高频) 98(高频)

    298. Binary Tree Longest Consecutive Sequence 先序遍历,根左右.如果该节点的 value == 父节点value + 1, 则长度+1; 否则重置为1. ...

  2. Paper | No-reference Quality Assessment of Deblocked Images

    目录 故事背景 本文方法(DBIQ) 发表在2016年Neurocomputing. 摘要 JPEG is the most commonly used image compression stand ...

  3. 阿里Sentinel整合Zuul网关详解

    前面我们讲解了Sentinel整合Spring Cloud Gateway,详细请查看文章:阿里Sentinel支持Spring Cloud Gateway啦 目前来说,大部分公司线上的网关应该是Zu ...

  4. 行为驱动:Cucumber + Java - 实现数据的参数化

    1.什么是参数化 实际设计测试用例过程中,我们经常会用等价类.边界值这样的方法,针对一个功能进行测试数据上的测试,比如一个输入框,正向数据.逆向数据,非法输入等等 2.Cucumber的数据驱动 同上 ...

  5. 配置每次git push 不需要输入账号密码

    配置每次git push 不需要输入账号密码 .gitconfig文件地址 C:\Users\Admin

  6. MyCat(转)

    https://www.cnblogs.com/bingosblog/p/7171501.html    http://www.cnblogs.com/joylee/p/7513038.html ht ...

  7. 【转】C#各版本新增加功能

    本系列文章主要整理并介绍 C# 各版本的新增功能. C# 8.0 C#8.0 于 2019年4月 随 .NET Framework 4.8 与 Visual Studio 2019 一同发布,但是当前 ...

  8. Elasticsearch 7.x从入门到精通

    Elasticsearch是一个分布式.可扩展.近实时的搜索与数据分析引擎,它能从项目一开始就赋予你的数据以搜索.分析和探索的能力. 通过本专栏的学习,你可以了解到,Elasticsearch在互联网 ...

  9. ELK 日志平台 For Windows

    一.Logstash 安装 1. 下载最新版本的logstash:  https://www.elastic.co/fr/downloads/logstash 下载zip格式的压缩包. 然后解压缩放到 ...

  10. .NET WebFrom跨时区项目时间问题处理方法

    前段时间因为公司的一个 WebFrom 项目设计到跨时区的问题,处理了一段时间,终于解决了,写个博客记录一下,方便以后回顾以及给他人提供一个参考的方法. 本次的项目因为跨越了多个时区,在一些时间上会受 ...