52. N皇后 II

n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。

上图为 8 皇后问题的一种解法。

给定一个整数 n,返回 n 皇后不同的解决方案的数量。

示例:

输入: 4

输出: 2

解释: 4 皇后问题存在如下两个不同的解法。

[

[".Q…", // 解法 1

“…Q”,

“Q…”,

“…Q.”],

["…Q.", // 解法 2

“Q…”,

“…Q”,

“.Q…”]

]

class Solution {

    /**
* 记录某列是否已有皇后摆放
*/
private boolean col[]; /**
* 记录某条正对角线(左上右下)是否已有皇后摆放(某条对角线对应的摆放位置为 x - y + n - 1)
*/
private boolean dia1[]; /**
* 记录某条斜对角线(左下右上)是否已有皇后摆放(某条对角线对应的摆放位置为 x + y)
*/
private boolean dia2[]; public int totalNQueens(int n) {
// 依然可以使用 51 号问题的解决思路,但问题是有没有更好的方法
col = new boolean[n];
dia1 = new boolean[2 * n - 1];
dia2 = new boolean[2 * n - 1];
return putQueen(n, 0);
} /**
* 递归回溯方式摆放皇后
*
* @param n 待摆放皇后个数
* @param index 已摆放皇后个数
*/
private int putQueen(int n, int index) {
int res = 0;
if (index == n) {
return 1;
}
// 表示在 index 行的第 i 列尝试摆放皇后
for (int i = 0; i < n; i++) {
if (!col[i] && !dia1[i - index + n - 1] && !dia2[i + index]) {
// 递归
col[i] = true;
dia1[i - index + n - 1] = true;
dia2[i + index] = true;
res += putQueen(n, index + 1);
// 回溯
col[i] = false;
dia1[i - index + n - 1] = false;
dia2[i + index] = false;
}
}
return res;
} }

Java实现 LeetCode 52 N皇后 II的更多相关文章

  1. [LeetCode] 52. N皇后 II

    题目链接 : https://leetcode-cn.com/problems/n-queens-ii/ 题目描述: n 皇后问题研究的是如何将 n 个皇后放置在 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. leetcode 51. N皇后 及 52.N皇后 II

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

  4. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  5. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  6. Java for LeetCode 059 Spiral Matrix II

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  7. Java for LeetCode 126 Word Ladder II 【HARD】

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  8. leetcode 52 N皇后问题 II

    51的简化版,省去根据排列话棋盘的工作,直接计数,代码: class Solution { public: int totalNQueens(int n) { ; vector<); dfs(n ...

  9. Java实现 LeetCode 63 不同路径 II(二)

    63. 不同路径 II 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为"Start" ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在 ...

随机推荐

  1. Properties集合与IO流

    package com.itheima.demo07.Prop; import java.io.FileOutputStream; import java.io.FileReader; import ...

  2. WIn7系统下配置Java环境变量

    给个官网下载地址  :https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 1.首先 ...

  3. 通过 docker images 获取 Dockerfile

    通过docker image 获取到 dockerfile docker history --format {{.CreatedBy}} --no-trunc=true $DockerImage |s ...

  4. vue 路由钩子。

    一.全局钩子 你可以使用 router.beforeEach 注册一个全局的 before 钩子: const router = new VueRouter({ ... }) router.befor ...

  5. STM32学习笔记——USART

    STM32的USART组件支持异步.同步.单线半双工.多处理器.IrDA.LIN.SmartCard等模式,本文介绍的是异步即UART模式. 总线通信有三种模型:轮询.中断和DMA.DMA对我来说是陌 ...

  6. java 版本比较

    public class version { public static int compareVersion(String version1, String version2) throws Exc ...

  7. Centos 7 下自启动服务配置

    在服务器部署服务后,往往需要将服务设置成开机自启的状态 ,以防设备出现宕机或断电重启,服务无法访问的情况. 对于常见的服务(httpd,mysqld,nginx)来说,可通过系统 systemctl ...

  8. 剑指Offer02之替换空格

    剑指Offer02之替换空格 题目描述 实现一个方法,将输入的字符串中的空格替换成%20. 例子如下 hello world --> hello%20world 代码实现 //方法一 采用Jav ...

  9. 3.4 Go字符型

    1. Go字符型 Golang 中没有专门的字符类型,如果要存储单个字符(字母),一般使用 byte 来保存. 普通字符串就是一串固定长度的字符连接起来的字符序列. Go 的字符串是由单个字节连接起来 ...

  10. BZOJ1059 二分匹配

    1059: [ZJOI2007]矩阵游戏 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4810  Solved: 2297[Submit][Stat ...