progs/knight.scala
changeset 9 48a477fdef21
parent 3 575a5c07c7fe
equal deleted inserted replaced
8:ab77f6006f1f 9:48a477fdef21
    41 // non-circle tour
    41 // non-circle tour
    42 def ctour(n: Int)(steps: List[(Int, Int)]): Unit = {
    42 def ctour(n: Int)(steps: List[(Int, Int)]): Unit = {
    43   if (steps.length ==  n * n && moves(n)(steps.head).contains(steps.last))
    43   if (steps.length ==  n * n && moves(n)(steps.head).contains(steps.last))
    44     print_board(n)(steps)
    44     print_board(n)(steps)
    45   else 
    45   else 
    46     for (x <- moves(n)(steps.head).par; if (!steps.contains(x))) ctour(n)(x :: steps)
    46     for (x <- moves(n)(steps.head); if (!steps.contains(x))) ctour(n)(x :: steps)
    47 }
    47 }
    48 
    48 
    49 
    49 
    50 def faster_tour(n: Int)(steps: List[(Int, Int)]): Unit = {
    50 def faster_tour(n: Int)(steps: List[(Int, Int)]): Unit = {
    51   if (steps.length ==  n * n && moves(n)(steps.head).contains(steps.last))
    51   if (steps.length ==  n * n && moves(n)(steps.head).contains(steps.last))
    52     print_board(n)(steps)
    52     print_board(n)(steps)
    53   else 
    53   else 
    54     for (x <- ordered_moves(n)(steps)(steps.head).par; if (!steps.contains(x))) faster_tour(n)(x :: steps)
    54     for (x <- ordered_moves(n)(steps)(steps.head); if (!steps.contains(x))) faster_tour(n)(x :: steps)
    55 }
    55 }
    56 
    56 
    57 
    57 
    58 val n1 = 5
    58 val n1 = 5
    59 println(s"simple tour: n = $n1")
    59 println(s"simple tour: n = $n1")
    63     tour(n1)(List((i, j))) 
    63     tour(n1)(List((i, j))) 
    64   }
    64   }
    65 }
    65 }
    66 
    66 
    67 
    67 
    68 /*
       
    69 val n2 = 6
    68 val n2 = 6
    70 println(s"circle tour: n = $n2")
    69 println(s"circle tour: n = $n2")
    71 
    70 
    72 Try {
    71 Try {
    73   for (i <- 0 until n2; j <- 0 until n2) {
    72   for (i <- 0 until n2; j <- 0 until n2) {
    74     ctour(n2)(List((i, j))) 
    73     ctour(n2)(List((i, j))) 
    75   }
    74   }
    76 }
    75 }
    77 */
       
    78 
    76 
    79 val n3 = 9
    77 
       
    78 val n3 = 8
    80 println(s"fast circle tour: n = $n3")
    79 println(s"fast circle tour: n = $n3")
    81 
    80 
    82 Try {
    81 Try {
    83   for (i <- 0 until n3; j <- 0 until n3) {
    82   for (i <- 0 until n3; j <- 0 until n3) {
    84     faster_tour(n3)(List((i, j))) 
    83     faster_tour(n3)(List((i, j)))