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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:20:27+00:00 2026-05-31T05:20:27+00:00

I been working on this code for a while and can’t seem to get

  • 0

I been working on this code for a while and can’t seem to get it to work. I have started over multiple times. I am not sure if my logic is off or if I could be doing something better. Any suggestion in the right direction or new ideas to try would be helpful. I understand that a maze could be solved recursively but for this part of the assignment I need to use a stack.

I created two stacks. One for the path the other for spots I already searched. Ideally I would check to see if the searched path contains the next spot in a direction. If it does it checks another direction.

Sample maze

0 1 0 1 0
0 0 0 1 0
0 1 0 0 0
0 1 0 1 1
0 1 0 0 0

My algorithm seems to get stuck between 2,3 and 2,4. Never explores 1,4 or 0,4. I see it keep bouncing between 2,3 and 2,4 in an infinite loop. So it seems my searched.contains() is not functioning properly. Any suggestion to fix my searched stack? Ideally, when I run my code. I want it to check East, South, west then North has already been searched or not. If all points have been checked it will pop the last position from my path stack using current= path.pop inside the while loop and repeat.

Position is a custom class. I have considered adding a previous position variable to the constructor in the position class but seems to not to be needed if I can get my path stack to work. If I am wrong on this please let me know.

public static Position [] stackSearch(char [] [] maze){
    //todo: your path finding algorithm here using the stack to manage search list
    //your algorithm should modify maze to mark positions on the path, and also
    //return array of Position objects coressponding to path, or null if no path found
     ArrayDeque <Position> path = new ArrayDeque<Position>();
     ArrayDeque <Position> searched = new ArrayDeque<Position>();


        //creates position object
        Position start = new Position(0,0,'0');
        Position current;
        Position north,south, east, west;

        int i = 0; int j = 0;
        //push (0,0) onto stack
        path.push(start);
        searched.push(start);
        while(!path.isEmpty()){
            current=path.pop();
            i=current.i;
            j=current.j;
            if(i==maze.length-1 && j==maze.length-1 &&  maze[i][j]=='0'){
                 Position[] trail= new Position [path.size()];
                while(!path.isEmpty()){
                    for(int k=0; k<path.size();k++){
                    trail[k]=path.pop();
                }
                    return trail;
            }
        }
            System.out.println(i +"," +j);

            //check east.
            east= new Position(i,j+1,'0');
            south= new Position(i+1,j,'0');
            west= new Position(i,j-1,'0');
            north= new Position(i-1,j,'0');
            if (j+1 >= 0 && j+1 < maze.length  && maze[i][j+1] == '0' && searched.contains(east)==false)
            {


                    searched.push(east);
                    path.push(current);
                    path.push(east);




            }
            //check south, add its position to the list.

            else if (i+1 >= 0 && i+1 < maze.length &&  maze[i+1][j] == '0' && searched.contains(south)==false)
            {


                    searched.push(south);
                    path.push(current);
                    path.push(south);


            }
          //check west.

             else if (j-1 >= 0 && j-1 < maze.length  && maze[i][j-1] == '0' && searched.contains(west)==false)
            {


                    searched.push(west);


                    path.push(current);
                    path.push(west);

            }              
            //check north

             else if (i-1 >= 0 && i-1 < maze.length &&  maze[i-1][j] == '0' && searched.contains(north)==false)
            {


                    searched.push(north);
                    path.push(current);
                    path.push(north);


            }

       }
    return null;
}
  • 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-31T05:20:28+00:00Added an answer on May 31, 2026 at 5:20 am

    I would guess that the problem lies, not in the code you posted, but rather in the Position class. If you have not overridden hashcode() and equals() in the Position class, the contains() comparison here will be looking for object equality.

    So when you ask if searched.contains(east), having just created east as a new object, it will return false, even though east‘s coordinates perfectly match what is already in your search.

    See this answer for more.

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

Sidebar

Related Questions

I've been working on this for a while now and I can't seem to
I have been working on this code but I can't find out what is
I've been working on this for a while and can't seem to solve the
i've been working with this for a while and i can't seem to figure
I've been glancing at this code for a while now, and I can't seem
So I have been working on this project for a short while now. I
I have been working on this for days but I don't get it so
I have been working on this problem for a while now. I am trying
I have been working on this for a while and am trying to implement
i've been working on this question since yesterday, i have the following code.. <?php

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.