N-Queens leetcode
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.."]
]
解题思路:
使用回溯法解决N皇后问题,是常见的解决方法。分N步放置皇后,由于皇后之间不能同行、同列、同斜线,所以每次放置皇后的时候,都要考虑是否与已有的皇后“冲突”,如果冲突,则改变位置,直至所有皇后放置完毕。
测试皇后冲突的函数:isValid();
本文思路比较取巧,使用a[n]记录N皇后位置。根据皇后放置规则可知,每一行有且只有一个皇后,所以从第一行到第N行,依次放置第n个皇后。a[n]记录第n个皇后所在列数。即第i个皇后放置在第i行第a[i]列。
参考:http://blog.csdn.net/feixiaoxing/article/details/6877965
具体实现如下(AC 36ms):
class Solution {
public:
vector<vector<string> > re; //测试在第row行,第row列放置皇后是否有效
int isValid(int *a, int n, int row, int col)
{
int tmpcol=0;
for(int tmprow=0;tmprow<row;tmprow++)
{
tmpcol = a[tmprow];
if(tmpcol == col)// 同列
return 0;
if((tmpcol-col) == (tmprow - row))// 在同一右斜线
return 0;
if((tmpcol-col) == (row - tmprow))// 在同一左斜线
return 0;
}
return 1;
} void PrintN(int *a, int n)
{
vector<string> tmps;
for(int i=0;i<n;i++)
{
string s(n,'.');
s[a[i]]='Q';
tmps.push_back(s);
}
re.push_back(tmps);
}
void n_queens(int *a,int n, int index)
{
for(int i=0;i<n;i++)
{
if(isValid(a,n,index,i))
{
a[index]=i;
if(index == n-1)
{
PrintN(a,n);
a[index]=0;
return;
}
n_queens(a,n,index+1);
a[index]=0;
}
}
} vector<vector<string> > solveNQueens(int n) { int *a = new int[n];
memset(a,0,sizeof(int)*n);
n_queens(a,n,0);
return re;
}
};
N皇后个数对应解的个数:(验证程序)
n solution(n)
1 1
2 0
3 0
4 2
5 10
6 4
7 40
8 92
9 352
10 724
11 2680
12 14200
13 73712
14 365596
15 2279184
16 14772512
17 95815104
18 666090624
19 4968057848
20 39029188884
21 314666222712
22 2691008701644
23 24233937684440
24 227514171973736
25 2207893435808352
N-Queens leetcode的更多相关文章
- N-Queens II leetcode java
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
- 【LeetCode】1222. Queens That Can Attack the King 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- [Leetcode] n queens ii n皇后问题
Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...
- 【leetcode】1222. Queens That Can Attack the King
题目如下: On an 8x8 chessboard, there can be multiple Black Queens and one White King. Given an array of ...
- [LeetCode] N-Queens II N皇后问题之二
Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...
- [LeetCode] N-Queens N皇后问题
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...
- [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 ...
- Leetcode | N-Queens I & II
N-Queens I The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no ...
- [LeetCode]题解(python):051-N-Queens
题目来源 https://leetcode.com/problems/n-queens/ The n-queens puzzle is the problem of placing n queens ...
- [Leetcode][Python]52: N-Queens II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 52: N-Queens IIhttps://oj.leetcode.com/ ...
随机推荐
- Some Simple Models of Neurons
Linear neuron: \[y=b+\sum\limits_i{x_i w_i}\] Binary threshold neuron: \[z = \sum\limits_i{x_i w_i}\ ...
- 《JavaScript权威指南》学习笔记 第六天 开始学习DOM了。
昨天学习了window对象的一些方法.window对象主要是针对当前视窗的操作.window对象提供了一些列API来帮助我们了解当前窗口的信息.例如history对象可以让我们获取浏览历史.nvaig ...
- BZOJ1798: [Ahoi2009]Seq 维护序列seq
传送门 写这道题是为了get一个同时传送乘法下标和加法下标的小技巧,线段树模板题.不多说. 标记名字打错无限智力-- //BZOJ 1798 //by Cydiater //2016.9.13 #in ...
- BZOJ1588[HNOI2002]营业额统计
传送门 平衡树常规题,给出两种实现算法 Treap版: //OJ 1610 //by Cydiater //2016.9.1 #include <iostream> #include &l ...
- angularjs工具方法
1.angular.extend var dst = {name: 'xxx', country: 'China'}; var src1 = {name: 'yyy', age: 10}; var s ...
- C#------获取最后一个"/"字符后面的所有内容
public ActionResult GetFile(string id) { var path = _db.MailAtchs.Where(p => p.MailID == new Guid ...
- Apache+php+mysql+SQLyog在windows7下的安装与配置图解
先准备好软件: Apache官方下载地址:httpd-2.2.25-win32-x86-openssl-0.9.8y.msi,更多版本在这里: php官方下载地址:php-5.4.37-Win32-V ...
- 自然语言19_Lemmatisation
QQ:231469242 欢迎喜欢nltk朋友交流 https://en.wikipedia.org/wiki/Lemmatisation Lemmatisation (or lemmatizatio ...
- 图片延迟加载jquery插件imgLazyLoading
实现了图片延迟加载功能,插件代码非常简洁,且每个功能都把注释写得非常详细,适合网友们学习,好好利用哦 引入图片延迟加载Jquery插件文件后,页面使用代码如下: <script type=&qu ...
- iTunes访问自己应用的沙盒