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. 单片机电平转换电路5V 3.3V串口通讯等(转)

    源: 单片机电平转换电路5V 3.3V串口通讯等

  2. 20145326蔡馨熠《网络对抗》shellcode注入&Return-to-libc攻击深入

    20145326蔡馨熠<网络对抗>shellcode注入&Return-to-libc攻击深入 准备一段shellcode 首先我们应该知道,到底什么是shellcode.经过上网 ...

  3. Educational Codeforces Round 21 Problem D(Codeforces 808D)

    Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into t ...

  4. NOIP Mayan游戏 - 搜索

    Mayan puzzle是最近流行起来的一个游戏.游戏界面是一个7行5列的棋盘,上面堆放着一些方块,方块不能悬空堆放,即方块必须放在最下面一行,或者放在其他方块之上.游戏通关是指在规定的步数内消除所有 ...

  5. 自定义LisetView

    1.ListView listview=findViewById(R.id.listview); 2.public class MyAdapter extends BaseAdapter{ priva ...

  6. python创建MySQL多实例-1

    python创建MySQL多实例-1 前言 什么是多实例 多实例就是允许在同一台机器上创建另外一套不同配置文件的数据库,他们之间是相互独立的,主要有以下特点, 1> 不能同时使用一个端口 2&g ...

  7. ubuntu16.04下内核模块解析

    一.环境如下: 1.1内核版本: jello@jello:~$ uname -a Linux jello 4.4.0-89-generic #112-Ubuntu SMP Mon Jul 31 19: ...

  8. P3901 数列找不同

    P3901 数列找不同 题目描述 现有数列 \(A_1,A_2,\cdots,A_N\) ,Q 个询问 \((L_i,R_i)\) , \(A_{Li} ,A_{Li+1},\cdots,A_{Ri} ...

  9. Future Works on P4

    Future Works on P4 P4 and NV: MPvisor, Hyper4, HyperV, Flex4 P4 and NFV P4 and Network Cache P4 and ...

  10. Axure RP 8.0 Licence

    新版本:(比如 Axure RP 8.0.0 3319)Licensee:米 业成 (STUDENT)Key:nFmqBBvEqdvbiUjy8NZiyWiRSg3yO+PtZ8c9wdwxWse4W ...