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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:50:53+00:00 2026-06-15T13:50:53+00:00

Once this is instantiated and method traverse(0,0) is invoked, it will automatically solve the

  • 0

Once this is instantiated and method traverse(0,0) is invoked, it will automatically solve the maze showing 7 as the path and 3 as “tried paths”.
I’m trying to understand this code but I get stuck at the first else statement in the traverse method.

public class Maze
{
    private final int TRIED = 3;
    private final int PATH = 7;
    private int[][] grid = { {1,1,1,0,1,1,0,0,0,1,1,1,1},
                             {1,0,1,1,1,0,1,1,1,1,0,0,1},
                             {0,0,0,0,1,0,1,0,1,0,1,0,0},
                             {1,1,1,0,1,1,1,0,1,0,1,1,1},
                             {1,0,1,0,0,0,0,1,1,1,0,0,1},
                             {1,0,1,1,1,1,1,1,0,1,1,1,1},
                             {1,0,0,0,0,0,0,0,0,0,0,0,0},
                             {1,1,1,1,1,1,1,1,1,1,1,1,1} };

public boolean traverse (int row, int column)
{
    boolean done = false;
    if (valid (row, column))
    {
       grid[row][column] = TRIED;
       // this cell has been tried
       if (row == grid.length-1 && column == grid[0].length-1)
          done = true;
          // the maze is solved
       else
       {
          done = traverse (row+1, column); 
          // down
          if (!done)
          done = traverse (row, column+1); 
          // right
          if (!done)
          done = traverse (row-1, column);
          // up

          if (!done)
          done = traverse (row, column-1);
          // left
       }
    if (done)
    // this location is part of the final path
    grid[row][column] = PATH;
    }
    return done;
}

//-----------------------------------------------------------------
//  Determines if a specific location is valid.
//-----------------------------------------------------------------
private boolean valid (int row, int column)
{
    boolean result = false;
    // check if cell is in the bounds of the matrix
    if (row >= 0 && row < grid.length && column >= 0 &&
       column < grid[row].length)
       // check if cell is not blocked and not previously tried
       if (grid[row][column] == 1)
       result = true;
       return result;
}

To my knowledge,

done = traverse (row+1, column); 

This line is a recursive call and will move it down once, and will run the method again but what if it’s not valid? Doesn’t the whole method stop? I don’t understand where the flow of control shifts after a certain spot is not valid.

For example, if [1][0] is not valid, does the control shift back to the [0][0] call and process the “going right” statement? Where and what does it mean by if (!done), i mean done only equals true when the maze is completely solved and if it is valid, done will equal true, so wouldn’t the whole thing just stop?

  • 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-15T13:50:54+00:00Added an answer on June 15, 2026 at 1:50 pm

    Here’s your method:

    public boolean traverse (int row, int column)
    {
        boolean done = false;
        if (valid (row, column))
        {
           ...
        }
        return done;
    }
    

    Therefore, if the point is not valid, it will skip all of the testing and just return false.

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

Sidebar

Related Questions

hi I am trying to get user profile information like this once the authentication
I'm trying to do this: #pragma once #include <fstream> #include <string> static std::ofstream ErrorLog;
I've done this before once, I'm trying to replicate what I did so far
I have an activity that starts after a certain time. Once this activity starts,
I was once asked of this in an interview: How to write a recursive
How do I make this so once the user inputs a number and presses
I think I've seen this done once, but I cannot find it again after
I got stumbled upon this issue once and I guess I need some expertise
We're getting this error once a day on a script that runs every two
Possible Duplicate: Getting specific days in a month I've touched on this problem once

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.