Skip to content

Commit

Permalink
Functional with small Puzzle
Browse files Browse the repository at this point in the history
  • Loading branch information
Shuxun Cao committed Jul 20, 2017
1 parent 0966e48 commit 368417f
Show file tree
Hide file tree
Showing 12 changed files with 264 additions and 81 deletions.
Binary file added chapter4/Board$1$1.class
Binary file not shown.
Binary file added chapter4/Board$1.class
Binary file not shown.
Binary file modified chapter4/Board$BoardIterator$BoardIterators.class
Binary file not shown.
Binary file modified chapter4/Board$BoardIterator.class
Binary file not shown.
Binary file modified chapter4/Board.class
Binary file not shown.
236 changes: 176 additions & 60 deletions chapter4/Board.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import edu.princeton.cs.algs4.MinPQ;
import java.util.Iterator;
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 Board[] neighborBoard = new Board[4];

private int dimension;
private int blankX;
private int blankY;
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
Expand All @@ -26,48 +38,79 @@ public Board(int[][] blocks) // construct a board from an n-by-n array
}
}
dimension = blocks[0].length;
}
// (where blocks[i][j] = block in row i, column j)
public int dimension() // board dimension n
{
return this.dimension;
}
public int hamming() // number of blocks out of place
{
int numOfOut = 0;

/*
* 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 (locBlocks[i][j] != i * dimension + j + 1)
{
numOfOut++;
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);

}
}
}
}
return numOfOut;
}
}
public int manhattan() // sum of Manhattan distances between blocks and goal

private void findAllNeighbor()
{
int numOfOut = 0;
for (int i = 0; i < dimension; i++)
numOfNeighbor = 0;
if (blankX > 0)
{
for (int j = 0; j < dimension; j++)
{
if (locBlocks[i][j] != 0)
{
if (locBlocks[i][j] != i * dimension + j + 1)
{
numOfOut += i - (locBlocks[i][j] - 1) % dimension
+ j - (locBlocks[i][j] - 1) / dimension;
}
}
}
int temp = neighbor1[blankX - 1][blankY];
neighbor1[blankX - 1][blankY] = neighbor1[blankX][blankY];
neighbor1[blankX][blankY] = temp;
neighborBoard[numOfNeighbor++] = new Board(neighbor1);
}
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);
}
return numOfOut;
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);
}
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);
}
}
public int dimension() // board dimension n
{
return this.dimension;
}
public int hamming() // number of blocks out of place
{
return numOfBlocksOutOfSpace;
}
public int manhattan() // sum of Manhattan distances between blocks and goal
{
return sumOfManhattanDistance;
}
public boolean isGoal() // is this board the goal board?
{
Expand All @@ -89,56 +132,129 @@ public boolean isGoal() // is this board the goal board?
}
public Board twin() // a board that is obtained by exchanging any pair of blocks
{
int swapX;
int swapY;
do {
swapX = StdRandom.uniform(3);
swapY = StdRandom.uniform(3);
} while (swapX == blankX && swapY == blankY);

int[][] twinBlock = new int[3][3];
twinBlock = locBlocks;
for (int i = 0; i < dimension; i++)
{
for (int j = 0; j < dimension; j++)
{
twinBlock[i][j] = locBlocks[i][j];
}
}
int temp = twinBlock[swapX][swapY];
twinBlock[swapX][swapY] = twinBlock[blankX][blankY];
twinBlock[blankX][blankY] = temp;

Board newBoard = new Board(twinBlock);
return newBoard;
}
public boolean equals(Object y) // does this board equal y?
{
return true;
if (y != null && this.toString().equals(((Board)y).toString()))
{
return true;
}
else
{
return false;
}
}
public Iterable<Board> neighbors() // all neighboring boards
{
return new BoardIterator();
}
private class BoardIterator implements Iterable<Board>
{
public Iterator<Board> iterator()
{
return new BoardIterators();
}
private class BoardIterators implements Iterator<Board>
findAllNeighbor();
return new Iterable<Board>()
{
private Board currentBoard = new Board(locBlocks);
public boolean hasNext()
@Override
public Iterator<Board> iterator()
{
return true;
return new Iterator<Board>()
{
private int i = numOfNeighbor;
public boolean hasNext()
{
return (i > 0);
}
public void remove() {}
public Board next()
{
return neighborBoard[--i];
}
};
}
public void remove() {}
public Board next()
{
Board board = currentBoard.twin();
return board;
};
}

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("\n");
}
return s.toString();
}

// public String toString() // string representation of this board (in the output format specified below)

public static void main(String[] args) // unit tests (not graded)
{
int[][] initBoard = new int[3][3];
initBoard[0][0] = 0;
int[][] initBoard2 = new int[3][3];

initBoard[0][0] = 5;
initBoard[0][1] = 1;
initBoard[0][2] = 3;
initBoard[1][0] = 4;
initBoard[1][1] = 2;
initBoard[1][2] = 5;
initBoard[2][0] = 7;
initBoard[2][1] = 8;
initBoard[2][2] = 6;
initBoard[0][2] = 7;
initBoard[1][0] = 6;
initBoard[1][1] = 8;
initBoard[1][2] = 0;
initBoard[2][0] = 3;
initBoard[2][1] = 2;
initBoard[2][2] = 4;

initBoard2[0][0] = 1;
initBoard2[0][1] = 2;
initBoard2[0][2] = 3;
initBoard2[1][0] = 4;
initBoard2[1][1] = 5;
initBoard2[1][2] = 6;
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!");
}

if (testBoard.equals(ReferenceBoard))
{
System.out.println("Equal");
}
else
{
System.out.println("Not Equal");
}
System.out.println("twin of reference Board: " + (ReferenceBoard.twin()).toString());
for (Board neighborBoard : testBoard.neighbors())
{
System.out.println(neighborBoard.toString());
}
System.out.printf("\n hamming: %d", testBoard.hamming());
System.out.printf("\n manhattan: %d", testBoard.manhattan());
}
}
}
Binary file modified chapter4/Solver$1$1.class
Binary file not shown.
Binary file modified chapter4/Solver$1.class
Binary file not shown.
Binary file added chapter4/Solver$2$1.class
Binary file not shown.
Binary file added chapter4/Solver$2.class
Binary file not shown.
Binary file modified chapter4/Solver.class
Binary file not shown.
Loading

0 comments on commit 368417f

Please sign in to comment.