1 import scala.util._ |
1 // Part 3 about finding a single tour using the Warnsdorf Rule |
|
2 //============================================================= |
2 |
3 |
3 def print_board(n: Int)(steps: List[(Int, Int)]): Unit = { |
4 |
4 for (i <- 0 until n) { |
5 type Pos = (Int, Int) |
5 for (j <- 0 until n) { |
6 type Path = List[Pos] |
6 print(f"${steps.indexOf((i, j))}%3.0f ") |
7 |
|
8 def print_board(dim: Int, path: Path): Unit = { |
|
9 println |
|
10 for (i <- 0 until dim) { |
|
11 for (j <- 0 until dim) { |
|
12 print(f"${path.reverse.indexOf((i, j))}%3.0f ") |
7 } |
13 } |
8 println |
14 println |
9 } |
15 } |
10 //readLine() |
|
11 System.exit(0) |
|
12 } |
16 } |
13 |
17 |
14 def add_pair(x: (Int, Int))(y: (Int, Int)) = |
18 def add_pair(x: Pos)(y: Pos): Pos = |
15 (x._1 + y._1, x._2 + y._2) |
19 (x._1 + y._1, x._2 + y._2) |
16 |
20 |
17 def is_legal(n: Int)(x: (Int, Int)) = |
21 def is_legal(dim: Int, path: Path)(x: Pos): Boolean = |
18 0 <= x._1 && 0 <= x._2 && x._1 < n && x._2 < n |
22 0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x) |
19 |
23 |
20 def moves(n: Int)(x: (Int, Int)): List[(Int, Int)] = { |
24 def moves(x: Pos): List[Pos] = |
21 List((1, 2),(2, 1),(2, -1),(1, -2), |
25 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)) |
26 (-1, -2),(-2, -1),(-2, 1),(-1, 2)).map(add_pair(x)) |
|
27 |
|
28 def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] = |
|
29 moves(x).filter(is_legal(dim, path)) |
|
30 |
|
31 def ordered_moves(dim: Int, path: Path, x: Pos): List[Pos] = |
|
32 legal_moves(dim, path, x).sortBy((x) => legal_moves(dim, path, x).length) |
|
33 |
|
34 |
|
35 def first(xs: List[Pos], f: Pos => Option[Path]): Option[Path] = xs match { |
|
36 case Nil => None |
|
37 case x::xs => { |
|
38 val result = f(x) |
|
39 if (result.isDefined) result else first(xs, f) |
|
40 } |
23 } |
41 } |
24 |
42 |
25 def ordered_moves(n: Int)(steps: List[(Int, Int)])(x : (Int, Int)): List[(Int, Int)] = |
|
26 moves(n)(x).sortBy((x: (Int, Int)) => moves(n)(x).filterNot(steps.contains(_)).length) |
|
27 |
43 |
28 moves(8)(1,3) |
44 def first_closed_tour_heuristics(dim: Int, path: Path): Option[Path] = { |
29 ordered_moves(8)(Nil)(1,3) |
45 if (path.length == dim * dim && moves(path.head).contains(path.last)) Some(path) |
30 ordered_moves(8)(List((2, 4), (2, 6)))(1,3) |
46 else |
31 |
47 first(ordered_moves(dim, path, path.head), (x: Pos) => first_closed_tour_heuristics(dim, x::path)) |
32 // non-circle tour parallel |
|
33 def tour(n: Int)(steps: List[(Int, Int)]): Unit = { |
|
34 if (steps.length == n * n && moves(n)(steps.head).contains(steps.last)) |
|
35 print_board(n)(steps) |
|
36 else |
|
37 for (x <- moves(n)(steps.head).par; if (!steps.contains(x))) tour(n)(x :: steps) |
|
38 } |
48 } |
39 |
49 |
40 val n = 7 |
50 for (dim <- 1 to 6) { |
41 println(s"circle tour parallel: n = $n") |
51 val t = first_closed_tour_heuristics(dim, List((dim / 2, dim / 2))) |
|
52 println(s"${dim} x ${dim} closed: " + (if (t == None) "" else { print_board(dim, t.get) ; "" })) |
|
53 } |
42 |
54 |
43 val starts = for (i <- 0 until n; j <- 0 until n) yield (i, j) |
|
44 |
55 |
45 starts.par.foreach((x:(Int, Int)) => tour(n)(List(x))) |
56 def first_tour_heuristics(dim: Int, path: Path): Option[Path] = { |
|
57 if (path.length == dim * dim) Some(path) |
|
58 else |
|
59 first(ordered_moves(dim, path, path.head), (x: Pos) => first_tour_heuristics(dim, x::path)) |
|
60 } |
|
61 |
|
62 for (dim <- 1 to 50) { |
|
63 val t = first_tour_heuristics(dim, List((dim / 2, dim / 2))) |
|
64 println(s"${dim} x ${dim}: " + (if (t == None) "" else { print_board(dim, t.get) ; "" })) |
|
65 } |