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 ...
随机推荐
- Node-webkit 安装使用npm安装模块方法
原文链接:http://jingyan.baidu.com/article/5225f26b5aaa20e6fa0908a6.html package.json可以放在软件根目录下,也可以放在项目目录 ...
- 20145336《网络对抗技术》Exp6 信息搜集技术
20145336张子扬 <网络对抗技术> 信息搜集与漏洞扫描 实验内容 使用whois进行域名注册信息查询,使用nslookup进行域名查询 实现对IP地理位置的查询 使用PING.nam ...
- 《Python程序设计(第3版)》[美] 约翰·策勒(John Zelle) 第 2 章 答案
判断对错1.编写程序的好方法是立即键入一些代码,然后调试它,直到它工作.2.可以在不使用编程语言的情况下编写算法.3.程序在写入和调试后不再需要修改.4.Python 标识符必须以字母或下划线开头.5 ...
- 在一个activity中销毁指定activity
通过静态变量的方法: 1.在Aactivity中设置一个Activity静态变量 static Activity activity; 2.在onCreate中: activity=this: 3.在B ...
- python2.7+pyqt4 +eric4安装配置
eric4安装与汉化一直没找到合适python的IDE工具,直到遇到了eric4这款开源软件.然而在使用过程中发现输出的中文字符竟然是乱码,修修改改配置总算正常显示了,何不干脆把软件界面也汉化下. 一 ...
- ubuntu下桌面假死处理方法(非重启)
一.背景 2018/05/22,就在这一天,进入ubuntu的桌面后随便点击任何位置均无法响应,此时又不想重启,遂出此文 二.解决方案 2.1 关掉Xorg进程 2.1.1按下ctrl+alt+F1进 ...
- SpringBoot中使用mybatis-generator自动生产
步骤: 1.在pom.xml中添加插件配置 <plugin> <groupId>org.mybatis.generator</groupId> <artifa ...
- BZOJ1966: [Ahoi2005]VIRUS 病毒检测 Trie+搜索
Description 科学家们在Samuel星球上的探险仍在继续.非常幸运的,在Samuel星球的南极附近,探险机器人发现了一个巨大的冰湖!机器人在这个冰湖中搜集到了许多RNA片段运回了实验基地.科 ...
- 用python + hadoop streaming 编写分布式程序(三) -- 自定义功能
又是期末又是实训TA的事耽搁了好久……先把写好的放上博客吧 相关随笔: Hadoop-1.0.4集群搭建笔记 用python + hadoop streaming 编写分布式程序(一) -- 原理介绍 ...
- 并发队列ConcurrentLinkedQueue、阻塞队列AraayBlockingQueue、阻塞队列LinkedBlockingQueue 区别和使用场景总结
三者区别与联系: 联系,三者 都是线程安全的.区别,就是 并发 和 阻塞,前者为并发队列,因为采用cas算法,所以能够高并发的处理:后2者采用锁机制,所以是阻塞的.注意点就是前者由于采用cas算 ...