mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-29 04:50:56 +08:00
* Review the EN heading format. * Fix pythontutor headings. * Fix pythontutor headings. * bug fixes * Fix headings in **/summary.md * Revisit the CN-to-EN translation for Python code using Claude-4.5 * Revisit the CN-to-EN translation for Java code using Claude-4.5 * Revisit the CN-to-EN translation for Cpp code using Claude-4.5. * Fix the dictionary. * Fix cpp code translation for the multipart strings. * Translate Go code to English. * Update workflows to test EN code. * Add EN translation for C. * Add EN translation for CSharp. * Add EN translation for Swift. * Trigger the CI check. * Revert. * Update en/hash_map.md * Add the EN version of Dart code. * Add the EN version of Kotlin code. * Add missing code files. * Add the EN version of JavaScript code. * Add the EN version of TypeScript code. * Fix the workflows. * Add the EN version of Ruby code. * Add the EN version of Rust code. * Update the CI check for the English version code. * Update Python CI check. * Fix cmakelists for en/C code. * Fix Ruby comments
68 lines
2.4 KiB
Swift
68 lines
2.4 KiB
Swift
/**
|
|
* File: n_queens.swift
|
|
* Created Time: 2023-05-14
|
|
* Author: nuomi1 (nuomi1@qq.com)
|
|
*/
|
|
|
|
/* Backtracking algorithm: N queens */
|
|
func backtrack(row: Int, n: Int, state: inout [[String]], res: inout [[[String]]], cols: inout [Bool], diags1: inout [Bool], diags2: inout [Bool]) {
|
|
// When all rows are placed, record the solution
|
|
if row == n {
|
|
res.append(state)
|
|
return
|
|
}
|
|
// Traverse all columns
|
|
for col in 0 ..< n {
|
|
// Calculate the main diagonal and anti-diagonal corresponding to this cell
|
|
let diag1 = row - col + n - 1
|
|
let diag2 = row + col
|
|
// Pruning: do not allow queens to exist in the column, main diagonal, and anti-diagonal of this cell
|
|
if !cols[col] && !diags1[diag1] && !diags2[diag2] {
|
|
// Attempt: place the queen in this cell
|
|
state[row][col] = "Q"
|
|
cols[col] = true
|
|
diags1[diag1] = true
|
|
diags2[diag2] = true
|
|
// Place the next row
|
|
backtrack(row: row + 1, n: n, state: &state, res: &res, cols: &cols, diags1: &diags1, diags2: &diags2)
|
|
// Backtrack: restore this cell to an empty cell
|
|
state[row][col] = "#"
|
|
cols[col] = false
|
|
diags1[diag1] = false
|
|
diags2[diag2] = false
|
|
}
|
|
}
|
|
}
|
|
|
|
/* Solve N queens */
|
|
func nQueens(n: Int) -> [[[String]]] {
|
|
// Initialize an n*n chessboard, where 'Q' represents a queen and '#' represents an empty cell
|
|
var state = Array(repeating: Array(repeating: "#", count: n), count: n)
|
|
var cols = Array(repeating: false, count: n) // Record whether there is a queen in the column
|
|
var diags1 = Array(repeating: false, count: 2 * n - 1) // Record whether there is a queen on the main diagonal
|
|
var diags2 = Array(repeating: false, count: 2 * n - 1) // Record whether there is a queen on the anti-diagonal
|
|
var res: [[[String]]] = []
|
|
|
|
backtrack(row: 0, n: n, state: &state, res: &res, cols: &cols, diags1: &diags1, diags2: &diags2)
|
|
|
|
return res
|
|
}
|
|
|
|
@main
|
|
enum NQueens {
|
|
/* Driver Code */
|
|
static func main() {
|
|
let n = 4
|
|
let res = nQueens(n: n)
|
|
|
|
print("Input board size is \(n)")
|
|
print("Total queen placement solutions: \(res.count)")
|
|
for state in res {
|
|
print("--------------------")
|
|
for row in state {
|
|
print(row)
|
|
}
|
|
}
|
|
}
|
|
}
|