/** * Beispiel aus * * - Algorithmen und Datenstrukturen für Dummies * - von Andreas Gogol-Döring und Thomas Letschert * - Verlag Wiley-VCH; Oktober 2019 * - Kapitel 11, Probleme totschlagen * * @author A. Gogol-Döring, Th. Letschert */ import Foundation struct AuD_11_03_EightQueensExhaustiveSearch { static func Ok(_ board: [Int]) -> Bool { for i in 0 ..< board.count { for j in i + 1 ..< board.count { let x = board[i] let y = board[j] let d = j - i if (x == y || y == x - d || y == x + d) { return false } } } return true } /* * Es lebe die Kraft der geschachtelten Schleifen. */ static func EightQueens() -> [Int] { for p0 in 0 ..< 8 { for p1 in 0 ..< 8 { for p2 in 0 ..< 8 { for p3 in 0 ..< 8 { for p4 in 0 ..< 8 { for p5 in 0 ..< 8 { for p6 in 0 ..< 8 { for p7 in 0 ..< 8 { if Ok([p0, p1, p2, p3, p4, p5, p6, p7]) { return [p0, p1, p2, p3, p4, p5, p6, p7] } } } } } } } } } return [] // keine Loesung } static func run() { print(EightQueens()) } }