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++算法原理与实践(面试中的算法和准备过程)
第0部分 简介 1. 举个例子:面试的时候,可能会出一道算法考试题,比如写一个 strstr 函数——字符串匹配. 可能会想到用KMP算法来解题,但是该算法很复杂,不适宜在面试中使用. 1.1 C++ ...
- 关于编译安装php
之前也装过一次是nginx和php的, 这次也是... but, 不知道是不是版本的问题还是环境方面的影响, 导致之前的gd库是安装失败的, 所以上次安装zabbix也是失败的, 这次换了5.6.2版 ...
- PHP-Iterator迭代器(遍历)接口详讲
echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' /> "; class ...
- 20145306 《网络攻防》 MSF基础应用
20145306张文锦<网络对抗>MSF应用 Adobe阅读器渗透攻击 两台虚拟机,其中一台为kali,一台为windows xp sp3,并保证两台虚拟机可以ping通. 实验过程 进入 ...
- C++ tinyXml直接解析XML字符串
转载:http://www.cnblogs.com/1024Planet/p/4401929.html <?xml version=\"1.0\" encoding=\&qu ...
- Python3基础 str 通过拆分字符串与插入新的内容形成新的字符串
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- Ansible 操作windows
1.主控端安装ansible 1) pip install ansible 2.主控端安装相关的包 pip install http://github.com/diyan/pywi ...
- IEnumerable与IEnumerator
IEnumerable接口 IEnumerable接口:实现该接口的类,表明该类下有可以枚举的元素 public interface IEnumerable { //返回一个实现了IEnumerato ...
- poj 2762 Going from u to v or from v to u? trajan+拓扑
Going from u to v or from v to u? Description In order to make their sons brave, Jiajia and Wind t ...
- java编写编译器和解释器
on 2012-07-14 21:24 Bang 阅读(102) 评论(0) 编辑 收藏 续 第二部分 初始后端实现 框架后端支持编译器和解释器.现在框架抽象类Backend有两个极简版实现,一个 ...