forked from reking/algs-8puzzle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPuzzleChecker.java
51 lines (46 loc) · 1.47 KB
/
PuzzleChecker.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*************************************************************************
* Compilation: javac PuzzleChecker.java
* Execution: java PuzzleChecker filename1.txt filename2.txt ...
* Dependencies: Board.java Solver.java In.java
*
* This program creates an initial board from each filename specified
* on the command line and finds the minimum number of moves to
* reach the goal state.
*
* % java PuzzleChecker puzzle*.txt
* puzzle00.txt: 0
* puzzle01.txt: 1
* puzzle02.txt: 2
* puzzle03.txt: 3
* puzzle04.txt: 4
* puzzle05.txt: 5
* puzzle06.txt: 6
* ...
* puzzle3x3-impossible: -1
* ...
* puzzle42.txt: 42
* puzzle43.txt: 43
* puzzle44.txt: 44
* puzzle45.txt: 45
*
*************************************************************************/
public class PuzzleChecker {
public static void main(String[] args) {
// for each command-line argument
for (String filename : args) {
// read in the board specified in the filename
In in = new In(filename);
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();
}
}
// solve the slider puzzle
Board initial = new Board(tiles);
Solver solver = new Solver(initial);
System.out.println(filename + ": " + solver.moves());
}
}
}