51. N-Queens 52. N-Queens II *HARD*
1. 求所有解
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 insert(vector<vector<string>> &ans, vector<int> q, int n)
{
vector<string> v(n, string(n, '.'));
for(int i = ; i < n; i++)
v[i][q[i]] = 'Q';
ans.push_back(v);
}
void helper(vector<vector<string>> &ans, vector<int> q, int n, int k)
{
if(k == n)
{
insert(ans, q, n);
return;
}
int i, j;
for(i = ; i < n; i++)
{
for(j = ; j < k; j++)
{
if(q[j] == i || abs(j-k) == abs(q[j]-i))
break;
}
if(j < k)
continue;
q[k] = i;
helper(ans, q, n, k+);
}
}
vector<vector<string>> solveNQueens(int n) {
vector<vector<string>> ans;
vector<int> q(n, -);
for(int i = ; i < n; i++)
{
q[] = i;
helper(ans, q, n, );
}
return ans;
}
2. 求解的个数
Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.

void helper(int &ans, vector<int> q, int n, int k)
{
if(k == n)
{
ans++;
return;
}
int i, j;
for(i = ; i < n; i++)
{
for(j = ; j < k; j++)
{
if(q[j] == i || abs(j-k) == abs(q[j]-i))
break;
}
if(j < k)
continue;
q[k] = i;
helper(ans, q, n, k+);
}
}
int totalNQueens(int n) {
int ans = ;
vector<int> q(n, -);
for(int i = ; i < n; i++)
{
q[] = i;
helper(ans, q, n, );
}
return ans;
51. N-Queens 52. N-Queens II *HARD*的更多相关文章
- leetcode 51. N皇后 及 52.N皇后 II
51. N皇后 问题描述 n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回所有不同的 n 皇后 ...
- Leetcode之回溯法专题-52. N皇后 II(N-Queens II)
Leetcode之回溯法专题-52. N皇后 II(N-Queens II) 与51题的代码80%一样,只不过52要求解的数量,51求具体解,点击进入51 class Solution { int a ...
- Java实现 LeetCode 52 N皇后 II
52. N皇后 II n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回 n 皇后不同的解决方案 ...
- leetcode 51. N-Queens 、52. N-Queens II
51. N-Queens 使用isValid判断当前的位置是否合法 每次遍历一行,使用queenCol记录之前行的存储位置,一方面是用于判断合法,另一方面可以根据存储结果输出最终的结果 class S ...
- LeetCode(52) N-Queens II
题目 Follow up for N-Queens problem. Now, instead outputting board configurations, return the total nu ...
- [LeetCode] 52. N皇后 II
题目链接 : https://leetcode-cn.com/problems/n-queens-ii/ 题目描述: n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间 ...
- 2019年7-8月Leetcode每日训练日志
2019-08-29 #274 H指数 2019-08-28 #287 寻找重复数 #875 爱吃香蕉的珂珂 #704 二分查找 2019-08-27 #744 寻找比目标字母大的最小字母 #225 ...
- leetcode刷题目录
leetcode刷题目录 1. 两数之和 2. 两数相加 3. 无重复字符的最长子串 4. 寻找两个有序数组的中位数 5. 最长回文子串 6. Z 字形变换 7. 整数反转 8. 字符串转换整数 (a ...
- [Leetcode][Python]52: N-Queens II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 52: N-Queens IIhttps://oj.leetcode.com/ ...
- 52. N-Queens II
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
随机推荐
- c++第十五天
<c++ primer, 5E> 第94页到第99页,笔记: 1.迭代器(iterator):一种比下标访问更通用的访问容器中元素的机制. (并不是所有标准库容器都支持下标访问,<运 ...
- Python3 判断文件和文件夹是否存在、创建文件夹
Python3 判断文件和文件夹是否存在.创建文件夹 python中对文件.文件夹的操作需要涉及到os模块和shutil模块. 创建文件: 1) os.mknod(“test.txt”) 创建空文件 ...
- linux内核分析 第四周
一.系统调用的三层皮 内核态.用户态 Intel x86 CPU有四个权限分级,0-3.Linux只取两种,0是内核态,3是用户态. 0xc0000000以上的空间只能在内核态下访问 0x000000 ...
- Delphi XE5 for Android (一)
Delphi XE5 出来了,支持Android的开发,试用了一下,有几个问题: 1.只支持ARM7的设备,不支持Inter设备.手上刚好有一个华硕K004,很遗憾用不上,只能用手机试了. 2.要支持 ...
- python3+pyqt5 +eric5安装配置
一.大纲内容: 1.预备PC环境: 2.预备安装程序: 2.1.下载Python3.2 2.2.下载PyQt4 2.3.下载Eric5 3.安装配置步骤: 3.1.安装Pyhon3.2 3.2.安装P ...
- Sql 最简单的Sqlserver连接
string name = txtUserName.Text.Trim();//移除用户名前部和后部的空格 string pwd = txtUserPwd.Text.Trim();//移除密码前部和后 ...
- 【jdk源码分析】java.lang.Appendable
1.概述 public interface Appendable 能够被添加 char 序列和值的对象.如果某个类的实例打算接收取自 Formatter 的格式化输出,那么该类必须实现 Appenda ...
- 08_Flume_Selector实践
实践一:replicating selector 1.目标场景 selector将event复制,分发给所有下游节点 2.Flume Agent配置 Agent配置 # Name the compon ...
- hdu 1269 迷宫城堡 最简单的联通图题 kosaraju缩点算法
迷宫城堡 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem Des ...
- ubuntu 14.04 (desktop amd 64) 下载
http://cdimage.ubuntu.com/ubuntukylin/releases/14.04/release/