Skip to content

Commit 52e834a

Browse files
committed
Solved solutions
0 parents  commit 52e834a

13 files changed

+1306
-0
lines changed

8puzzle/Board.java

+156
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
import edu.princeton.cs.algs4.LinkedStack;
2+
3+
public class Board {
4+
private int[][] blocks;
5+
private int n;
6+
7+
public Board(int[][] blocks ) {
8+
n=blocks.length;
9+
this.blocks=new int[n][n];
10+
11+
for (int i=0;i<n;i++)
12+
for (int j=0;j<n;j++)
13+
this.blocks[i][j]=blocks[i][j];
14+
15+
}
16+
17+
public int dimension() {
18+
return n;
19+
}
20+
21+
public int hamming() {
22+
int num,hamming=0;
23+
for (int i=0;i<n;i++) {
24+
for (int j=0;j<n;j++) {
25+
num=i*n+j+1;
26+
if (blocks[i][j]!=0 && blocks[i][j]!=num){
27+
hamming++;
28+
}
29+
}
30+
}
31+
return hamming;
32+
}
33+
34+
public int manhattan() {
35+
int manhattan=0;
36+
37+
for(int i=0;i< n;i++){
38+
for(int j=0;j< n;j++){
39+
if(blocks[i][j]==0) continue;
40+
41+
int iValue=(blocks[i][j]-1)/n;
42+
int jValue=(blocks[i][j]-1)%n;
43+
44+
manhattan+=Math.abs(i-iValue)+Math.abs(j-jValue);
45+
}
46+
}
47+
return manhattan;
48+
}
49+
50+
public boolean isGoal() {
51+
return hamming() == 0;
52+
}
53+
54+
public Board twin() {
55+
int[][] twinBlocks=new int[n][n];
56+
57+
for (int i=0;i<n;i++)
58+
for (int j=0;j<n;j++)
59+
twinBlocks[i][j]=blocks[i][j];
60+
61+
if(twinBlocks[0][0]!=0 && twinBlocks[0][1]!=0){
62+
int temp=twinBlocks[0][0];
63+
twinBlocks[0][0]=twinBlocks[0][1];
64+
twinBlocks[0][1]=temp;
65+
}else {
66+
int temp=twinBlocks[1][0];
67+
twinBlocks[1][0]=twinBlocks[1][1];
68+
twinBlocks[1][1]=temp;
69+
}
70+
71+
return new Board(twinBlocks);
72+
}
73+
74+
public boolean equals(Object y) {
75+
if (y==this) return true;
76+
if( y == null || !(y instanceof Board)) return false;
77+
Board that = (Board) y;
78+
79+
if(that.blocks.length != n) return false;
80+
81+
for (int i=0;i<n;i++){
82+
for (int j=0;j<n;j++){
83+
if(that.blocks[i][j]!=this.blocks[i][j])
84+
return false;
85+
}
86+
}
87+
88+
return true;
89+
}
90+
91+
public Iterable<Board> neighbors() {
92+
LinkedStack<Board> neighs = new LinkedStack<>();
93+
94+
// find position of blank tile
95+
int r = 0;
96+
int c = 0;
97+
98+
for (int i = 0; i < n; i++) {
99+
for (int j = 0; j < n; j++) {
100+
if (blocks[i][j] == 0) {
101+
r = i;
102+
c = j;
103+
break;
104+
}
105+
}
106+
}
107+
108+
if (r > 0) {
109+
Board left=new Board(blocks);
110+
left.blocks[r][c] = left.blocks[r - 1][c];
111+
left.blocks[r - 1][c] = 0;
112+
neighs.push(left);
113+
}
114+
115+
if (c > 0) {
116+
Board top=new Board(blocks);
117+
top.blocks[r][c] = top.blocks[r][c - 1];
118+
top.blocks[r][c - 1] = 0;
119+
neighs.push(top);
120+
}
121+
122+
if (r < n - 1) {
123+
Board right=new Board(blocks);
124+
right.blocks[r][c] = right.blocks[r + 1][c];
125+
right.blocks[r + 1][c] = 0;
126+
neighs.push(right);
127+
}
128+
129+
if (c < n - 1) {
130+
Board down=new Board(blocks);
131+
down.blocks[r][c] = down.blocks[r][c + 1];
132+
down.blocks[r][c + 1] = 0;
133+
neighs.push(down);
134+
}
135+
136+
return neighs;
137+
}
138+
139+
// string representation of this board (in the output format specified below)
140+
public String toString() {
141+
StringBuilder s = new StringBuilder();
142+
s.append(n + "\n");
143+
for (int i = 0; i < n; i++) {
144+
for (int j = 0; j < n; j++) {
145+
s.append(String.format("%2d ", blocks[i][j]));
146+
}
147+
s.append("\n");
148+
}
149+
return s.toString();
150+
}
151+
152+
public static void main(String[] args){
153+
154+
}
155+
156+
}

