package kapitel_11 /** * 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 */ object AuD_11_02_TwoQueensArrangement_App extends App { // Erzeugt alle möglichen Positionen in einen 2x2 Feld def TwoQPositionsV1: List[(Int, Int)] = { var result: List[(Int, Int)] = Nil for (i <- 0 to 1) for (j <- 0 to 1) result = result ++ List((i, j)) result } // Erzeugt alle möglichen Positionen in einen 2x2 Feld // idiomatischere Variante def TwoQPositionsV2: IndexedSeq[(Int, Int)] = for ( i <- 0 to 1; j <- 0 to 1 ) yield (i, j) val TwoQPositions = TwoQPositionsV2 // Erzeugt alle möglichen Paare von Positionen def TwoQSearchSpaceV1: List[((Int, Int), (Int, Int))] = { var result: List[((Int, Int), (Int, Int))] = Nil for (p_0 <- TwoQPositions) for (p_1 <- TwoQPositions) result = result ++ List((p_0, p_1)) result } // Erzeugt 12 Paare von Positionen ohne symetrische Paare def TwoQSearchSpaceV2 = { var result: List[((Int, Int),(Int, Int))] = Nil for (p_0 <- TwoQPositions; p_1 <- TwoQPositions if p_0 != p_1 && ! result.contains((p_1, p_0)) ) { result = result ++ List((p_0, p_1)) } result } println(TwoQSearchSpaceV1) println() println(TwoQSearchSpaceV1) }