题目

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. 暴力 BestCoder Round #41 1001 ZCC loves straight flush

    题目传送门 /* m数组记录出现的花色和数值,按照数值每5个搜索,看看有几个已满足,剩下 5 - cnt需要替换 ╰· */ #include <cstdio> #include < ...

  2. Eclipse 运行内存不足情况

    在debug或者run 时 在VM arguments 处添加  -Xms512m -Xmx512m 

  3. 498 Diagonal Traverse 对角线遍历

    详见:https://leetcode.com/problems/diagonal-traverse/description/ C++: class Solution { public: vector ...

  4. (022)[工具软件]图片浏览 JPEGView

    JPEGView是一款小巧绿色快速的图像浏览工具,并且支持全屏或窗口模式.主页地址: https://sourceforge.net/projects/jpegview/JPEGView软件小巧,但功 ...

  5. 日常博客----rem,em的恩怨相杀

    基本知识: 使用 em 和 rem 单位可以让我们的设计更加灵活,能够控制元素整体放大缩小,而不是固定大小. 我们可以使用这种灵活性,使我们在开发期间,能更加快速灵活的调整,允许浏览器用户调整浏览器大 ...

  6. Ubuntu 创建docker 容器 系列一

    docker 官网安装地址:https://docs.docker.com/install/linux/docker-ce/ubuntu/ 1.Ubuntu的版本要在12.04 LTS 以上,使用un ...

  7. bt设置指定的ip地址

    auto eth0 iface eth0 inet staticaddress 192.168.1.112 IP地址netmask 255.255.255.0 子网掩码network 192.168. ...

  8. JS正则表达式匹配<div><style>标签

    测试字符串: <style>v\:* {                 BEHAVIOR: url(#default#VML) } o\:* {                 BEHA ...

  9. vba,自定义公式,农历互转公历,excel ,wps

    'vba 模块内容如下 自定义公式 '公历转农历模块 '原创:互联网 '修正: '// 农历数据定义 // '先以 H2B 函数还原成长度为 18 的字符串,其定义如下: '前12个字节代表1-12月 ...

  10. dig - 发送域名查询信息包到域名服务器

    SYNOPSIS(总览) dig [@ server ] domain [Aq query-type ] [Aq query-class ] [+ Aq query-option ] [-Aq dig ...