summaryrefslogtreecommitdiffstats
path: root/Algorithms/Part-I/4-Puzzle/Solver.java
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2013-03-21 13:04:31 +0100
committerJérémy Zurcher <jeremy@asynk.ch>2013-11-15 17:38:44 +0100
commitb8887c752e9553dd1f51bcc8fcc876810a7457fc (patch)
tree394c57b61ea41f7e368f0a649fc4c14431013d58 /Algorithms/Part-I/4-Puzzle/Solver.java
parent02d71d87de40ad3a14b67726683747b415fa9158 (diff)
downloadcoursera-b8887c752e9553dd1f51bcc8fcc876810a7457fc.zip
coursera-b8887c752e9553dd1f51bcc8fcc876810a7457fc.tar.gz
Algorithms-I : 4-Puzzle: add skeleton Solver and implemented Board
Diffstat (limited to 'Algorithms/Part-I/4-Puzzle/Solver.java')
-rw-r--r--Algorithms/Part-I/4-Puzzle/Solver.java53
1 files changed, 53 insertions, 0 deletions
diff --git a/Algorithms/Part-I/4-Puzzle/Solver.java b/Algorithms/Part-I/4-Puzzle/Solver.java
new file mode 100644
index 0000000..1f96098
--- /dev/null
+++ b/Algorithms/Part-I/4-Puzzle/Solver.java
@@ -0,0 +1,53 @@
+/* vim: set expandtab tabstop=4 shiftwidth=4 : */
+
+public class Solver {
+
+ // find a solution to the initial board (using the A* algorithm)
+ public Solver(Board initial)
+ {
+ // use MinPQ
+ }
+
+ // is the initial board solvable?
+ public boolean isSolvable()
+ {
+ return false;
+ }
+
+ // min number of moves to solve initial board; -1 if no solution
+ public int moves()
+ {
+ return 0;
+ }
+
+ // sequence of boards in a shortest solution; null if no solution
+ public Iterable<Board> solution()
+ {
+ return null;
+ }
+
+ // solve a slider puzzle (given below)
+ public static void main(String[] args)
+ {
+ // create initial board from file
+ In in = new In(args[0]);
+ int N = in.readInt();
+ int[][] blocks = new int[N][N];
+ for (int i = 0; i < N; i++)
+ for (int j = 0; j < N; j++)
+ blocks[i][j] = in.readInt();
+ Board initial = new Board(blocks);
+
+ // solve the puzzle
+ Solver solver = new Solver(initial);
+
+ // print solution to standard output
+ if (!solver.isSolvable())
+ StdOut.println("No solution possible");
+ else {
+ StdOut.println("Minimum number of moves = " + solver.moves());
+ for (Board board : solver.solution())
+ StdOut.println(board);
+ }
+ }
+}