I am writting a program in which i have a board (3 x 3 matrix) and i have to swap the value in a particular row and column with its adjacent rows and column by logic that that if i have 0 value at [0,0] then i want to have two boards. One board with 0 value at [0,1] and [0,1]s value at [0,0] and the other board with 0 value at [1,0] and [1,0]s values at [0,0]. But after implementing the following code i have two boards with same value and i am not able to understand any explanation for these wrong values.
EDIT : Below i have two related classes and related methods for that. The problem lies with the neighbor method of the Board class. It seems when i create a board in the neighbor method it does not do what its suppose to do.
BOARD CLASS
public final class Board {
private final int dimen;
private final int[][] blocks;
public Board(int[][] blocks) // construct a board from an N-by-N array of blocks
// (where blocks[i][j] = block in row i, column j)
{
this.dimen = blocks.length;
this.blocks = new int[dimen][dimen];
for (int i = 0; i < dimen; ++i) {
for (int j = 0; j < dimen; ++j) {
this.blocks[i][j] = blocks[i][j];
System.out.println (this.blocks[i][j]);
}
}
...
...
public Iterable<Board> neighbors() // all neighboring boards
{
Stack<Board> neighborStack = new Stack <Board>();
int x = 0, y = 0;
outer : for (int i = 0; i < dimen; ++i){
for (int j = 0; j < dimen; ++j) {
if (this.blocks[i][j] == 0) {
x = i;
y = j;
break outer;
}
}
}
if (x == 0)
{
if (y == 0) {
int tmpBlocks1[][] = Arrays.copyOf (this.blocks, this.blocks.length );
int tmpBlocks2[][] = Arrays.copyOf (this.blocks, this.blocks.length );
tmpBlocks1[0][0] = tmpBlocks1[0][1];
tmpBlocks1[0][1] = 0;
tmpBlocks2[0][0] = tmpBlocks2[1][0];
tmpBlocks2[1][0] = 0;
Board tmpBoard1 = new Board (tmpBlocks1);
neighborStack.push (tmpBoard1);
Board tmpBoard2 = new Board (tmpBlocks2);
neighborStack.push (tmpBoard2);
}
SOLVER CLASS :
public final class Solver {
private MinPQ <SearchNode> pqOriginal;
private MinPQ <SearchNode> pqTwin;
Stack <Board> shortestBoardSequence = null;
int moves = 0;
public Solver(Board initial) // find a solution to the initial board (using the A* algorithm)
{
pqOriginal = new MinPQ<SearchNode>();
pqTwin = new MinPQ<SearchNode>();
pqOriginal.insert(new SearchNode (moves, initial, null) );
pqTwin.insert(new SearchNode (moves, initial.twin(), null) );
}
public boolean isSolvable() // is the initial board solvable?
{
SearchNode originalNode = null;
SearchNode twinNode = null;
Stack <Board> neighborBoards = null;
while (!pqOriginal.isEmpty() || !pqTwin.isEmpty()) {
originalNode = pqOriginal.delMin();
// shortestBoardSequence.push(originalNode.board);
neighborBoards = (Stack<Board>)originalNode.board.neighbors();
...
}
...
}
...
public static void main(String[] args) // solve a slider puzzle (given below)
{
// 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()) // SEE THE ISSOLVABLE
StdOut.println("No solution possible");
..
}
This probably is because you have a two-dimensional array and you are only copying one dimension.
By doing:
you are actually only copying the row references, not the row data. You are doing the equivalent of:
and so
tmpBlocksholds the same rows asblocks.You need to do what is called a
deep copyof the array.Example code to demonstrate the problem:
Prints:
Note that the change to 99 was overridden by the change to 88 demonstrating that the copy refers to the rows of the original. Only the deep copy was affected when 77 was used.