15 puzzle solver: Difference between revisions

→‎{{header|Scala}}: Use 2.12 compatible, previously was 2.13.2
(Scala solution, quite slow using A* with some trial and error weighting.)
(→‎{{header|Scala}}: Use 2.12 compatible, previously was 2.13.2)
Line 4,649:
// Playing with weight of heuristic vs travelled so far. Giving equal weight take too long.
// Heuristic only will converge too fast on a longer than 52 steps solution.
def aStar() = heuristic() * 53 + travelled() * 42
// Find children/neighbour that is not himself.
def children: List[Board] = List(this.up(), this.down(), this.right(), this.left()).flatten
Line 4,674:
var found = false
var head = initBoard
val pq: mutable.PriorityQueue[Board] = new mutable.PriorityQueue.from(List(head))
pq.enqueue(head)
 
val visited = mutable.HashSet[String]()
var explore = 0
Line 4,683 ⟶ 4,685:
if (!head.key.equals(finalBoard.key)) {
val newChildren = head.children.filter(b ⇒ !visited.contains(b.key))
visited.addAll ++= (newChildren.map(_.key))
pq.enqueue(newChildren: _*)
if (explore % 10000 == 0)
Anonymous user