Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@@ -7,7 +7,7 @@
|
||||
|
||||
# 第51题. N皇后
|
||||
|
||||
[力扣题目链接](https://leetcode-cn.com/problems/n-queens/)
|
||||
[力扣题目链接](https://leetcode.cn/problems/n-queens/)
|
||||
|
||||
n 皇后问题 研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。
|
||||
|
||||
@@ -129,7 +129,6 @@ for (int col = 0; col < n; col++) {
|
||||
|
||||
```CPP
|
||||
bool isValid(int row, int col, vector<string>& chessboard, int n) {
|
||||
int count = 0;
|
||||
// 检查列
|
||||
for (int i = 0; i < row; i++) { // 这是一个剪枝
|
||||
if (chessboard[i][col] == 'Q') {
|
||||
@@ -178,7 +177,6 @@ void backtracking(int n, int row, vector<string>& chessboard) {
|
||||
}
|
||||
}
|
||||
bool isValid(int row, int col, vector<string>& chessboard, int n) {
|
||||
int count = 0;
|
||||
// 检查列
|
||||
for (int i = 0; i < row; i++) { // 这是一个剪枝
|
||||
if (chessboard[i][col] == 'Q') {
|
||||
@@ -458,6 +456,58 @@ var solveNQueens = function(n) {
|
||||
};
|
||||
```
|
||||
|
||||
### TypeScript
|
||||
|
||||
```typescript
|
||||
function solveNQueens(n: number): string[][] {
|
||||
const board: string[][] = new Array(n).fill(0).map(_ => new Array(n).fill('.'));
|
||||
const resArr: string[][] = [];
|
||||
backTracking(n, 0, board);
|
||||
return resArr;
|
||||
function backTracking(n: number, rowNum: number, board: string[][]): void {
|
||||
if (rowNum === n) {
|
||||
resArr.push(transformBoard(board));
|
||||
return;
|
||||
}
|
||||
for (let i = 0; i < n; i++) {
|
||||
if (isValid(i, rowNum, board) === true) {
|
||||
board[rowNum][i] = 'Q';
|
||||
backTracking(n, rowNum + 1, board);
|
||||
board[rowNum][i] = '.';
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
function isValid(col: number, row: number, board: string[][]): boolean {
|
||||
const n: number = board.length;
|
||||
if (col < 0 || col >= n || row < 0 || row >= n) return false;
|
||||
// 检查列
|
||||
for (let row of board) {
|
||||
if (row[col] === 'Q') return false;
|
||||
}
|
||||
// 检查45度方向
|
||||
let x: number = col,
|
||||
y: number = row;
|
||||
while (y >= 0 && x < n) {
|
||||
if (board[y--][x++] === 'Q') return false;
|
||||
}
|
||||
// 检查135度方向
|
||||
x = col;
|
||||
y = row;
|
||||
while (x >= 0 && y >= 0) {
|
||||
if (board[y--][x--] === 'Q') return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
function transformBoard(board: string[][]): string[] {
|
||||
const resArr = [];
|
||||
for (let row of board) {
|
||||
resArr.push(row.join(''));
|
||||
}
|
||||
return resArr;
|
||||
}
|
||||
```
|
||||
|
||||
### Swift
|
||||
|
||||
```swift
|
||||
@@ -509,6 +559,56 @@ func solveNQueens(_ n: Int) -> [[String]] {
|
||||
}
|
||||
```
|
||||
|
||||
### Rust
|
||||
|
||||
```Rust
|
||||
impl Solution {
|
||||
fn is_valid(row: usize, col: usize, chessboard: &mut Vec<Vec<char>>, n: usize) -> bool {
|
||||
let mut i = 0 as usize;
|
||||
while i < row {
|
||||
if chessboard[i][col] == 'Q' { return false; }
|
||||
i += 1;
|
||||
}
|
||||
let (mut i, mut j) = (row as i32 - 1, col as i32 - 1);
|
||||
while i >= 0 && j >= 0 {
|
||||
if chessboard[i as usize][j as usize] == 'Q' { return false; }
|
||||
i -= 1;
|
||||
j -= 1;
|
||||
}
|
||||
let (mut i, mut j) = (row as i32 - 1, col as i32 + 1);
|
||||
while i >= 0 && j < n as i32 {
|
||||
if chessboard[i as usize][j as usize] == 'Q' { return false; }
|
||||
i -= 1;
|
||||
j += 1;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
fn backtracking(result: &mut Vec<Vec<String>>, n: usize, row: usize, chessboard: &mut Vec<Vec<char>>) {
|
||||
if row == n {
|
||||
let mut chessboard_clone: Vec<String> = Vec::new();
|
||||
for i in chessboard {
|
||||
chessboard_clone.push(i.iter().collect::<String>());
|
||||
}
|
||||
result.push(chessboard_clone);
|
||||
return;
|
||||
}
|
||||
for col in 0..n {
|
||||
if Self::is_valid(row, col, chessboard, n) {
|
||||
chessboard[row][col] = 'Q';
|
||||
Self::backtracking(result, n, row + 1, chessboard);
|
||||
chessboard[row][col] = '.';
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn solve_n_queens(n: i32) -> Vec<Vec<String>> {
|
||||
let mut result: Vec<Vec<String>> = Vec::new();
|
||||
let mut chessboard: Vec<Vec<char>> = vec![vec!['.'; n as usize]; n as usize];
|
||||
Self::backtracking(&mut result, n as usize, 0, &mut chessboard);
|
||||
result
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### C
|
||||
```c
|
||||
char ***ans;
|
||||
@@ -634,5 +734,77 @@ char *** solveNQueens(int n, int* returnSize, int** returnColumnSizes){
|
||||
}
|
||||
```
|
||||
|
||||
### Scala
|
||||
|
||||
```scala
|
||||
object Solution {
|
||||
import scala.collection.mutable
|
||||
def solveNQueens(n: Int): List[List[String]] = {
|
||||
var result = mutable.ListBuffer[List[String]]()
|
||||
|
||||
def judge(x: Int, y: Int, maze: Array[Array[Boolean]]): Boolean = {
|
||||
// 正上方
|
||||
var xx = x
|
||||
while (xx >= 0) {
|
||||
if (maze(xx)(y)) return false
|
||||
xx -= 1
|
||||
}
|
||||
// 左边
|
||||
var yy = y
|
||||
while (yy >= 0) {
|
||||
if (maze(x)(yy)) return false
|
||||
yy -= 1
|
||||
}
|
||||
// 左上方
|
||||
xx = x
|
||||
yy = y
|
||||
while (xx >= 0 && yy >= 0) {
|
||||
if (maze(xx)(yy)) return false
|
||||
xx -= 1
|
||||
yy -= 1
|
||||
}
|
||||
xx = x
|
||||
yy = y
|
||||
// 右上方
|
||||
while (xx >= 0 && yy < n) {
|
||||
if (maze(xx)(yy)) return false
|
||||
xx -= 1
|
||||
yy += 1
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
def backtracking(row: Int, maze: Array[Array[Boolean]]): Unit = {
|
||||
if (row == n) {
|
||||
// 将结果转换为题目所需要的形式
|
||||
var path = mutable.ListBuffer[String]()
|
||||
for (x <- maze) {
|
||||
var tmp = mutable.ListBuffer[String]()
|
||||
for (y <- x) {
|
||||
if (y == true) tmp.append("Q")
|
||||
else tmp.append(".")
|
||||
}
|
||||
path.append(tmp.mkString)
|
||||
}
|
||||
result.append(path.toList)
|
||||
return
|
||||
}
|
||||
|
||||
for (j <- 0 until n) {
|
||||
// 判断这个位置是否可以放置皇后
|
||||
if (judge(row, j, maze)) {
|
||||
maze(row)(j) = true
|
||||
backtracking(row + 1, maze)
|
||||
maze(row)(j) = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
backtracking(0, Array.ofDim[Boolean](n, n))
|
||||
result.toList
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
-----------------------
|
||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||
|
||||
Reference in New Issue
Block a user