8puzzle/Solver.java

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import edu.princeton.cs.algs4.MinPQ;
2+
import java.util.ArrayList;
3+
import edu.princeton.cs.algs4.In;
4+
import edu.princeton.cs.algs4.StdOut;
5+
6+
public class Solver {
7+
private MinPQ<Node> pq1 ; //For the initial board
8+
private MinPQ<Node> pq2; //For the twin of initial board
9+
private boolean isSolvable;
10+
private int moves;
11+
private ArrayList<Board> solutionSteps=new ArrayList<>();
12+
private class Node implements Comparable<Node> {
13+
private Board board;
14+
private Node previous;
15+
int moves;
16+
17+
public Node(Board board,Node previous,int moves){
18+
this.board=board;
19+
this.previous=previous;
20+
this.moves=moves;
21+
}
22+
23+
public int compareTo(Node that) {
24+
return this.board.manhattan()+this.moves-that.board.manhattan()-that.moves;
25+
}
26+
}
27+
28+
public Solver(Board initial) {
29+
pq1=new MinPQ<Node>();
30+
pq2=new MinPQ<Node>();
31+
isSolvable=false;
32+
Node node=new Node(initial,null,0);
33+
Node nodeTwin=new Node(initial.twin(),null,0);
34+
35+
while(!node.board.isGoal() && !nodeTwin.board.isGoal()) {
36+
for(Board b:node.board.neighbors()) {
37+
if(node.previous==null || !b.equals(node.previous.board)){
38+
Node neighbor = new Node(b, node,node.moves+1);
39+
pq1.insert(neighbor);
40+
}
41+
}
42+
43+
for(Board b:nodeTwin.board.neighbors()) {
44+
if(nodeTwin.previous==null || !b.equals(nodeTwin.previous.board)){
45+
Node neighbor = new Node(b, nodeTwin,nodeTwin.moves+1);
46+
pq2.insert(neighbor);
47+
}
48+
}
49+
50+
node=pq1.delMin();
51+
nodeTwin=pq2.delMin();
52+
53+
}
54+
55+
if(node.board.isGoal()){
56+
isSolvable=true;
57+
moves=node.moves;
58+
while(node!=null){
59+
solutionSteps.add(0,node.board);
60+
node=node.previous;
61+
}
62+
}
63+
}
64+
65+
public boolean isSolvable(){
66+
return isSolvable;
67+
}
68+
69+
public int moves(){
70+
if(!isSolvable)
71+
return -1;
72+
return moves;
73+
}
74+
75+
public Iterable<Board> solution(){
76+
if(!isSolvable)
77+
return null;
78+
return solutionSteps;
79+
}
80+
81+
public static void main(String[] args) {
82+
// create initial board from file
83+
In in = new In(args[0]);
84+
int n = in.readInt();
85+
int[][] blocks = new int[n][n];
86+
for (int i = 0; i < n; i++)
87+
for (int j = 0; j < n; j++)
88+
blocks[i][j] = in.readInt();
89+
Board initial = new Board(blocks);
90+
91+
// solve the puzzle
92+
Solver solver = new Solver(initial);
93+
94+
// print solution to standard output
95+
if (!solver.isSolvable())
96+
StdOut.println("No solution possible");
97+
else {
98+
StdOut.println("Minimum number of moves = " + solver.moves());
99+
for (Board board : solver.solution())
100+
StdOut.println(board);
101+
}
102+
}
103+
}

