blob: 1f960984a46b7ba83ea387fc117595710a2e9fbf (
plain)
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
|
/* 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);
}
}
}
|