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

The Archive Base Latest Questions

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

I am trying to implement a breadth first traversal for a maze. This is

  • 0

I am trying to implement a breadth first traversal for a maze. This is the code I have so far using a linked list but I am not sure if it is searching breadth first. Is this the proper way to do it? any suggestions, comments?

    public boolean traverseBreadth(){
    //traverse the floor from entrance to exit
    //stepping only on red tiles
    steps = new LinkedList<Tile>();
    possibleSteps = new LinkedList<Tile>();
     //reset markings
     reset();
    //push the entrance onto the stack
    entrance.setVisited();
    steps.add(entrance);

    System.out.println("add " + entrance);
    nextMoves(entrance);
    //keep going as long as we have a possibility to move
    //and we haven't reached the end yet
    while (!possibleSteps.isEmpty()&& (!possibleSteps.getLast().equals(exit)))
    {   
        Tile x = possibleSteps.removeLast();

        x.setMarked();   //walked on that square
        steps.add(x);  //walk to that square
        System.out.println("Walked to the square  " + x);
        //now figure out where you can walk from this square
        nextMoves(x);
        try {
               Thread.currentThread().sleep(1000);
               }
             catch (InterruptedException e) {
               e.printStackTrace();
               }





    }
    if (possibleSteps.getLast().equals(exit)){
        steps.push(possibleSteps.removeLast());
        System.out.println("made it from entrance to exit");
        System.out.println(steps.toString());
        return true;
    }
    else 
    {   JOptionPane.showMessageDialog(null,"sorry can't reach the exit");
        return false;
    }

}
  • 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-27T22:43:21+00:00Added an answer on May 27, 2026 at 10:43 pm

    This isn’t a breadth first search – this is depth first.

    There are 2 places that are obviously depth first

    • you remove from the end of the frontier (possibleTiles), not the beginning.
    • you do not have a traversal hierarchy (parent to child) to rebuild the traversal path, only a single “path” with tiles. Therefore, it is depth first.

    Things to change:

    • instead of LinkedList, change that to Dictionary. The key of the dictionary will be the tile, and the value will be the tile’s parent. Use this to reconstruct your final path.
    • your “moveNext” function is probably right. Make sure it is pushing to the end of the List.
    • do not pop (getLast()) from PossibleSteps. PossibleSteps should be a First-In-First-Out queue. Retrieve the first Tile of PossibleSteps (this will explore the tiles closest to the start first).

    Things to improve:

    • instead of using Tile to track exploration, use a Set (call it ExploredTile) where you put tiles that you have traversed)
    • use a Queue instead of List.

    Some C#/Pseudo Code (cuz I’m not at an IDE)

    Dictionary<Tile,Tile> path = ...;
    Queue<Tile> frontier = ...;
    Set<Tile> explored = ...;
    
    path[start] = null;
    
    while(frontier.Count > 0){
        var tile = frontier.Dequeue();
    
        if(explored.Contains(tile)){
            continue;
        }
        explored.add(tile);
    
        if (Tile == Exit){
            rebuildPath(Exit);
        }else{
            for (Tile t in Tile.neighbours){
                if (!explored.Contains(t)){
                    frontier.Enqueue(t);
                    path[t] = tile; // Set the path hierarchy
                }
            }
        }
    }
    

    RebuildPath

    LinkedList<Tile> finalPath = ...;
    
    Tile parent = path[exitTile];
    finalPath.push(exitTile);
    
    while(parent != null){
        finalPath.push(parent);
        parent = path[parent];
    }
    
    finalPath.reverse();
    

    Hopefully I got it right… 🙂

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

Sidebar

Related Questions

Trying to implement NSCopying for the first time, and I have a question about
Trying to implement this gallery on my website. http://coffeescripter.com/code/ad-gallery/ It is noted in the
iam trying to implement an Searchmachine into my site. Iam using Zend_Search_Lucene for this.
Im trying to implement a UnitofWork pattern using this Scott Allen tutorial My current
I am trying implement the Data transformation using Reflection 1 example in my code.
I'm trying implement my project in Apache Struts 2 but I'm not very familiar
Im trying to implement pagination using multiple searching criteria. Supposed I Have student table.
Im trying to implement this code: http://wcf.codeplex.com/wikipage?title=Getting%20started:%20Building%20a%20simple%20web%20api and want to add it to my
and I am trying to now do a Breadth First Order for files but,
Trying to implement Piwik using REST API over http but need a little help.

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.