LeetCode(51) 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 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;
}
};
LeetCode(51) N-Queens的更多相关文章
- LeetCode(51):N皇后
Hard! 题目描述: n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回所有不同的 n 皇后问 ...
- Qt 学习之路 2(51):布尔表达式树模型
Qt 学习之路 2(51):布尔表达式树模型 豆子 2013年5月15日 Qt 学习之路 2 17条评论 本章将会是自定义模型的最后一部分.原本打算结束这部分内容,不过实在不忍心放弃这个示例.来自于 ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- 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 ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- 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 ...
- 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 ...
随机推荐
- chrome调试中resource改到application中了
如题,看视频的时候发现在resource下面查看cookie,但是自己试的时候发现没有了这个工具, google之后发现原来该位置了
- profile和bashrc
转自某不知名网友 /etc/profile,/etc/bashrc 是系统全局环境变量设定~/.profile,~/.bashrc用户家目录下的私有环境变量设定当登入系统时候获得一个shell进程时, ...
- URAL 7077 Little Zu Chongzhi's Triangles(14广州I)
题目传送门 题意:有n根木棍,三根可能能够构成三角形,选出最多的三角形,问最大面积 分析:看到这个数据范围应该想到状压DP,这次我想到了.0010101的状态中,1表示第i根木棍选择,0表示没选,每一 ...
- 165 Compare Version Numbers 比较版本号
比较两个版本号 version1 和 version2.如果 version1 大于 version2 返回 1,如果 version1 小于 version2 返回 -1, 除此以外 返回 0.您可 ...
- salt-stack系列报错
master启动报错 实验环境为: [root@master salt]# cat /proc/version Linux version 3.10.0-327.el7.x86_64 (mockbui ...
- html引入另一个html
在写页面的时候,有些东西是一样的,比如头部的导航或者尾部的标注.所以复用的东西可以写到一个文件中,之后再引入,angularjs或是jsp中都有很好的标签引入,而html没有,但是可以借助一些方式进行 ...
- Android开发精品收藏贴
各种下拉刷新效果: https://github.com/scwang90/SmartRefreshLayout
- Java长存!12个Java长久占居主要地位的原因
Java长存!12个Java长久占居主要地位的原因 我们很容易就会遗忘那些曾经在猿群中大热而又被各种新技术掩盖直至堙灭的技术的价值.就拿COBOL这个老猿们当年所用的神器来说,就跟条死鱼一样被现代猿基 ...
- COGS 2274. [HEOI 2016] tree
★☆ 输入文件:heoi2016_tree.in 输出文件:heoi2016_tree.out 简单对比时间限制:1 s 内存限制:128 MB 这道题数据弱到炸了 . 第一次做用树刨 ...
- easybcd 支持 windows 10 和 ubuntu 14.04 双系统启动
家里计算机系统 windows 10 全新安装. 原本是双系统的,还有一个ubuntu. windows 10 安装以后,恢复ubuntu就是问题了. (事后经验:请不要立刻安装bcd修改工具) 最初 ...