1/* 2 * Queen.java 3 * This file is part of JaCoP. 4 * <p> 5 * JaCoP is a Java Constraint Programming solver. 6 * <p> 7 * Copyright (C) 2000-2008 Krzysztof Kuchcinski and Radoslaw Szymanek 8 * <p> 9 * This program is free software: you can redistribute it and/or modify 10 * it under the terms of the GNU Affero General Public License as published by 11 * the Free Software Foundation, either version 3 of the License, or 12 * (at your option) any later version. 13 * <p> 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU Affero General Public License for more details. 18 * <p> 19 * Notwithstanding any other provision of this License, the copyright 20 * owners of this work supplement the terms of this License with terms 21 * prohibiting misrepresentation of the origin of this work and requiring 22 * that modified versions of this work be marked in reasonable ways as 23 * different from the original version. This supplement of the license 24 * terms is in accordance with Section 7 of GNU Affero General Public 25 * License version 3. 26 * <p> 27 * You should have received a copy of the GNU Affero General Public License 28 * along with this program. If not, see <http://www.gnu.org/licenses/>. 29 */ 30 31package org.jacop.examples.scala 32 33import org.jacop.scala._ 34 35/** 36 * A problem defined as in Java based examples. 37 * 38 * rewriting to Scala by Krzysztof Kuchcinski. 39 * 40 * @author Krzysztof Kuchcinski and Radoslaw Szymanek 41 * @version 4.5 42 */ 43object Queen extends App with jacop { 44 45 val n = 50 46 47 val q: List[IntVar] = for (i <- List.range(0, n)) yield new IntVar("q" + i, 0, n) 48 49 def noattack(i: Int, j: Int, qi: IntVar, qj: IntVar) = { 50 qi #\= qj 51 qi + i #\= qj + j 52 qi - i #\= qj - j 53 } 54 55 for (i <- 0 until n; j <- i + 1 until n) noattack(i, j, q(i), q(j)) 56 57 val result = satisfy(search(q, first_fail, indomain_middle)) 58 59 if (result) 60 q.foreach(qi => { 61 for (i <- 0 until n) 62 if (qi.value() == i) print(" # ") else print(" . ") 63 println() 64 65 }) 66 else println("No solution") 67} 68