collinear/BruteCollinearPoints.java

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
import java.util.ArrayList;
3+
import java.util.Arrays;
4+
public class BruteCollinearPoints {
5+
6+
private ArrayList <LineSegment> segmentList = new ArrayList<>();
7+
public BruteCollinearPoints(Point[] points) {
8+
9+
if(points == null) {
10+
throw new NullPointerException();
11+
}
12+
13+
for(Point p:points) {
14+
if(p == null) {
15+
throw new NullPointerException();
16+
}
17+
}
18+
19+
Point[] pointCopy= Arrays.copyOf(points, points.length);
20+
Arrays.sort(pointCopy);
21+
int length=pointCopy.length;
22+
23+
for(int i=0;i<length-1;i++) {
24+
if(pointCopy[i].compareTo(pointCopy[i+1]) == 0) {
25+
throw new IllegalArgumentException();
26+
}
27+
}
28+
29+
for( int i=0;i<length;i++) {
30+
for (int j=i+1;j<length;j++) {
31+
for (int k=j+1;k<length;k++) {
32+
for (int m=k+1;m<length;m++) {
33+
Point p1=pointCopy[i],p2=pointCopy[j],p3=pointCopy[k],p4=pointCopy[m];
34+
if(p1.slopeTo(p2) ==p1.slopeTo(p3) && p1.slopeTo(p2)==p1.slopeTo(p4)) {
35+
segmentList.add(new LineSegment(p1,p4));
36+
}
37+
}
38+
}
39+
}
40+
}
41+
42+
}
43+
44+
45+
public int numberOfSegments() {
46+
return segmentList.size();
47+
}
48+
49+
public LineSegment[] segments() {
50+
LineSegment[] segmentArr=new LineSegment[segmentList.size()];
51+
return segmentList.toArray(segmentArr);
52+
}
53+
54+
}

collinear/FastCollinearPoints.java

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
public class FastCollinearPoints {
4+
private ArrayList <LineSegment> segmentList = new ArrayList<>();
5+
6+
public FastCollinearPoints(Point[] points) {
7+
if(points == null) {
8+
throw new NullPointerException();
9+
}
10+
11+
for(Point p:points) {
12+
if(p == null) {
13+
throw new NullPointerException();
14+
}
15+
}
16+
17+
Point[] pointCopy=Arrays.copyOf(points, points.length);
18+
Arrays.sort(pointCopy);
19+
int length=pointCopy.length;
20+
21+
22+
for(int i=0;i<length-1;i++) {
23+
if(pointCopy[i].compareTo(pointCopy[i+1]) == 0) {
24+
throw new IllegalArgumentException();
25+
}
26+
}
27+
28+
29+
for(int i=0;i<length-2;i++) {
30+
Point origin=pointCopy[i];
31+
ArrayList<Point> pts = new ArrayList<>();
32+
33+
for (int j = i + 1; j < pointCopy.length; j++) {
34+
pts.add(pointCopy[j]);
35+
}
36+
37+
Point[] ptsArr = pts.toArray(new Point[pts.size()]);
38+
Arrays.sort(ptsArr,origin.slopeOrder());
39+
40+
for(int j=0;j<ptsArr.length-1;j++) {
41+
int counter=1;
42+
Point endPoint=null;
43+
double slope1=origin.slopeTo(ptsArr[j]);
44+
45+
for(int k=j+1;k<ptsArr.length;k++) {
46+
if(slope1==origin.slopeTo(ptsArr[k])) {
47+
counter++;
48+
if(counter >= 3) {
49+
endPoint=ptsArr[k];
50+
j=k;
51+
}
52+
} else {
53+
break;
54+
}
55+
56+
}
57+
58+
if(endPoint!=null){
59+
segmentList.add(new LineSegment(origin,endPoint));
60+
}
61+
}
62+
}
63+
64+
65+
}
66+
67+
public int numberOfSegments() {
68+
return segmentList.size();
69+
}
70+
71+
public LineSegment[] segments() {
72+
LineSegment[] segmentArr=new LineSegment[segmentList.size()];
73+
return segmentList.toArray(segmentArr);
74+
}
75+
}

0 commit comments

Comments
 (0)