Skip to content

Commit

Permalink
functional execpt hard to solve 4x4
Browse files Browse the repository at this point in the history
  • Loading branch information
Shuxun Cao committed Aug 27, 2017
1 parent 53e52f7 commit e93846c
Show file tree
Hide file tree
Showing 129 changed files with 785 additions and 186 deletions.
Binary file modified chapter4/Board$1$1.class
Binary file not shown.
Binary file modified chapter4/Board$1.class
Binary file not shown.
Binary file modified chapter4/Board.class
Binary file not shown.
119 changes: 59 additions & 60 deletions chapter4/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,39 @@
import edu.princeton.cs.algs4.StdRandom;

public class Board {
private int[][] locBlocks = new int[3][3];

private int[][] neighbor1 = new int[3][3];
private int[][] neighbor2 = new int[3][3];
private int[][] neighbor3 = new int[3][3];
private int[][] neighbor4 = new int[3][3];
private int[][] initBlocks; //initial blocks
private int[][] neighborBlock1; //neigbor blocks
private int[][] neighborBlock2; //neigbor blocks
private int[][] neighborBlock3; //neigbor blocks
private int[][] neighborBlock4; //neigbor blocks
private Board[] neighborBoard = new Board[4];

private int dimension;
private int numOfBlocksOutOfSpace = 0;
private int sumOfManhattanDistance = 0;
private int blankX = 0;
private int blankY = 0;

private int numOfNeighbor = 0;

public Board(int[][] blocks) // construct a board from an n-by-n array of blocks
{
//for now, allocate a big two dimension array
//todo: dynamic creation
// lockBlocks = new int[3][3];
//copy value from blocks to locBlocks
initBlocks = new int[blocks[0].length][blocks[0].length];
neighborBlock1 = new int[blocks[0].length][blocks[0].length];
neighborBlock2 = new int[blocks[0].length][blocks[0].length];
neighborBlock3 = new int[blocks[0].length][blocks[0].length];
neighborBlock4 = new int[blocks[0].length][blocks[0].length];
for (int i = 0; i < blocks[0].length; i++)
{
for (int j = 0; j < blocks[0].length; j++)
{
locBlocks[i][j] = blocks[i][j];
if (locBlocks[i][j] == 0)
initBlocks[i][j] = blocks[i][j];
// create neighbor blocks
neighborBlock1[i][j] = initBlocks[i][j];
neighborBlock2[i][j] = initBlocks[i][j];
neighborBlock3[i][j] = initBlocks[i][j];
neighborBlock4[i][j] = initBlocks[i][j];
if (initBlocks[i][j] == 0)
{
//init location of blank
blankX = i;
Expand All @@ -38,66 +43,60 @@ public Board(int[][] blocks) // construct a board from an n-by-n array
}
}
dimension = blocks[0].length;

/*
* compute sum of distance between blocks and goal
*/
for (int i = 0; i < dimension; i++)
{
for (int j = 0; j < dimension; j++)
{
neighbor1[i][j] = locBlocks[i][j];
neighbor2[i][j] = locBlocks[i][j];
neighbor3[i][j] = locBlocks[i][j];
neighbor4[i][j] = locBlocks[i][j];


if (locBlocks[i][j] != 0)
if (initBlocks[i][j] != 0)
{
if (locBlocks[i][j] != i * dimension + j + 1)
if (initBlocks[i][j] != i * dimension + j + 1)
{
numOfBlocksOutOfSpace++;
sumOfManhattanDistance += (i >= (locBlocks[i][j] - 1) / dimension ?
i - (locBlocks[i][j] - 1) / dimension : (locBlocks[i][j] - 1) / dimension - i)
+ (j >= (locBlocks[i][j] - 1) % dimension ?
j - (locBlocks[i][j] - 1) % dimension : (locBlocks[i][j] - 1) % dimension - j);
sumOfManhattanDistance += (i >= (initBlocks[i][j] - 1) / dimension ?
i - (initBlocks[i][j] - 1) / dimension : (initBlocks[i][j] - 1) / dimension - i)
+ (j >= (initBlocks[i][j] - 1) % dimension ?
j - (initBlocks[i][j] - 1) % dimension : (initBlocks[i][j] - 1) % dimension - j);

}
}
}
}
}
}

private void findAllNeighbor()
{
numOfNeighbor = 0;
if (blankX > 0)
{
int temp = neighbor1[blankX - 1][blankY];
neighbor1[blankX - 1][blankY] = neighbor1[blankX][blankY];
neighbor1[blankX][blankY] = temp;
neighborBoard[numOfNeighbor++] = new Board(neighbor1);
int temp = neighborBlock1[blankX - 1][blankY];
neighborBlock1[blankX - 1][blankY] = neighborBlock1[blankX][blankY];
neighborBlock1[blankX][blankY] = temp;
neighborBoard[numOfNeighbor++] = new Board(neighborBlock1);
}
if (blankX < 2)
{
int temp = neighbor2[blankX + 1][blankY];
neighbor2[blankX + 1][blankY] = neighbor2[blankX][blankY];
neighbor2[blankX][blankY] = temp;
neighborBoard[numOfNeighbor++] = new Board(neighbor2);
int temp = neighborBlock2[blankX + 1][blankY];
neighborBlock2[blankX + 1][blankY] = neighborBlock2[blankX][blankY];
neighborBlock2[blankX][blankY] = temp;
neighborBoard[numOfNeighbor++] = new Board(neighborBlock2);
}
if (blankY > 0)
{
int temp = neighbor3[blankX][blankY - 1];
neighbor3[blankX][blankY - 1] = neighbor3[blankX][blankY];
neighbor3[blankX][blankY] = temp;
neighborBoard[numOfNeighbor++] = new Board(neighbor3);
int temp = neighborBlock3[blankX][blankY - 1];
neighborBlock3[blankX][blankY - 1] = neighborBlock3[blankX][blankY];
neighborBlock3[blankX][blankY] = temp;
neighborBoard[numOfNeighbor++] = new Board(neighborBlock3);
}
if (blankY < 2)
{
int temp = neighbor4[blankX][blankY + 1];
neighbor4[blankX][blankY + 1] = neighbor4[blankX][blankY];
neighbor4[blankX][blankY] = temp;
neighborBoard[numOfNeighbor++] = new Board(neighbor4);
int temp = neighborBlock4[blankX][blankY + 1];
neighborBlock4[blankX][blankY + 1] = neighborBlock4[blankX][blankY];
neighborBlock4[blankX][blankY] = temp;
neighborBoard[numOfNeighbor++] = new Board(neighborBlock4);
}
}
public int dimension() // board dimension n
Expand All @@ -121,7 +120,7 @@ public boolean isGoal() // is this board the goal board?
{
if (!(i == 2 & j == 2))
{
if (locBlocks[i][j] != i * dimension + j + 1)
if (initBlocks[i][j] != i * dimension + j + 1)
{
isGoal = false;
}
Expand All @@ -144,19 +143,19 @@ public Board twin() // a board that is obtained by exchanging
swapX2 = StdRandom.uniform(3);
swapY2 = StdRandom.uniform(3);
} while (swapX2 == blankX && swapY2 == blankY);
int[][] twinBlock = new int[3][3];

int[][] twinBlock = new int[dimension][dimension];
for (int i = 0; i < dimension; i++)
{
for (int j = 0; j < dimension; j++)
{
twinBlock[i][j] = locBlocks[i][j];
twinBlock[i][j] = initBlocks[i][j];
}
}
int temp = twinBlock[swapX1][swapY1];
twinBlock[swapX1][swapY1] = twinBlock[swapX2][swapY2];
twinBlock[swapX2][swapY2] = temp;

Board newBoard = new Board(twinBlock);
return newBoard;
}
Expand Down Expand Up @@ -195,14 +194,14 @@ public Board next()
}
};
}

