题目

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:

分析

一个经典N皇后问题,这种棋盘类的题目一般是回溯法, 依次放置每行的皇后。要求在放置的时候,要保持当前的状态为合法,即当前放置位置的同一行、同一列、两条对角线上都不存在皇后。

多种解法请参考

AC代码


class Solution {
private:
vector<vector<string> > ret;
public:
vector<vector<string>> solveNQueens(int n) {
if (n <= 0)
return vector<vector<string>>(); //二维字符矩阵,存储当前满足N皇后的解
vector<string> cur(n, string(n, '.')); //调用主函数
set_queens(cur, 0);
return ret;
} void set_queens(vector<string> &cur, int row)
{
int size = cur.size();
if (row == size)
{
ret.push_back(cur);
return;
}
else{
for (int col = 0; col < size; col++)
{
if (isValid(cur, row, col))
{
cur[row][col] = 'Q';
//安置下一个皇后
set_queens(cur, row + 1);
cur[row][col] = '.';
}//for
}//for
}
} //判断在cur[row][col]位置放一个皇后,是否是合法的状态
//已经保证了每行一个皇后,只需要判断列是否合法以及对角线是否合法。
bool isValid(vector<string> &cur, int row, int col)
{
//判断是否同列
for (int i = 0; i < row; i++)
{
if (cur[i][col] == 'Q')
return false;
}//for //判断是否同一左对角线
for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; --i, --j)
{
if (cur[i][j] == 'Q')
return false;
} //判断是否同一右对角线
for (int i = row - 1, j = col + 1; i >= 0 && j <= cur.size(); --i, ++j)
{
if (cur[i][j] == 'Q')
return false;
} return true;
}
};

GitHub测试程序源码

LeetCode(51) N-Queens的更多相关文章

  1. LeetCode(51):N皇后

    Hard! 题目描述: n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回所有不同的 n 皇后问 ...

  2. Qt 学习之路 2(51):布尔表达式树模型

    Qt 学习之路 2(51):布尔表达式树模型 豆子 2013年5月15日 Qt 学习之路 2 17条评论 本章将会是自定义模型的最后一部分.原本打算结束这部分内容,不过实在不忍心放弃这个示例.来自于 ...

  3. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  4. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  5. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  6. LeetCode(122) Best Time to Buy and Sell Stock II

    题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...

  7. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

  8. LeetCode(113) Path Sum II

    题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...

  9. LeetCode(107) Binary Tree Level Order Traversal II

    题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...

随机推荐

  1. Apache Kylin 是什么?

    Apache Kylin的官网 http://kylin.apache.org/cn/ - 可扩展超快OLAP引擎:  Kylin是为减少在Hadoop上百亿规模数据查询延迟而设计 - Hadoop ...

  2. 一些CSS的备忘

    text-transform 文本转换 属性值是 none表示没有 不转换 同时也是默认的 capitalize 表示首字母大写 uppercase全部转换为大写 lowercase全部转为小写 te ...

  3. two_sum问题

    def two_sum(li, target): for i in range(len(li)): for j in range(i+1, len(li)): if li[i] + li[j] == ...

  4. 使用Ctex中遇到的一些问题

    一般下载好Ctex,我是使用Latex+dvi2pdf完成编译的,但是发现推荐的使用为:1)运行CCT & Latex命令生成两次dvi和ps文件 2)使用dvi2pdf编译dvi文件生成pd ...

  5. 简单几何(求交点) UVA 11178 Morley's Theorem

    题目传送门 题意:莫雷定理,求三个点的坐标 分析:训练指南P259,用到了求角度,向量旋转,求射线交点 /*********************************************** ...

  6. 题解报告:poj 2631 Roads in the North(最长链)

    Description Building and maintaining roads among communities in the far North is an expensive busine ...

  7. Hibernate的一级缓存:快照区

    参考来源:http://blog.sina.com.cn/s/blog_981ee5d80102w85f.html

  8. Win7系统出现提示: “Windows已遇到关键问题,将在一分钟后自动重新启动。”

    1. 若用户在使用Win7系统时,遇到上述系统故障,建议重启电脑.等电脑开机自检一过,马上按键盘上的F8键,选择进入安全模式.在安全模式下,进行系统还原.其他的解决方法见下. 1.或者,在安全模式下, ...

  9. java.util.Properties类的介绍-配置文件的读写【-Z-】

    简介:java.util.Properties是对properties这类配置文件的映射.支持key-value类型和xml类型两种. #打头的是注释行,Properties会忽略注释.允许只有key ...

  10. 数位dp知识

    转自http://blog.csdn.net/zhaoxinfan/article/details/8707605 下面先给出数位DP的背景: •在给定区间[A,B]内,找满足要求的数. •要求一般和 ...