[LintCode] N-Queens
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.
There exist two distinct solutions to the 4-queens puzzle:
[
[".Q..", // Solution 1
"...Q",
"Q...",
"..Q."],
["..Q.", // Solution 2
"Q...",
"...Q",
".Q.."]
]
class Solution {
public:
/**
* Get all distinct N-Queen solutions
* @param n: The number of queens
* @return: All distinct solutions
* For example, A string '...Q' shows a queen on forth position
*/
bool isOK(vector<string> &v, int n, int x, int y) {
for (int i = ; i < x; ++i)
if (v[i][y] == 'Q') return false;
for (int i = ; x - i >= && y - i >= ; ++i)
if (v[x-i][y-i] == 'Q') return false;
for (int i = ; x - i >= && y + i < n; ++i)
if (v[x-i][y+i] == 'Q') return false;
return true;
}
void dfs(vector<vector<string>> &res, vector<string> &v, int n, int idx) {
if (idx == n) {
res.push_back(v);
return;
}
for (int i = ; i < n; ++i) {
v[idx][i] = 'Q';
if (isOK(v, n, idx, i)) dfs(res, v, n, idx + );
v[idx][i] = '.';
}
}
vector<vector<string> > solveNQueens(int n) {
// write your code here
vector<vector<string>> res;
string s;
for (int i = ; i < n; ++i) s.push_back('.');
vector<string> v(n, s);
dfs(res, v, n, );
return res;
}
};
[LintCode] N-Queens的更多相关文章
- lintcode 中等题:N Queens II N皇后问题 II
题目: N皇后问题 II 根据n皇后问题,现在返回n皇后不同的解决方案的数量而不是具体的放置布局. 样例 比如n=4,存在2种解决方案 解题: 和上一题差不多,这里只是求数量,这个题目定义全局变量,递 ...
- lintcode 中等题:N Queens N皇后问题
题目: N皇后问题 n皇后问题是将n个皇后放置在n*n的棋盘上,皇后彼此之间不能相互攻击.<不同行,不同列,不同对角线> 给定一个整数n,返回所有不同的n皇后问题的解决方案. 每个解决方案 ...
- (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)
--------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...
- 【LeetCode/LintCode】丨Google面试题:N皇后问题
n皇后问题是将n个皇后放置在n*n的棋盘上,皇后彼此之间不能相互攻击(任意两个皇后不能位于同一行,同一列,同一斜线). 给定一个整数n,返回所有不同的n皇后问题的解决方案. 每个解决方案包含一个明确的 ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- Lintcode 85. 在二叉查找树中插入节点
-------------------------------------------- AC代码: /** * Definition of TreeNode: * public class Tree ...
- Lintcode 166. 主元素
----------------------------------- Moore's voting algorithm算法:从一个集合中找出出现次数半数以上的元素,每次从集合中去掉一对不同的数,当剩 ...
- Lintcode 166. 链表倒数第n个节点
----------------------------------- 最开始的想法是先计算出链表的长度length,然后再从头走 length-n 步即是需要的位置了. AC代码: /** * De ...
- Lintcode 157. 判断字符串是否没有重复字符
------------------------ 因为字符究竟是什么样的无法确定(比如编码之类的),恐怕是没办法假设使用多大空间(位.数组)来标记出现次数的,集合应该可以但感觉会严重拖慢速度... 还 ...
- Lintcode 175. 翻转二叉树
-------------------- 递归那么好为什么不用递归啊...我才不会被你骗...(其实是因为用惯了递归啰嗦的循环反倒不会写了...o(╯□╰)o) AC代码: /** * Definit ...
随机推荐
- Webwork【07】文件上传下载
Web上传和下载应该是很普遍的一个需求,无论是小型网站还是大并发访问的交易网站.WebWork 当然也提供了很友好的拦截器来实现对文件的上传,让我们可以专注与业务逻辑的设计和实现,在实现上传和下载时顺 ...
- Htmlparser专题
http://htmlparser.sourceforge.net/javadoc/index.html
- wordpress 开源博客系统部署
1.开发工具 server apache 下载地址:http://www.apache.org http://httpd.apache.org/download.cgi 数据库 mys ...
- 3、redis之java client环境搭建
JAVA Client环境搭建 POM: <dependency> <groupId>redis.clients</groupId> <artifactId& ...
- django之创建第3个项目:编写第一个模板文件
1.django结构 2.在站点blog下创建templates文件夹,专门用于存放模板文件 3.在templates文件夹下创建index.html文件 #index.html <!DOCTY ...
- html 5检查Mobile App是否在线
在PhoneGap应用,或者黑莓Java/HTML混搭应用中,如何使用JavaScript检查Mobile App网络连接状态呢? <script type="text/javascr ...
- 整合ssm框架之配置文件
原文:https://blog.csdn.net/zwyanqing/article/details/53039591 ssm整合 一.applicationContext.xml 1.配置数据源 & ...
- Ubuntu 1604配置安装mysql8.0
安装步骤: 一.通过APT方式安装 说明:此种方式完全参考官方提供的教程https://dev.mysql.com/doc/mysql-apt-repo-quick-guide/en/. 注意:通过A ...
- Spring Boot 使用Jar打包发布, 并使用 Embedded Jetty/Tomcat 容器
Jar包发布 在项目pom.xml中, 如果继承了Spring Boot的starter parent, 那么默认已经包含打包需要的plugins了, 设置为jar就能直接打包成包含依赖的可执行的ja ...
- Oracle 中TNS的作用
什么是TNS? TNS是Oracle Net的一部分,专门用来管理和配置Oracle数据库和client连接的一个工具,在大多数情况下client和数据库要通讯,必须配置TNS,当然在少数情况下,不用 ...