|
| 1 | +package com.algs4.hw4; |
| 2 | + |
| 3 | +/****************************************************************************** |
| 4 | + * Compilation: javac PuzzleChecker.java |
| 5 | + * Execution: java PuzzleChecker filename1.txt filename2.txt ... |
| 6 | + * Dependencies: Board.java Solver.java |
| 7 | + * |
| 8 | + * This program creates an initial board from each filename specified |
| 9 | + * on the command line and finds the minimum number of moves to |
| 10 | + * reach the goal state. |
| 11 | + * |
| 12 | + * % java PuzzleChecker puzzle*.txt |
| 13 | + * puzzle00.txt: 0 |
| 14 | + * puzzle01.txt: 1 |
| 15 | + * puzzle02.txt: 2 |
| 16 | + * puzzle03.txt: 3 |
| 17 | + * puzzle04.txt: 4 |
| 18 | + * puzzle05.txt: 5 |
| 19 | + * puzzle06.txt: 6 |
| 20 | + * ... |
| 21 | + * puzzle3x3-impossible: -1 |
| 22 | + * ... |
| 23 | + * puzzle42.txt: 42 |
| 24 | + * puzzle43.txt: 43 |
| 25 | + * puzzle44.txt: 44 |
| 26 | + * puzzle45.txt: 45 |
| 27 | + * |
| 28 | + ******************************************************************************/ |
| 29 | + |
| 30 | +import edu.princeton.cs.algs4.In; |
| 31 | +import edu.princeton.cs.algs4.StdOut; |
| 32 | + |
| 33 | +public class PuzzleChecker { |
| 34 | + |
| 35 | + public static void main(String[] args) { |
| 36 | + |
| 37 | + // for each command-line argument |
| 38 | + for (String filename : args) { |
| 39 | + |
| 40 | + // read in the board specified in the filename |
| 41 | + In in = new In(filename); |
| 42 | + int n = in.readInt(); |
| 43 | + int[][] tiles = new int[n][n]; |
| 44 | + for (int i = 0; i < n; i++) { |
| 45 | + for (int j = 0; j < n; j++) { |
| 46 | + tiles[i][j] = in.readInt(); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + // solve the slider puzzle |
| 51 | + Board initial = new Board(tiles); |
| 52 | + Solver solver = new Solver(initial); |
| 53 | + StdOut.println(filename + ": " + solver.moves()); |
| 54 | + } |
| 55 | + } |
| 56 | +} |
0 commit comments