progs/knight.scala
changeset 3 575a5c07c7fe
parent 2 e0ec73c462c7
child 9 48a477fdef21
equal deleted inserted replaced
2:e0ec73c462c7 3:575a5c07c7fe
     1 
     1 import scala.util._
     2 
     2 
     3 def print_board(n: Int)(steps: List[(Int, Int)]): Unit = {
     3 def print_board(n: Int)(steps: List[(Int, Int)]): Unit = {
     4   for (i <- 0 until n) {
     4   for (i <- 0 until n) {
     5     for (j <- 0 until n) {
     5     for (j <- 0 until n) {
     6       print(f"${steps.indexOf((i, j))}%2.0f ")
     6       print(f"${steps.indexOf((i, j))}%3.0f ")
     7     }
     7     }
     8     println
     8     println
     9   } 
     9   } 
    10   //readLine()
    10   //readLine()
    11   System.exit(0)
    11   //System.exit(0)
       
    12   throw new Exception("\n")
    12 }
    13 }
    13 
    14 
    14 def add_pair(x: (Int, Int))(y: (Int, Int)) = 
    15 def add_pair(x: (Int, Int))(y: (Int, Int)) = 
    15   (x._1 + y._1, x._2 + y._2)
    16   (x._1 + y._1, x._2 + y._2)
    16 
    17 
    20 def moves(n: Int)(x: (Int, Int)): List[(Int, Int)] = {
    21 def moves(n: Int)(x: (Int, Int)): List[(Int, Int)] = {
    21   List((1, 2),(2, 1),(2, -1),(1, -2),
    22   List((1, 2),(2, 1),(2, -1),(1, -2),
    22        (-1, -2),(-2, -1),(-2, 1),(-1, 2)).map(add_pair(x)).filter(is_legal(n))
    23        (-1, -2),(-2, -1),(-2, 1),(-1, 2)).map(add_pair(x)).filter(is_legal(n))
    23 }
    24 }
    24 
    25 
       
    26 def ordered_moves(n: Int)(steps: List[(Int, Int)])(x : (Int, Int)): List[(Int, Int)] = 
       
    27   moves(n)(x).sortBy((x: (Int, Int)) => moves(n)(x).filterNot(steps.contains(_)).length)
       
    28 
       
    29 moves(8)(1,3)
       
    30 ordered_moves(8)(Nil)(1,3)
       
    31 ordered_moves(8)(List((2, 4), (2, 6)))(1,3)
       
    32 
       
    33 // non-circle tour parallel
    25 def tour(n: Int)(steps: List[(Int, Int)]): Unit = {
    34 def tour(n: Int)(steps: List[(Int, Int)]): Unit = {
    26   if (steps.length ==  n * n && moves(n)(steps.head).contains(steps.last))
    35   if (steps.length ==  n * n)
    27     print_board(n)(steps)
    36     print_board(n)(steps)
    28   else 
    37   else 
    29     for (x <- moves(n)(steps.head); if (!steps.contains(x))) tour(n)(x :: steps)
    38     for (x <- moves(n)(steps.head); if (!steps.contains(x))) tour(n)(x :: steps)
    30 }
    39 }
    31 
    40 
    32 val n = 6
    41 // non-circle tour
       
    42 def ctour(n: Int)(steps: List[(Int, Int)]): Unit = {
       
    43   if (steps.length ==  n * n && moves(n)(steps.head).contains(steps.last))
       
    44     print_board(n)(steps)
       
    45   else 
       
    46     for (x <- moves(n)(steps.head).par; if (!steps.contains(x))) ctour(n)(x :: steps)
       
    47 }
    33 
    48 
    34 println("started")
    49 
    35 for (i <- 0 until n; j <- 0 until n) tour(n)(List((i, j)))
    50 def faster_tour(n: Int)(steps: List[(Int, Int)]): Unit = {
       
    51   if (steps.length ==  n * n && moves(n)(steps.head).contains(steps.last))
       
    52     print_board(n)(steps)
       
    53   else 
       
    54     for (x <- ordered_moves(n)(steps)(steps.head).par; if (!steps.contains(x))) faster_tour(n)(x :: steps)
       
    55 }
       
    56 
       
    57 
       
    58 val n1 = 5
       
    59 println(s"simple tour: n = $n1")
       
    60 
       
    61 Try {
       
    62   for (i <- 0 until n1; j <- 0 until n1) {
       
    63     tour(n1)(List((i, j))) 
       
    64   }
       
    65 }
       
    66 
       
    67 
       
    68 /*
       
    69 val n2 = 6
       
    70 println(s"circle tour: n = $n2")
       
    71 
       
    72 Try {
       
    73   for (i <- 0 until n2; j <- 0 until n2) {
       
    74     ctour(n2)(List((i, j))) 
       
    75   }
       
    76 }
       
    77 */
       
    78 
       
    79 val n3 = 9
       
    80 println(s"fast circle tour: n = $n3")
       
    81 
       
    82 Try {
       
    83   for (i <- 0 until n3; j <- 0 until n3) {
       
    84     faster_tour(n3)(List((i, j))) 
       
    85   }
       
    86 }
       
    87 
    36 println("finished")
    88 println("finished")