public String toString() // string representation of this board (in the output format specified below)
{
StringBuilder s = new StringBuilder();
s.append(dimension + "\n");
for (int i = 0; i < dimension; i++) {
for (int j = 0; j < dimension; j++) {
s.append(String.format("%2d ", locBlocks[i][j]));
s.append(String.format("%2d ", initBlocks[i][j]));
}
s.append("\n");
}
Expand All @@ -213,7 +212,7 @@ public static void main(String[] args) // unit tests (not graded)
{
int[][] initBoard = new int[3][3];
int[][] initBoard2 = new int[3][3];

initBoard[0][0] = 5;
initBoard[0][1] = 1;
initBoard[0][2] = 7;
Expand All @@ -223,7 +222,7 @@ public static void main(String[] args) // unit tests (not graded)
initBoard[2][0] = 3;
initBoard[2][1] = 2;
initBoard[2][2] = 4;

initBoard2[0][0] = 1;
initBoard2[0][1] = 2;
initBoard2[0][2] = 3;
Expand All @@ -233,27 +232,27 @@ public static void main(String[] args) // unit tests (not graded)
initBoard2[2][0] = 7;
initBoard2[2][1] = 8;
initBoard2[2][2] = 0;

Board testBoard = new Board(initBoard);
Board ReferenceBoard = new Board(initBoard2);

System.out.println(testBoard.toString());
if (testBoard.isGoal())
{
System.out.println("Is Goal!");
}
else
{
System.out.println("Is not Goal!");
System.out.println("Is not Goal!");
}

if (testBoard.equals(ReferenceBoard))
{
System.out.println("Equal");
}
else
{
System.out.println("Not Equal");
System.out.println("Not Equal");
}
System.out.println("twin of reference Board: " + (ReferenceBoard.twin()).toString());
for (Board neighborBoard : testBoard.neighbors())
Expand Down
Binary file modified chapter4/Solver$1.class
Binary file not shown.
Binary file modified chapter4/Solver$2$1.class
Binary file not shown.
Binary file modified chapter4/Solver$2.class
Binary file not shown.
Binary file modified chapter4/Solver$SearchNode.class
Binary file not shown.
Binary file modified chapter4/Solver.class
Binary file not shown.
Loading

0 comments on commit e93846c

Please sign in to comment.