16 |
16 |
17 def is_legal(n: Int)(x: (Int, Int)) = |
17 def is_legal(n: Int)(x: (Int, Int)) = |
18 0 <= x._1 && 0 <= x._2 && x._1 < n && x._2 < n |
18 0 <= x._1 && 0 <= x._2 && x._1 < n && x._2 < n |
19 |
19 |
20 def moves(n: Int)(x: (Int, Int)): List[(Int, Int)] = { |
20 def moves(n: Int)(x: (Int, Int)): List[(Int, Int)] = { |
21 Set((1, 2),(2, 1),(2, -1),(1, -2), |
21 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)) |
22 (-1, -2),(-2, -1),(-2, 1),(-1, 2)).map(add_pair(x)).filter(is_legal(n)) |
23 } |
23 } |
24 |
24 |
25 def tour(n: Int)(steps: List[(Int, Int)]): Unit = { |
25 def tour(n: Int)(steps: List[(Int, Int)]): Unit = { |
26 if (steps.length == n * n && moves(n)(steps.head).contains(steps.last)) |
26 if (steps.length == n * n && moves(n)(steps.head).contains(steps.last)) |
27 print_board(n)(steps) |
27 print_board(n)(steps) |