LeetCode_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:
[
[".Q..", // Solution 1
"...Q",
"Q...",
"..Q."], ["..Q.", // Solution 2
"Q...",
"...Q",
".Q.."]
]
分析: The classic recursive problem.
1. Use a int vector to store the current state, A[i]=j refers that the ith row and jth column is placed a queen.
2. Valid state: not in the same column, which is A[i]!=A[current], not in the same diagonal direction: abs(A[i]-A[current]) != r-i
3. Recursion:
Start: placeQueen(0,n)
if current ==n then print result
else
for each place less than n,
place queen
if current state is valid, then place next queen place Queen(cur+1,n)
end for
end if
class Solution {
public:
void record(vector<int> row)
{
vector<string> temp;
for(int i = ; i< n ; i++)
{
string str(n,'.');
str[row[i]] = 'Q';
temp.push_back(str);
}
res.push_back(temp) ;
}
bool isValid(vector<int> row, int curRow)
{
for(int i = ; i< curRow; i++)
if(row[i] == row[curRow] || abs(row[i] - row[curRow]) == curRow - i)
return false; return true;
}
void nqueue(vector<int> row,int curRow)
{
if(curRow == n)
{
record(row);
return ;
}
for(int i = ; i< n ;i++)
{
row[curRow] = i;
if(isValid(row,curRow))
nqueue(row,curRow+);
}
}
vector<vector<string> > solveNQueens(int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
res.clear();
if( n < ) return res;
this->n = n;
vector<int> row(n,-);
nqueue(row, );
return res;
}
private:
int n;
vector<vector<string> > res;
};
http://yucoding.blogspot.com/2013/01/leetcode-question-59-n-queens.html
LeetCode_N-Queens的更多相关文章
- Jeff Somers's N Queens Solutions 最快的n皇后算法
/* Jeff Somers * * Copyright (c) 2002 * * jsomers@alumni.williams.edu * or * allagash98@yahoo.com * ...
- [CareerCup] 9.9 Eight Queens 八皇后问题
9.9 Write an algorithm to print all ways of arranging eight queens on an 8x8 chess board so that non ...
- lintcode 中等题:N Queens II N皇后问题 II
题目: N皇后问题 II 根据n皇后问题,现在返回n皇后不同的解决方案的数量而不是具体的放置布局. 样例 比如n=4,存在2种解决方案 解题: 和上一题差不多,这里只是求数量,这个题目定义全局变量,递 ...
- lintcode 中等题:N Queens N皇后问题
题目: N皇后问题 n皇后问题是将n个皇后放置在n*n的棋盘上,皇后彼此之间不能相互攻击.<不同行,不同列,不同对角线> 给定一个整数n,返回所有不同的n皇后问题的解决方案. 每个解决方案 ...
- Codeforces Gym 100650D Queens, Knights and Pawns 暴力
Problem D: Queens, Knights and PawnsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu ...
- Poj 3239 Solution to the n Queens Puzzle
1.Link: http://poj.org/problem?id=3239 2.Content: Solution to the n Queens Puzzle Time Limit: 1000MS ...
- Pat1128:N Queens Puzzle
1128. N Queens Puzzle (20) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The & ...
- PAT 1128 N Queens Puzzle
1128 N Queens Puzzle (20 分) The "eight queens puzzle" is the problem of placing eight ch ...
- kolla queens on centos7.5 -all in one
目录 环境准备 开始配置 快照,快照,快照 pull镜像并部署 登录配置OpenStack 环境准备 我这里用workstation创建了一个虚拟机,安装centos7.5 mini系统,这台虚拟机上 ...
- openstack系列文章(1)devstack安装测试Queens
1.在OpenStack 圈子中,有这么一句名言:”不要让朋友在生产环境中运行DevStack.但是初学者在没有掌握OpenStack CLI的情况下用devstack安装测试环境还是不错的.本系列文 ...
随机推荐
- Linux企业级项目实践之网络爬虫(1)——项目概述及准备工作
我们在学习了Linux系统编程之后,需要一些实战项目来提高自己的水平,本系列我们通过编写一个爬虫程序,将我们学习的知识进行综合应用,同时在实现项目的过程中逐渐养成一些有用的思维方式,并具有初步的软件开 ...
- 传智播客8月C/C++基础班开班
秋天已经向我们走来,在这个充满收获的季节里,大家齐聚传智C/C++学院这个大家庭,无论你曾经从事什么工作,都拥有着一颗热爱C/C++的心,为了自己心中的梦想,大家要付出百倍的努力,要做到&quo ...
- 【No system images installed for this target】的解决方式
打开eclipse,新建安卓SDK模拟器时,选择完Target之后,再选择CPU/ABI时,默认为No system images installed for this target. 且无法编辑: ...
- allVncClients
VNC Viewer Free Edition 37 RealVNC Ltd. 15,367 Freeware 1021.58 KB VNC is client and server remo ...
- hsql使用架构包启动数据库
一.通常我们平时启动就是直接通过hsql.jar来进行启动 java -cp hsqldb.jar org.hsqldb.util.DatabaseManagerSwing java -cp hsql ...
- dom4j解析接口使用SOAP传递的xml
xml 文件的格式类型: <?xml version="1.0" encoding="utf-8"?> <SOAP-ENV:Envelope ...
- HeadFirst设计模式 之 C++实现(二):Observer(观察者模式)
观察者模式是最经常使用的设计模式之中的一个,[对象之间多对一的依赖关系,当一个对象发生变化时,其会通知全部依赖它的对象].拿订阅报纸和发行报社打例如,报社採集到news制作新的报纸,派送给订阅的客户. ...
- Eclipse UML插件AmaterasUML的配置及使用
AmaterasUML是个人觉得最好用的Eclipse UML插件,可以通过拖拽Java源文件,轻松生成类图结构,同时支持活动图.时序图和用例图.它的官方下载地址是:http://sourceforg ...
- telnet IP不通/sybase central工具无法连接到数据库
问题描述:客户端sybase central工具无法连接到数据库 服务端操作系统:RHEL5.8_x64,安装sybase-ASE15.7,端口号4112 IP:192.168.1.220 hos ...
- [CSAPP笔记][第六章存储器层次结构]
第六章 存储器层次结构 在简单模型中,存储器系统是一个线性的字节数组,CPU能够在一个常数访问每个存储器位置. 虽然是一个行之有效的模型,但没有反应现代系统实际工作方式. 实际上,存储器系统(memo ...