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. Linux Shell Demo

    #!/bin/sh for p in ./* do if test -d $p then touch $p".ok" if test -f $p"/busi.xml&qu ...

  2. Hibernate 映射关系

    映射组成关系 •建立域模型和关系数据模型有着不同的出发点: –域模型: 由程序代码组成, 通过细化持久化类的的粒度可提高代码的可重用性, 简化编程 –在没有数据冗余的情况下, 应该尽可能减少表的数目, ...

  3. 建筑行业如何用BPM替换OA?

    2015年4月,K2正式与上海水石建筑规划设计有限公司签约. 为提高公司运作流程的效率,有效的对各流程的运作管理和优化,降低成本,同时提高公司的资金管理水平,水石公司利用K2系统作为整个公司流程的管理 ...

  4. 【转】局域网内访问VS2012 调试的IIS Express web服务器

    1.修改发布项目的web属性 2.在我的文档中打开IISExpress\config\applicationhost.config 加上下面的一句 3.重启调试 转自:http://blog.chin ...

  5. 在你决定从事iOS开发前需要清楚的几个问题

    作者:David McGraw  翻译:丁丁(jackiehoo) 原文:http://www.xmcgraw.com/what-you-need-to-know-to-start-learning- ...

  6. Xcode真机测试could not find developer disk image解决方法(支持iOS9.2)

    这个问题开发者经常碰到,因为当我们更新手机iOS版本的时候,可能我们开发人员因为项目的需要等原因并一定愿意更新xcode到最新版本.但是老版本的xcode极有可能不支持最新的iOS版本,也有一些旧的i ...

  7. 解决Only a type can be imported. com.mysql.jdbc.Connection resolves to a package的报错问题

    写jsp加载数据驱动以后老是提示Only a type can be imported. com.mysql.jdbc.Connection resolves to a package的错误,然而改成 ...

  8. (转)PhoneGap开发环境搭建

    (原)http://www.cnblogs.com/Random/archive/2011/12/28/2305398.html PhoneGap开发环境搭建   项目中要用PhoneGap开发,了解 ...

  9. ssential Diagram for Windows FormsC#/winForm类似visio的拓扑图节点连线控件免费下载

    Essential Diagram for Windows Forms是一款可扩展的.高性能的.NET平台下的拓扑图控件,可用于开发像Microsoft Visio一样的交互式地绘图和图解应用程序,在 ...

  10. Golang Clearing slice

    //first method : slice = nil // second method : slice = slice[0:0] Source page : https://www.socketl ...