Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8588869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:54:32+00:00 2026-06-11T22:54:32+00:00

I am writting a program in which i have a board (3 x 3

  • 0

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");
    ..
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T22:54:33+00:00Added an answer on June 11, 2026 at 10:54 pm

    This probably is because you have a two-dimensional array and you are only copying one dimension.

    By doing:

    int tmpBlocks1[][] = Arrays.copyOf (blocks, blocks.length );
    

    you are actually only copying the row references, not the row data. You are doing the equivalent of:

    int[] row0 = {0,0,0};
    int[] row1 = {0,0,0};
    int[] row2 = {0,0,0};
    int[][] blocks = {row0, row1, row2};
    int[][] tmpBlocks1 = {row0, row1, row2};
    

    and so tmpBlocks holds the same rows as blocks.

    You need to do what is called a deep copy of the array.

    Example code to demonstrate the problem:

    public final class Test<T> {
      int[][] blocks = {{0,1,2},{10,11,12},{20,21,22}};
      int[][] copyOfBlocks = Arrays.copyOf(blocks, blocks.length);
      int[][] deepCopyOfBlocks = {
        Arrays.copyOf(blocks[0], blocks[0].length),
        Arrays.copyOf(blocks[1], blocks[1].length),
        Arrays.copyOf(blocks[2], blocks[2].length)
      };
    
      public void test() {
        System.out.println("Before");
        System.out.println("Blocks: "+Arrays.deepToString(blocks));
        System.out.println("Shallow Copy: "+Arrays.deepToString(copyOfBlocks));
        System.out.println("Deep Copy: "+Arrays.deepToString(deepCopyOfBlocks));
    
        // Change blocks and copy and deep copy.
        blocks[0][0] = 99;
        copyOfBlocks[0][0] = 88;
        deepCopyOfBlocks[0][0] = 77;
    
        System.out.println("After");
        System.out.println("Blocks: "+Arrays.deepToString(blocks));
        System.out.println("Shallow Copy: "+Arrays.deepToString(copyOfBlocks));
        System.out.println("Deep Copy: "+Arrays.deepToString(deepCopyOfBlocks));
      }
    
      public static void main(String[] args) throws InterruptedException {
        try {
          Test test = new Test();
          test.test();
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }
    

    Prints:

    Before
    Blocks: [[0, 1, 2], [10, 11, 12], [20, 21, 22]]
    Shallow Copy: [[0, 1, 2], [10, 11, 12], [20, 21, 22]]
    Deep Copy: [[0, 1, 2], [10, 11, 12], [20, 21, 22]]
    After
    Blocks: [[88, 1, 2], [10, 11, 12], [20, 21, 22]]
    Shallow Copy: [[88, 1, 2], [10, 11, 12], [20, 21, 22]]
    Deep Copy: [[77, 1, 2], [10, 11, 12], [20, 21, 22]]
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have in my program a struct type called Square which is used to
I have been writing a program which asks you to input two integers, which
I'm writting a small graphics program using directx9 which imports some model files created
Situation: I have a C# program which does the following: Generate many files (replacing
I'm currently writing a program which will need to incorporate writing its output to
I'm writing a VisualC++ program which have code invoke ffmpeg.exe to convert video file.
I am writing a MFC program in which I have a a lot of
I'm writing a program which must download documents from google docs. I have downloaded
I am currently writing a program which takes user input and creates rows of
Good day! I have met problem with synchronizing threads. I am writing program which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.