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的更多相关文章

  1. 【LeetCode】1222. Queens That Can Attack the King 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...

  2. [LeetCode] “全排列”问题系列(一) - 用交换元素法生成全排列及其应用,例题: Permutations I 和 II, N-Queens I 和 II,数独问题

    一.开篇 Permutation,排列问题.这篇博文以几道LeetCode的题目和引用剑指offer上的一道例题入手,小谈一下这种类型题目的解法. 二.上手 最典型的permutation题目是这样的 ...

  3. “全排列”问题系列(一)[LeetCode] - 用交换元素法生成全排列及其应用,例题: Permutations I 和 II, N-Queens I 和 II,数独问题

    转:http://www.cnblogs.com/felixfang/p/3705754.html 一.开篇 Permutation,排列问题.这篇博文以几道LeetCode的题目和引用剑指offer ...

  4. 52. N-Queens II

    题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...

  5. LeetCode--052--N皇后II(java)

    n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回 n 皇后不同的解决方案的数量. 示例: 输入 ...

  6. [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 ...

  7. [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 ...

  8. LeetCode 264

    Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...

  9. 用试探回溯法解决N皇后问题

    学校数据结构的课程实验之一. 数据结构:(其实只用了一个二维数组) 算法:深度优先搜索,试探回溯 需求分析: 设计一个在控制台窗口运行的“n皇后问题”解决方案生成器,要求实现以下功能: 由n*n个方块 ...

  10. 八皇后问题详细分析与解答(递归法解答,c#语言描述)

    八皇后问题,是一个古老而著名的问题,是回溯算法的典型例题.该问题是十九世纪著名的数学家高斯1850年提出:在8X8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行.同一列或 ...

随机推荐

  1. php max_input_vars限制数组大小

    今天做一个项目需要post2000个数组过去,发现一直只能接到一半,后来发现是max_input_vars显示问题. 修改php.ini里面max_input_vars的大小就可以了

  2. jquery 、 JS 脚本参数的认识与使用

    jquery . JS 脚本参数的认识与使用 如何使用jquery刷新当前页面 下面介绍全页面刷新方法:有时候可能会用到 window.location.reload(); //刷新当前页面. par ...

  3. 黑马程序员——OC语言Foundation框架 NSArray NSSet NSDictionary\NSMutableDictionary

    Java培训.Android培训.iOS培训..Net培训.期待与您交流! (以下内容是对黑马苹果入学视频的个人知识点总结) (一) NSNumber 将各种基本数据类型包装成NSNumber对象 @ ...

  4. hibernate内部测试题(附赠答案)

    一.选择题(共25题,每题2.5分,选择一项或多项,漏选错选不得分) 1.在Hibernate中,以下关于主键生成器说法错误的是( ). A.increment可以用于类型为long.short或by ...

  5. php-数据库访问--数据修改

    主页面元素修改脚本 <?php $code = $_GET["c"]; //造连接对象 $db = new MySQLi("localhost",&quo ...

  6. Apache Jena - A Bootstrap

    前言 这篇文档属探究立项性质,作为语义网和本体建模工作的延续. 依照NoSQL Distilled上的考察方法,将Apache Jena作为图数据库的泛型考察.   内容 多种出版物上声明主要有四类N ...

  7. 在Swift中应用Grand Central Dispatch(下)

    在第一部分中, 你学到了并发,线程以及GCD的工作原理.通过使用dispatch_barrrier和dispatch_sync,你做到了让 PhotoManager单例在读写照片时是线程安全的.除此之 ...

  8. Python Webk框架学习 Flask

    Flask是一个使用Python编写的轻量级Web应用框架.基于Werkzeug WSGI工具箱和Jinja2 模板引擎. Flask使用BSD授权.Flask也被称为“microframework” ...

  9. 【NOIP2012】借教室

    因为本校OJ+1s所以用线段树水过了,不去syz的水库水这题还真不知道线段树过不了= = 原题: 在大学期间,经常需要租借教室.大到院系举办活动,小到学习小组自习讨论,都需要 向学校申请借教室.教室的 ...

  10. java Map迭代

    //先入先出 public class Test { public static void main(String[] args) { LinkedHashMap<String,Object&g ...