MCPcopy Create free account
hub / github.com/MolinDeng/Princeton-algs4 / Solver

Method Solver

LabEnv/04Lab/Solver.java:38–62  ·  view source on GitHub ↗
(Board initial)

Source from the content-addressed store, hash-verified

36
37 // find a solution to the initial board (using the A* algorithm)
38 public Solver(Board initial) {
39 if (initial == null) throw new IllegalArgumentException();
40 MinPQ<Node> minPQ = new MinPQ<>();
41 MinPQ<Node> minPQForTwin = new MinPQ<>();
42 minPQ.insert(new Node(initial, null, 0));
43 minPQForTwin.insert(new Node(initial.twin(), null, 0));
44 MinPQ<Node> executor = minPQ;
45 Node node = null;
46 while (!executor.isEmpty()) {
47 node = executor.delMin();
48 if (node.board.isGoal()) {
49 break;
50 }
51 Iterable<Board> neighbors = node.board.neighbors();
52 for (Board b : neighbors)
53 if (node.prev == null || !b.equals(node.prev.board))
54 executor.insert(new Node(b, node, node.moves + 1));
55
56 executor = executor == minPQ ? minPQForTwin : minPQ;
57 }
58 solvable = executor == minPQ;
59 assert node != null;
60 moves = node.moves;
61 goal = node;
62 }
63
64 // is the initial board solvable? (see below)
65 public boolean isSolvable() {

Callers

nothing calls this directly

Calls 6

insertMethod · 0.45
twinMethod · 0.45
isEmptyMethod · 0.45
isGoalMethod · 0.45
neighborsMethod · 0.45
equalsMethod · 0.45

Tested by

no test coverage detected