当前位置:网站首页>LeetCode_52_N皇后Ⅱ
LeetCode_52_N皇后Ⅱ
2022-07-23 07:02:00 【Fitz1318】
题目链接
题目描述
按照国际象棋的规则,皇后可以攻击与之处在同一行或同一列或同一斜线上的棋子。
n 皇后问题 研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。
给你一个整数 n ,返回所有不同的 n 皇后问题 的解决方案的数量
示例 1:

输入:n = 4
输出:2
解释:如上图所示,4 皇后问题存在两个不同的解法。
示例 2:
输入:n = 1
输出:1
提示:
1 <= n <= 9
解题思路
直接使用N皇后的代码,然后返回数量
AC代码
class Solution {
public int totalNQueens(int n) {
return solveNQueens(n).size();
}
public static List<List<String>> solveNQueens(int n) {
List<List<String>> ans = new ArrayList<>();
char[][] chess = new char[n][n];
for (char[] c : chess) {
Arrays.fill(c, '.');
}
backTracing(n, 0, chess, ans);
return ans;
}
private static void backTracing(int n, int row, char[][] chess, List<List<String>> ans) {
if (row == n) {
ans.add(Array2List(chess));
return;
}
for (int col = 0; col < n; col++) {
if (isValid(row, col, n, chess)) {
chess[row][col] = 'Q';
backTracing(n, row + 1, chess, ans);
chess[row][col] = '.';
}
}
}
private static List<String> Array2List(char[][] chess) {
List<String> list = new ArrayList<>();
for (char[] c : chess) {
list.add(String.copyValueOf(c));
}
return list;
}
private static boolean isValid(int row, int col, int n, char[][] chess) {
//检查列
for (int i = 0; i < row; i++) {
if (chess[i][col] == 'Q') {
return false;
}
}
//检查45度对角线
for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {
if (chess[i][j] == 'Q') {
return false;
}
}
//检查135度对角线
for (int i = row - 1, j = col + 1; i >= 0 && j < n; i--, j++) {
if (chess[i][j] == 'Q') {
return false;
}
}
return true;
}
}
边栏推荐
- 【可视化调度软件】上海道宁为SMB组织带来NETRONIC下载、试用、教程
- 图形管线(一)后处理阶段 alpha测试 模版测试 深度测试 混合
- High school Chinese teaching material examination outline
- 养老机构智能视频监控解决方案,用新技术助力养老院智慧监管
- ModuleNotFoundError: No module named ‘setuptools_rust‘
- 高性能JVM的参数
- 数据库系统原理与应用教程(038)—— MySQL 的索引(四):使用 EXPLAIN 命令分析索引
- ROS2自学笔记:URDF机器人建模
- Smart canteen data analysis system
- 数据库系统原理与应用教程(050)—— MySQL 查询(十二):SELECT 命令的执行过程分析
猜你喜欢

Charles' bag grabbing tool test practice

4D antenna array layout design

2022暑假软件创新实验室集训 项目实战1

Smart city infrastructure management based on bim+3dgis

Why choose AMD epyc for cloud solutions?

深入解读 EVM 的生态帝国

Chapter II relational database after class exercises

Interface test - simple interface automation test demo

Light chain dissection / tree chain dissection

keepalived双机热备
随机推荐
Client does not support authentication protocol requested by server; consider upgrading MySQL client
Interface test - simple interface automation test demo
养老机构智能视频监控解决方案,用新技术助力养老院智慧监管
面试官:有了解过ReentrantLock的底层实现吗?说说看
2. Les règles quantitatives
【cocos creator】spine动画,监听播放结束
Backtracking method to solve the eight queens problem
Wechat applet -- dynamically set the navigation bar color
Vs2019:constexpr function "qcountleadingzerobits" cannot generate constant expressions
数据库系统原理与应用教程(039)—— MySQL 查询(一):SELECT 命令的语法分析
Point target simulation of SAR imaging (III) -- Analysis of simulation results
Why choose AMD epyc for cloud solutions?
Probability meditation: 2. The quantitative rules
Special lecture 5 combinatorial mathematics learning experience (long-term update)
Jupyter notebook add existing virtual environment
Hardware system architecture of 4D millimeter wave radar
PHP获取当前时间戳三位毫秒 - 毫秒时间戳
【JS高级】正则入门基础—关于你想知道的正则表达式_01
Knowledge map: basic concepts
Introduction to radar part vii 3 formation and processing of SAR image