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

  1. leetcode 51. N皇后 及 52.N皇后 II

    51. N皇后 问题描述 n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回所有不同的 n 皇后 ...

  2. Leetcode之回溯法专题-52. N皇后 II(N-Queens II)

    Leetcode之回溯法专题-52. N皇后 II(N-Queens II) 与51题的代码80%一样,只不过52要求解的数量,51求具体解,点击进入51 class Solution { int a ...

  3. Java实现 LeetCode 52 N皇后 II

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

  4. leetcode 51. N-Queens 、52. N-Queens II

    51. N-Queens 使用isValid判断当前的位置是否合法 每次遍历一行,使用queenCol记录之前行的存储位置,一方面是用于判断合法,另一方面可以根据存储结果输出最终的结果 class S ...

  5. LeetCode(52) N-Queens II

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

  6. [LeetCode] 52. N皇后 II

    题目链接 : https://leetcode-cn.com/problems/n-queens-ii/ 题目描述: n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间 ...

  7. 2019年7-8月Leetcode每日训练日志

    2019-08-29 #274 H指数 2019-08-28 #287 寻找重复数 #875 爱吃香蕉的珂珂 #704 二分查找 2019-08-27 #744 寻找比目标字母大的最小字母 #225 ...

  8. leetcode刷题目录

    leetcode刷题目录 1. 两数之和 2. 两数相加 3. 无重复字符的最长子串 4. 寻找两个有序数组的中位数 5. 最长回文子串 6. Z 字形变换 7. 整数反转 8. 字符串转换整数 (a ...

  9. [Leetcode][Python]52: N-Queens II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 52: N-Queens IIhttps://oj.leetcode.com/ ...

  10. 52. N-Queens II

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

随机推荐

  1. 第八篇:支持向量机 (SVM)分类器原理分析与基本应用

    前言 支持向量机,也即SVM,号称分类算法,甚至机器学习界老大哥.其理论优美,发展相对完善,是非常受到推崇的算法. 本文将讲解的SVM基于一种最流行的实现 - 序列最小优化,也即SMO. 另外还将讲解 ...

  2. 20145335郝昊《网络攻防》Exp4 MS11_050

    20145335郝昊<网络攻防>Exp4 MS11_050 实验内容 初步掌握平台matesploit的使用 了解漏洞MS11_050漏洞:use-after-free漏洞,即对象被释放之 ...

  3. AD快捷键

    * 在PCB电气层之间切换.在布线的过程中,按此键则换层并自动添加过孔并换层. Q 在公制和英制之间切换 J+C 定位到指定的元件处.在弹出的对话框内输入该元件的编号. G+G 设定栅格吸附尺寸. T ...

  4. excel在msdn上的说明文档

    Microsoft.Office.Tools.Excel.Worksheet 对象提供和 Excel 主互操作程序集中的 Microsoft.Office.Interop.Excel.Workshee ...

  5. Linux mysql 添加远程连接

    方法/步骤 第一步 远程连接上Linux系统,确保Linux系统已经安装上了MySQL数据库.登陆数据库. mysql -u$user -p $pwd 第二步 创建用户用来远程连接 GRANT ALL ...

  6. shiro的简单入门使用

    这里只是测试登录认证,没有web模块,没有连接数据库,用户密码放在shiro.ini配置中,密码没有加密处理,简单入门. 基于maven 先看目录结构 测试结果 pom.xml <?xml ve ...

  7. C++课程小结 继承与派生

    单继承与多重继承的区别 单继承:一个子类(派生类)只有一个父类(只由一个基类派生而成) 多继承:一个子类(派生类)有多个父类(由多个基类派生而成) 派生类的构成 (1) 从基类继承过来的成员(包括数据 ...

  8. 改变checkbox样式问题

    选择1   选择2 选择3 选择4 选择5 <form action=""> <label for="test">选择1 <inp ...

  9. python 写文件刷新缓存

    搞爬虫的时候,结果是通过file.write(strs)写入文件的. 带来的问题是,进程如果是被杀死的时候,最后一条结果总是缺损的,因为缓存的部分还未写入文件. 解决办法是每次写入文件时,都刷新缓存, ...

  10. .net 与 java 开发微服务对比

    java+spring boot+maven对比.net 优势: 1. spring 自身带的ioc 比.net 更简单易用. 2. spring actuator的健康检测等运行时状态查看功能很赞. ...