Skip to content

Commit bd71c4f

Browse files
committed
inital classes
1 parent 73b2c8e commit bd71c4f

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
public class Board {
2+
int[][] tiles;
3+
4+
// create a board from an n-by-n array of tiles,
5+
// where tiles[row][col] = tile at (row, col)
6+
public Board(int[][] tiles) {
7+
this.tiles = tiles;
8+
}
9+
10+
// string representation of this board
11+
public String toString() {
12+
return "";
13+
}
14+
15+
// board dimension n
16+
public int dimension() {
17+
return tiles.length;
18+
}
19+
20+
// number of tiles out of place
21+
public int hamming() {
22+
return 0;
23+
}
24+
25+
// sum of Manhattan distances between tiles and goal
26+
public int manhattan() {
27+
return 0;
28+
}
29+
30+
// is this board the goal board?
31+
public boolean isGoal() {
32+
return false;
33+
}
34+
35+
// does this board equal y?
36+
public boolean equals(Object y) {
37+
return false;
38+
}
39+
40+
// all neighboring boards
41+
public Iterable<Board> neighbors() {
42+
return null;
43+
}
44+
45+
// a board that is obtained by exchanging any pair of tiles
46+
public Board twin() {
47+
return null;
48+
}
49+
50+
// unit testing (not graded)
51+
public static void main(String[] args) {
52+
//
53+
}
54+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import edu.princeton.cs.algs4.In;
2+
import edu.princeton.cs.algs4.StdOut;
3+
4+
public class Solver {
5+
// find a solution to the initial board (using the A* algorithm)
6+
public Solver(Board initial) {
7+
if (initial == null) {
8+
throw new IllegalArgumentException("initial argument to Solver constructor is null");
9+
}
10+
}
11+
12+
// is the initial board solvable? (see below)
13+
public boolean isSolvable() {
14+
return false;
15+
}
16+
17+
// min number of moves to solve initial board; -1 if unsolvable
18+
public int moves() {
19+
// Return -1 in moves() if the board is unsolvable.
20+
return -1;
21+
}
22+
23+
// sequence of boards in a shortest solution; null if unsolvable
24+
public Iterable<Board> solution() {
25+
// Return null in solution() if the board is unsolvable.
26+
return null;
27+
}
28+
29+
// test client (see below)
30+
public static void main(String[] args) {
31+
// create initial board from file
32+
In in = new In(args[0]);
33+
int n = in.readInt();
34+
int[][] tiles = new int[n][n];
35+
for (int i = 0; i < n; i++)
36+
for (int j = 0; j < n; j++)
37+
tiles[i][j] = in.readInt();
38+
39+
Board initial = new Board(tiles);
40+
41+
// solve the puzzle
42+
Solver solver = new Solver(initial);
43+
44+
// print solution to standard output
45+
if (!solver.isSolvable()) {
46+
StdOut.println("No solution possible");
47+
}
48+
else {
49+
StdOut.println("Minimum number of moves = " + solver.moves());
50+
for (Board board : solver.solution()) {
51+
StdOut.println(board);
52+
}
53+
}
54+
}
55+
56+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
0 1 3
2+
4 2 5
3+
7 8 6

0 commit comments

Comments
 (0)