当前位置:网站首页>LeetCode_ 51_ Queen n
LeetCode_ 51_ Queen n
2022-07-23 13:51:00 【Fitz1318】
Topic link
Title Description
According to the rules of chess , The queen can attack the pieces on the same row, column or diagonal line .
n queens problem It's about how to make n A queen is placed in n×n On the chessboard , And keep the Queens from attacking each other .
Give you an integer n , Go back to all the different n queens problem Solutions for .
Each solution contains a different n queens problem The chess piece placement scheme of , The scenario 'Q' and '.' They represent the queen and the vacant seat .
Example 1:

Input :n = 4
Output :[[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]
explain : As shown in the figure above ,4 There are two different solutions to the queen problem .
Example 2:
Input :n = 1
Output :[["Q"]]
Tips :
1 <= n <= 9
Their thinking
backtracking
There are three main restrictions on placing queens
- I can't go with you
- Not in the same column
- Cannot be the same slash
Let's say 3 * 3 Chessboard of , Explain the use of backtracking

The retrospective trilogy
- Recursive function parameters
n: Chessboard sizerow: Record the current level of traversal to the chessboardpath: Save qualified resultsans: Save a collection of results
- Recursive termination condition
- Find the leaf node
- Single layer logic
- The depth of recursion is
rowControl the line of the chessboard , Every floor for CycliccolControl the columns of the chessboard , Line by line , Determined the position to prevent the queen - Search every time from the beginning of a new line , So it's all from
0Start
- The depth of recursion is
Verify whether the chessboard is legal
- I can't go with you
- Not in the same column
- It can't be diagonal , Include 45 Degree and 135 Degree angle
AC Code
class Solution {
public 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 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 List<String> Array2List(char[][] chess) {
List<String> list = new ArrayList<>();
for (char[] c : chess) {
list.add(String.copyValueOf(c));
}
return list;
}
private boolean isValid(int row, int col, int n, char[][] chess) {
// Check column
for (int i = 0; i < row; i++) {
if (chess[i][col] == 'Q') {
return false;
}
}
// Check 45 Degree diagonal
for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {
if (chess[i][j] == 'Q') {
return false;
}
}
// Check 135 Degree diagonal
for (int i = row - 1, j = col + 1; i >= 0 && j < n; i--, j++) {
if (chess[i][j] == 'Q') {
return false;
}
}
return true;
}
}
边栏推荐
- LeetCode_491_递增子序列
- 微服务重点
- ModuleNotFoundError: No module named ‘setuptools_ rust‘
- QNX modify system time
- C#做一个简单浏览器
- 数据库系统原理与应用教程(042)—— MySQL 查询(四):使用通配符构造查询条件
- Learn to use canvas to build line chart, bar chart and pie chart
- 图像处理4:腐蚀
- 2. Les règles quantitatives
- Establish stm32f103c8t6 project template and STM32 st-link utility burn hex file
猜你喜欢
随机推荐
General contents of radar introduction column
Research on hardware architecture of Ti single chip millimeter wave radar xwr1642
IP地址分类及范围
Interviewer: have you learned about the underlying implementation of reentrantlock? tell us your opinion
C # make a simple browser
Convergence of abnormal integral
在虚拟环境下使用pip时默认使用系统环境的pip该怎么办
-20: +usecgroupmemorylimitforheap failed to create virtual machine problem
关于#redis#的问题:Redis设置数据持久化之后还是会有丢失数据的风险
[record] golang cross platform compilation
【JS高级】正则入门基础—关于你想知道的正则表达式_01
IP address classification and range
C#做一个简单浏览器
概率沉思录:2.The quantitative rules
[graphics] ASTC texture compression format
做测试如何应对新的开发模式?
数据库系统原理与应用教程(041)—— MySQL 查询(三):设置查询条件
KingbaseESV8R6不同隔离级下xmin的区别
CSDN推荐模板
同花顺开户风险性大吗,安全吗?









