(Solver Data Type) Create an immutable data type Solver in Solver.java with the following API: Corner cases....
Fantastic news! We've Found the answer you've been seeking!
Question:
(Solver Data Type) Create an immutable data type Solver in Solver.java with the following API:
Corner cases.
The constructor should throw a java.lang.NullPointerException if the initial board is null and a java.lang.IllegalArgumentException if the initial board is not solvable.
And the result should look like this:
Here's the input file:
3 4 1 3 0 2 6 7 5 8
And here's what I have and the format should look like:
Transcribed Image Text:
method Solver (Board initial) int moves () Iterable solution () description find a solution to the initial board (using the A* algorithm) the minimum number of moves to solve initial board sequence of boards in a shortest solution $ java Solver data/puzzle05.txt Minimum number of moves = 5 3 040 3 3 1 4 7 013 3 7 3 125 1 4 7 1 4 7 ∞ os c 12 4 0 7 5 8 6 5 8 03 2 6 58 3 600 2 5 2 3 5 0 8 ∞o os co 6 360 3 80 Solver.java * import edu.princeton.cs.algs4. In; import edu.princeton.cs.algs4.LinkedStack; import edu.princeton.cs.algs4.MinPQ; import edu.princeton.cs.algs4.Stdout; import java.util.Comparator; // A solver based on the A* algorithm for the 8-puzzle and its generalizations. public class Solver { LinkedStack solution; int moves; // Helper search node class. private class SearchNode { Board board; int moves; SearchNode prev; } SearchNode (Board board, int moves, SearchNode previous) { board; } this.board this. moves = moves; this.prev = previous; // Find a solution to the initial board (using the A* algorithm). public Solver (Board initial) { if (initial == null) { throw new NullPointerException(); } Comparator comparator = new ManhattanOrder(); solution = new LinkedStack (); SearchNode first = new SearchNode (initial,0,null); //initialize the first search node. int moves = 0; MinPQ pq = new MinPQ (233, comparator); pq.insert (first); //insert the first node into the queue. while (!pq.isEmpty()) { SearchNode node = Hamming or manhattan distance. if(node.board.isGoal()) { // if a node's board has solved, push that and all previous node except the first into the solution stack. } pq.delMin(); // dequeue the board with minimum for (SearchNode x = node; x.prev != null;x x.prev) { solution.push(x.board); } break; } for (Board x:node.board.neighbors()) { SearchNode next = new SearchNode (x, moves, node); pq.insert(next); } // insert all neighbors into the queue. } this.moves++; // The minimum number of moves to solve the initial board. public int moves () { return this. moves; } } // Sequence of boards in a shortest solution. public Iterable solution () { return solution; } // Helper hamming priority function comparator. private static class HammingOrder implements Comparator { public int compare (SearchNode a, SearchNode b) { if(a.board.hamming() + a.moves > b. board.hamming() + b. moves) { return 1; } else if (a.board.hamming() + a.moves < b.board.hamming() + b.moves) return -1; } else { return 0; } } private static class Manhattanorder implements Comparator { public int compare (SearchNode a, SearchNode b) { if(a.board.manhattan () + a.moves > b. board. manhattan () + b.moves) { return 1; } else if (a.board.manhattan () + a.moves < b.board.manhattan () + b.moves) { return -1; } else { return 0; } } } // Test client. [DO NOT EDIT] public static void main(String[] args) { In in new In (args[0]); int N = in.readInt (); int[][] tiles = new int [N] [N]; for (int i = 0; i < N; i++) { } for (int j = 0; j < N; j++) { tiles[i][j] in. readInt (); = } } Board initial = new Board (tiles); if (initial.isSolvable()) { } Solver solver = new Solver(initial); Stdout.println("Minimum number of moves + solver.moves()); for (Board board solver.solution()) { Stdout.println (board); } } else { Stdout.println("Unsolvable puzzle"); method Solver (Board initial) int moves () Iterable solution () description find a solution to the initial board (using the A* algorithm) the minimum number of moves to solve initial board sequence of boards in a shortest solution $ java Solver data/puzzle05.txt Minimum number of moves = 5 3 040 3 3 1 4 7 013 3 7 3 125 1 4 7 1 4 7 ∞ os c 12 4 0 7 5 8 6 5 8 03 2 6 58 3 600 2 5 2 3 5 0 8 ∞o os co 6 360 3 80 Solver.java * import edu.princeton.cs.algs4. In; import edu.princeton.cs.algs4.LinkedStack; import edu.princeton.cs.algs4.MinPQ; import edu.princeton.cs.algs4.Stdout; import java.util.Comparator; // A solver based on the A* algorithm for the 8-puzzle and its generalizations. public class Solver { LinkedStack solution; int moves; // Helper search node class. private class SearchNode { Board board; int moves; SearchNode prev; } SearchNode (Board board, int moves, SearchNode previous) { board; } this.board this. moves = moves; this.prev = previous; // Find a solution to the initial board (using the A* algorithm). public Solver (Board initial) { if (initial == null) { throw new NullPointerException(); } Comparator comparator = new ManhattanOrder(); solution = new LinkedStack (); SearchNode first = new SearchNode (initial,0,null); //initialize the first search node. int moves = 0; MinPQ pq = new MinPQ (233, comparator); pq.insert (first); //insert the first node into the queue. while (!pq.isEmpty()) { SearchNode node = Hamming or manhattan distance. if(node.board.isGoal()) { // if a node's board has solved, push that and all previous node except the first into the solution stack. } pq.delMin(); // dequeue the board with minimum for (SearchNode x = node; x.prev != null;x x.prev) { solution.push(x.board); } break; } for (Board x:node.board.neighbors()) { SearchNode next = new SearchNode (x, moves, node); pq.insert(next); } // insert all neighbors into the queue. } this.moves++; // The minimum number of moves to solve the initial board. public int moves () { return this. moves; } } // Sequence of boards in a shortest solution. public Iterable solution () { return solution; } // Helper hamming priority function comparator. private static class HammingOrder implements Comparator { public int compare (SearchNode a, SearchNode b) { if(a.board.hamming() + a.moves > b. board.hamming() + b. moves) { return 1; } else if (a.board.hamming() + a.moves < b.board.hamming() + b.moves) return -1; } else { return 0; } } private static class Manhattanorder implements Comparator { public int compare (SearchNode a, SearchNode b) { if(a.board.manhattan () + a.moves > b. board. manhattan () + b.moves) { return 1; } else if (a.board.manhattan () + a.moves < b.board.manhattan () + b.moves) { return -1; } else { return 0; } } } // Test client. [DO NOT EDIT] public static void main(String[] args) { In in new In (args[0]); int N = in.readInt (); int[][] tiles = new int [N] [N]; for (int i = 0; i < N; i++) { } for (int j = 0; j < N; j++) { tiles[i][j] in. readInt (); = } } Board initial = new Board (tiles); if (initial.isSolvable()) { } Solver solver = new Solver(initial); Stdout.println("Minimum number of moves + solver.moves()); for (Board board solver.solution()) { Stdout.println (board); } } else { Stdout.println("Unsolvable puzzle");
Expert Answer:
Answer rating: 100% (QA)
Solution import javautilComparator public class Solver private MinPQ pq private MinPQ pqTwin private SearchNode goal null private boolean solvable tru... View the full answer
Related Book For
Introduction to Probability and Statistics
ISBN: 978-1133103752
14th edition
Authors: William Mendenhall, Robert Beaver, Barbara Beaver
Posted Date:
Students also viewed these programming questions
-
What is the shear capacity of the RC beam described below considering the steel reinforcement and using the formula: VRsyAw 2fyd cot 8/s The shear reinforcement in the beam is provided by sets of...
-
Create the following vector A. A = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18] Then using the MATLAB's built-in reshape function create the following matrix B from the vector A: By writing one...
-
In Exercises 7 and 8 of Chapter 4 you worked with data on sales for a line of skiwear that is produced by HeathCo Industries. Barbara Lynch, product manager for the skiwear, has the responsibility of...
-
Determine the vector A-C, given the vectors A and C in the figure. (Figure 1) Figure B (B=26.5) 56.0% (A = 44.0) 28.0 C(C= 31.0) 1 of 1 Determine the magnitude of the vector A - . Express your...
-
The trial balance of Torres Company as of February 28, 2013, appears below. INSTRUCTIONS 1. Record the trial balance in the Trial Balance section of the worksheet. 2. Complete the worksheet by making...
-
The U.S. government spends over $74 billion on its Supplemental Nutrition Assistance Program (SNAP) to provide millions of Americans with the means to purchase food. Beneficiaries use an Electronic...
-
What are the three parts of a make rule?
-
Jim Hetzel manufactures and sells homemade wine, and he wants to develop a standard cost per gallon. The following are required for production of a 50-gallon batch. 3,000 ounces of grape concentrate...
-
You have been hired as a data analyst for the company. Adventure Works managers have requested that you analyze 3 years of sales and cost data to help them answer specific business questions. In the...
-
A 12.75-year maturity zero-coupon bond selling at a yield to maturity of 8% (effective annual yield) has convexity of 150.3 and modified duration of 11.81 years. A 30-year maturity 6% coupon bond...
-
A solid adsorbent is used to remove colored impurity from an aqueous solution. The original value of color on an arbitrary scale is 48. It is required to reduce this to 10 % of its original value...
-
If an agile development team has already been doing testing during each sprint, why should they consider using parallel independent testing? 1. Software developers are not trained to perform tests 2....
-
If the government expects that a substantial portion of the resources supporting a special revenue fund's activities will no longer be derived from restricted and committed revenue sources, the...
-
Explain five (5) steps on how to develop an Internal Factor Evaluation (IFE) Matrix. Provide example for each step.
-
A company has issues with users deleting general ledger accounts that were created for future posting group usage but have nothing posted to them. You need to prevent deletion of these general ledger...
-
What is project management? What is the definition of a project? What are the basic components of project management? What is the role of a project manager? Can you describe these topics in the...
-
Determine the percent yield of solid iron.
-
1. Use these cost, revenue, and probability estimates along with the decision tree to identify the best decision strategy for Trendy's Pies. 2. Suppose that Trendy is concerned about her probability...
-
In Exercise 8.20, we explored the average cost of lodging at three different hotel chains. We randomly select 50 billing statements from the computer databases of the Marriott, Westin, and Doubletree...
-
Compilation of large masses of data on lung cancer shows that approximately 1 of every 40 adults acquires the disease. Workers in a certain occupation are known to work in an air-polluted environment...
-
Refer to Exercise 1.51. Listed here is the percentage of the popular vote received by President Obama in each of the 50 states: Exercise 1.51 The 2008 election was a race in which Barack Obama...
-
Show that under the assumptions of Proposition 23.5 we can interchange integration and differentiation: \(\frac{\partial^{2}}{\partial x_{j} \partial x_{k}} \int p(t, x, y) u(y) d y=\int...
-
Complete the proof of Proposition 23.6 (Kolmogorov's forward equation). Data From 23.6 Proposition 23.6 Proposition (forward equation. Kolmogorov 1931). Let (X+) to denote a diffusion Ex. 23.5...
-
Let \(\left(N_{t}, \mathscr{F}_{t} ight)_{t \geqslant 0}\) be a continuous, real-valued local martingale and \(u \in \mathcal{C}^{2}(\mathbb{R})\). Show the following It formula \(d u\left(N_{t}...
Study smarter with the SolutionInn App