This repository has been archived by the owner on Oct 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPuzzleChecker.java
55 lines (49 loc) · 1.59 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
52
53
54
55
/******************************************************************************
* Compilation: javac-algs4 PuzzleChecker.java
* Execution: java-algs4 PuzzleChecker filename1.txt filename2.txt ...
* Dependencies: Board.java Solver.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-algs4 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
*
******************************************************************************/
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdOut;
public class PuzzleChecker {
public static void main(String[] args) {
// for each command-line argument
for (String filename : args) {
StdOut.printf("%s: ", filename);
// 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);
StdOut.println(solver.moves());
}
}
}