58. N-Queens && N-Queens II
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.."]
]
思路: 简单题。全排列。(注意各行各列不同可以直接确定住)
void getSolution(veor<int> &r, vector<vector<string> > &vec) {
int n = r.size();
vector<string> vec2;
for(int i = 0; i < n; ++i) {
vec2.push_back(string(n, '.'));
vec2[i][r[i]] = 'Q';
}
vec.push_back(vec2);
} bool judge(vector<int> &r) {
for(size_t i = 0; i < r.size(); ++i)
for(size_t j = i+1; j < r.size(); ++j)
if(j-i == r[j]-r[i] || j-i == r[i]-r[j])
return false;
return true;
} void getPosition(int cur, vector<int> &r, vector<vector<string> > &vec) {
if(cur == r.size()) {
if(judge(r))
getSolution(r, vec);
return;
}
for(int e = cur; e < r.size(); ++e) {
int t = r[cur];
r[cur] = r[e];
r[e] = t;
getPosition(cur+1, r, vec);
r[e] = r[cur];
r[cur] = t;
}
} class Solution {
public:
vector<vector<string> > solveNQueens(int n) {
vector<vector<string> > vec;
if(n <= 0) return vec;
vector<int> r(n);
for(int i = 0; i < n; ++i) r[i] = i;
getPosition(0, r, vec);
return vec;
}
};
N-Queens II
Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.
思路: 比上题好像更简单一些。
bool judge(vector<int> &r) {
for(size_t i = 0; i < r.size(); ++i)
for(size_t j = i+1; j < r.size(); ++j)
if(j-i == r[j]-r[i] || j-i == r[i]-r[j])
return false;
return true;
}
void getPosition(int cur, vector<int> &r, int &cnt) {
if(cur == r.size()) {
if(judge(r))
++cnt;
return;
}
for(int e = cur; e < r.size(); ++e) {
int t = r[cur];
r[cur] = r[e];
r[e] = t;
getPosition(cur+1, r, cnt);
r[e] = r[cur];
r[cur] = t;
}
}
class Solution {
public:
int totalNQueens(int n) {
if(n <= 0) return 0;
int cnt = 0;
vector<int> r(n);
for(int i = 0; i < n; ++i) r[i] = i;
getPosition(0, r, cnt);
return cnt;
}
};
可参考剑指 offer:题28
58. N-Queens && N-Queens II的更多相关文章
- 【LeetCode】1222. Queens That Can Attack the King 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- [LeetCode] “全排列”问题系列(一) - 用交换元素法生成全排列及其应用,例题: Permutations I 和 II, N-Queens I 和 II,数独问题
一.开篇 Permutation,排列问题.这篇博文以几道LeetCode的题目和引用剑指offer上的一道例题入手,小谈一下这种类型题目的解法. 二.上手 最典型的permutation题目是这样的 ...
- “全排列”问题系列(一)[LeetCode] - 用交换元素法生成全排列及其应用,例题: Permutations I 和 II, N-Queens I 和 II,数独问题
转:http://www.cnblogs.com/felixfang/p/3705754.html 一.开篇 Permutation,排列问题.这篇博文以几道LeetCode的题目和引用剑指offer ...
- 52. N-Queens II
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
- LeetCode--052--N皇后II(java)
n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回 n 皇后不同的解决方案的数量. 示例: 输入 ...
- [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 ...
- [LeetCode] 51. N-Queens N皇后问题
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...
- LeetCode 264
Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...
- 用试探回溯法解决N皇后问题
学校数据结构的课程实验之一. 数据结构:(其实只用了一个二维数组) 算法:深度优先搜索,试探回溯 需求分析: 设计一个在控制台窗口运行的“n皇后问题”解决方案生成器,要求实现以下功能: 由n*n个方块 ...
- 八皇后问题详细分析与解答(递归法解答,c#语言描述)
八皇后问题,是一个古老而著名的问题,是回溯算法的典型例题.该问题是十九世纪著名的数学家高斯1850年提出:在8X8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行.同一列或 ...
随机推荐
- hdu 1041 (OO approach, private constructor to prevent instantiation, sprintf) 分类: hdoj 2015-06-17 15:57 25人阅读 评论(0) 收藏
a problem where OO seems more natural to me, implementing a utility class not instantiable. how to p ...
- java 深度探险 java 泛型
Java泛型(generics)是JDK 5中引入的一个新特性,允许在定义类和接口的时候使用类型参数(type parameter).声明的类型参数在使用时用具体的类型来替换.泛型最主要的应用是在JD ...
- Jmeter—7 测试中使用到的定时器和逻辑控制器
1 测试中提交数据有延时1min,所以查询数据是否提交成功要设置定时器. 固定定时器页面:单位是毫秒 [dinghanhua] 2 集合点.Synchronizing Timer 集合点编辑:集合用户 ...
- js(function(){alert(‘’‘)})
function(){alert('sss')}是个匿名函数.没有名字.所以没有办法调用.在外面加个括号,就变成了一个值,值的内容是函数的引用.例如var a = (function(){" ...
- C# winform中的datagridview控件标头加入checkbox,实现全选功能。
/// <summary> /// 给DataGridView添加全选 /// </summary> public class AddCheckBoxToDataGridVie ...
- Flask-DebugToolbar
This extension adds a toolbar overlay to Flask applications containing useful information for debugg ...
- asp.net 发布后,遇到的导出excel报错的问题
做的asp.net程序,最近要发布在外网上,发布过程不太难,网上都有现成的,只要按照相应的步骤基本都不会有什么问题,关键是发布成功后,程序中涉及到excel的导出或者导入问题,就会提示“检索COM 类 ...
- FreeBSD打开DTrace支持
主要翻译自:https://wiki.freebsd.org/DTrace FreeBSD跟Linux发行版一个比较大的差异,就是提倡源码构建.因此这里提到比较多的编译开关设置.自2012年5月后,D ...
- 看啦这么就别人的博客 我也来写一篇! Object转换其他类型
package com.sinitek.framework.util; import java.math.BigDecimal;import java.sql.Timestamp;import jav ...
- C#利用SMTP服务器发送邮件
使用.net(C#)发送邮件学习手册(带成功案例) 1.了解发送邮件的三种方式 2.实例介绍使用client.DeliveryMethod = System.Net.Mail.SmtpDelivery ...