/** * 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_02_TwoQueensArrangement { // Erzeugt alle möglichen Positionen in einen 2x2 Feld static func TwoQPositionsV1() -> [(Int, Int)] { var result: [(Int, Int)] = [] for i in 0 ... 1 { for j in 0...1 { result = result + [(i, j)] } } return result } // Erzeugt alle möglichen Positionen in einen 2x2 Feld // funktionale Variante static func TwoQPositionsV2() -> [(Int, Int)] { (0...1).flatMap({ (i) in (0...1).map({ (j) in (i, j) }) }) } static let TwoQPositions = TwoQPositionsV2 // Erzeugt alle möglichen Paare von Positionen static func TwoQSearchSpaceV1() -> [((Int, Int), (Int, Int))] { var result: [((Int, Int), (Int, Int))] = [] for p_0 in TwoQPositions() { for p_1 in TwoQPositions() { result = result + [(p_0, p_1)] } } return result } // Erzeugt Paare von Positionen ohne symetrische Paare static func TwoQSearchSpaceV2() -> [((Int, Int), (Int, Int))] { var result: [((Int, Int),(Int, Int))] = [] for p_0 in TwoQPositions() { for p_1 in TwoQPositions() { if ((p_0 != p_1) && (result.filter( { (pa, pb) in switch (pa, pb) { case ((let x, let y), (let u, let v)) : return p_1.0 == x && p_1.1 == y && p_0.0 == u && p_0.1 == v } } ).count == 0) ) { result = result + [(p_0, p_1)] } } } return result } static func run() { print(TwoQSearchSpaceV1()) print() print(TwoQSearchSpaceV2()) } }