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 6902439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:47:53+00:00 2026-05-27T07:47:53+00:00

I’m working on a programming assignment (in java) to solve a fifteen-puzzle sort of

  • 0

I’m working on a programming assignment (in java) to solve a fifteen-puzzle sort of thing. The first part is to use depth first search to find a solution. I want it to be able to solve an arbitrarily large puzzle, so the whole state space graph may not fit in memory. Instead, the algorithm will keep a list of explored states to check future states against. The algorithm goes something like this:

If we’re in the goal state, then we’re done.
If not, then for each neighbor that is not in the explored list, add it to the explored list and recursively depth first search it.

However, it’s not working properly. I did some tests and I believe the problem is that when I insert a state into the explored list, it actually inserts the same object, so that when I change the state later the explored list changes as well. Because of this, it never appears to the program that a state has not been explored.

I tried to write a method to copy states, and pass a copy to the explored list; but the same problem persists. However, when I tested the copy method independently, it appears to successfully copy states. I’m totally baffled as to what the problem is.

There’s a lot of code involved, and I’m not sure where the problem is, but here’s the code for the depth first search:

this class we're in is the puzzle class, and this is the depth first search
'state' keeps track of the current state we're in
'explored' is a linked list of explored states

public String depthFirst(){
    state.clearPath();            // state keeps track of the path taken
    explored = new LLNode(state); // create explored list with starting state
    this.dfs();                   // depth first search
    return state.path();
}

private boolean dfs(){
    if(state.equals(goal))        // if we're in the goal state, then done
        return true;
    char[] directions = {'n', 'e', 's', 'w'};
    for(char dir : directions){   // otherwise, for each direction
        if(state.move(dir)){      // if we can move in that direction, then do
            if(!explored.contains(state)){  // if the new state is unexplored
                explored = explored.insert(state.copy());
                if(this.dfs())              // add it to explored and recurse
                    return true;
            }
            switch(dir){                    // if search failed, move back and
                                            // search the next direction
                case 'n': state.move('s');
                case 'e': state.move('w');
                case 's': state.move('n');
                case 'w': state.move('e');
            }
        }
    }
    return false;  // indicates failure
}

Here are the insert and contains methods for explored:

public LLNode insert(Node gameState){
    if(state == null){        // if this list node doesn't have a state
        state = gameState;    // then give it this one
        return this;
    }                         // otherwise tack it on the front of the list
    return new LLNode(gameState, this);  // and return the new head
}

public boolean contains(Node gameState){
    // I had a test here that printed a message if (state == gameState)
    // which was triggered when I ran the program, suggesting the problem
    // I explained above
    if(state.equals(gameState))
        return true;
    else if(next == null)
        return false;
    else
        return next.contains(gameState);
}

Here is the copy function for the Node class

public Node copy(){
    int[][] newState = new int[state.length][state[0].length];
    for(int i = 0; i < state.length; ++i)
        for(int j = 0; j < state[0].length; ++j)
            newState[i][j] = state[i][j];
    return new Node(newState);
}

Thanks for any ideas!

  • 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-05-27T07:47:54+00:00Added an answer on May 27, 2026 at 7:47 am

    I’d check that state.move() doesn’t reference explored at all – looking at the supplied code, that’s the only place I can imagine your variables could be referencing the same arrays.

    Was it a requirement of the assignment to roll your own data structures (i.e. Linked list)? If not then I’d go with a standard Java HashSet (or whatever best suits your requirements), as I cannot see the need to remember the ordering of the search, only the fact that you’ve already explored a particular state.

    Also, is it necessary to store the whole array for every explored state? It might be better (consume less memory and be generally more efficient) to just remember a hash for each explored state (which you could store in a HashSet).

    Note: Your copy() method assumes an NxN array (which is fine for your example, but wouldn’t work if the puzzle was rectangular).

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have thousands of HTML files to process using Groovy/Java and I need to
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I'm making a simple page using Google Maps API 3. My first. One marker

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.