|
1 // Part 3 about finding a single tour using the Warnsdorf Rule |
|
2 //============================================================= |
|
3 |
|
4 |
|
5 type Pos = (Int, Int) |
|
6 type Path = List[Pos] |
|
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 ") |
|
13 } |
|
14 println |
|
15 } |
|
16 } |
|
17 |
|
18 def add_pair(x: Pos)(y: Pos): Pos = |
|
19 (x._1 + y._1, x._2 + y._2) |
|
20 |
|
21 def is_legal(dim: Int, path: Path)(x: Pos): Boolean = |
|
22 0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x) |
|
23 |
|
24 def moves(x: Pos): List[Pos] = |
|
25 List(( 1, 2),( 2, 1),( 2, -1),( 1, -2), |
|
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 import scala.annotation.tailrec |
|
35 |
|
36 /* |
|
37 @tailrec |
|
38 def first(xs: List[Pos], f: Pos => Option[Path]): Option[Path] = xs match { |
|
39 case Nil => None |
|
40 case x::xs => { |
|
41 val result = f(x) |
|
42 if (result.isDefined) result else first(xs, f) |
|
43 } |
|
44 } |
|
45 */ |
|
46 |
|
47 def first[A, B](xs: List[A], f: A => Option[B]): Option[B] = |
|
48 xs.flatMap(f(_)).headOption |
|
49 |
|
50 |
|
51 def first_closed_tour_heuristics(dim: Int, path: Path): Option[Path] = { |
|
52 if (path.length == dim * dim && moves(path.head).contains(path.last)) Some(path) |
|
53 else |
|
54 first(ordered_moves(dim, path, path.head), (x: Pos) => first_closed_tour_heuristics(dim, x::path)) |
|
55 } |
|
56 |
|
57 /* |
|
58 for (dim <- 1 to 6) { |
|
59 val t = first_closed_tour_heuristics(dim, List((dim / 2, dim / 2))) |
|
60 println(s"${dim} x ${dim} closed: " + (if (t == None) "" else { print_board(dim, t.get) ; "" })) |
|
61 } |
|
62 */ |
|
63 |
|
64 //@tailrec |
|
65 def first_tour_heuristics(dim: Int, path: Path): Option[Path] = { |
|
66 |
|
67 @tailrec |
|
68 def aux(dim: Int, path: Path, moves: List[Position]): Option[Path |
|
69 if (path.length == dim * dim) Some(path) |
|
70 else |
|
71 moves match { |
|
72 case Nil => None |
|
73 case x::xs => { |
|
74 val r = first_tour_heuristics(dim, x::path) |
|
75 if (r.isDefined) Some(r) else aux(dim, path, xs) |
|
76 } |
|
77 aux(dim, path, ordered_moves(dim, path, path.head)) |
|
78 } |
|
79 |
|
80 |
|
81 /* |
|
82 def first_tour_heuristics(dim: Int, path: Path): Option[Path] = { |
|
83 if (path.length == dim * dim) Some(path) |
|
84 else |
|
85 for (p <- ordered_moves(dim, path, path.head)) |
|
86 val r = first_tour_heuristics(dim, x::path) |
|
87 //first(ordered_moves(dim, path, path.head), (x: Pos) => first_tour_heuristics(dim, x::path)) |
|
88 ordered_moves(dim, path, path.head).view.flatMap((x: Pos) => first_tour_heuristics(dim, x::path)).headOption |
|
89 } |
|
90 */ |
|
91 |
|
92 /* |
|
93 for (dim <- 1 to 50) { |
|
94 val t = first_tour_heuristics(dim, List((dim / 2, dim / 2))) |
|
95 println(s"${dim} x ${dim}: " + (if (t == None) "" else { print_board(dim, t.get) ; "" })) |
|
96 } |
|
97 */ |
|
98 |
|
99 print_board(50, first_tour_heuristics(50, (25,25)::Nil).get) |