22 def moves(n: Int)(x: Pos): List[Pos] = { |
22 def moves(n: Int)(x: Pos): List[Pos] = { |
23 List((1, 2),(2, 1),(2, -1),(1, -2), |
23 List((1, 2),(2, 1),(2, -1),(1, -2), |
24 (-1, -2),(-2, -1),(-2, 1),(-1, 2)).map(add_pair(x)).filter(is_legal(n)) |
24 (-1, -2),(-2, -1),(-2, 1),(-1, 2)).map(add_pair(x)).filter(is_legal(n)) |
25 } |
25 } |
26 |
26 |
27 def ordered_moves(n: Int)(steps: List[Pos])(x : Pos): List[Pos] = |
27 // non-circle tours |
28 moves(n)(x).sortBy((x: Pos) => moves(n)(x).filterNot(steps.contains(_)).length) |
28 def tour(n: Int)(steps: List[Pos]): List[List[Pos]] = { |
29 |
29 if (steps.length == n * n) List(steps) |
30 moves(8)(1,3) |
|
31 ordered_moves(8)(Nil)(1,3) |
|
32 ordered_moves(8)(List((2, 4), (2, 6)))(1,3) |
|
33 |
|
34 // non-circle tour |
|
35 def tour(n: Int)(steps: List[Pos]): Unit = { |
|
36 if (steps.length == n * n) |
|
37 print_board(n)(steps) |
|
38 else |
30 else |
39 for (x <- moves(n)(steps.head); if (!steps.contains(x))) tour(n)(x :: steps) |
31 (for (x <- moves(n)(steps.head); if (!steps.contains(x))) yield tour(n)(x :: steps)).flatten |
40 } |
32 } |
41 |
33 |
42 val n = 8 |
34 //val n = 8 |
43 println(s"simple tour: n = $n") |
35 val n = 6 |
|
36 println(s"number simple tours: n = $n") |
44 |
37 |
45 for (i <- 0 until n; j <- 0 until n) tour(n)(List((i, j))) |
38 println((for (i <- 0 until n; j <- 0 until n) yield tour(n)(List((i, j)))).flatten.distinct.size) |