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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:08:56+00:00 2026-06-14T10:08:56+00:00

I am having trouble with a recursive Java program and currently the problem is

  • 0

I am having trouble with a recursive Java program and currently the problem is that I do not understand is why the base case1 if is executing. In the initial method call to findPath, the values 2 and 0 are being passed. exitRow = 4 and cols-1 = 11. So, my understanding is that this base case should not enter the if statement first thing since the two locations in maze[][] are not the same (maze[4][11] != maze[2][0]). But that is exactly what it is doing. I obviously have missed something in my understanding of the if structure or I have an error elsewhere and would appreciate some help.

Note: I also tried

if (row == exitRow && col == cols-1)

but this ended up giving me a stack overflow. And from what little I understand about that, it means that either my recursion is not bringing me closer to the base case or the base case is unreachable because of the way it is written. I am assuming that my recursion is correct based on this guide http://www.cs.bu.edu/teaching/alg/maze/ that I have been using. Which leads me to believe that the base case is the issue.

Many thanks.

private Boolean findPath(int row, int col)
{
    //base case 1
    if (maze[exitRow][cols-1]==maze[row][col])
    {
        System.out.println("test");//for debugging
        return true;
    }
    //base case 2
    if (maze[row][col] == '#')
    {
        return false;
    }

        maze[row][col] = 'O';
        System.out.println("test1");//for debugging
        steps++;

        //check north
        if (findPath(row+1,col)==true )
        {
        return true;
        }
        //check east
        if (findPath(row,col+1)==true  )
        {
            System.out.println("test2");
        return true;
        }    
        //check south
         if (findPath(row-1,col)== true)
        {
        return true;
        } 
        //check west 
        if (findPath(row,col-1)== true)
        {
        return true;
        }

        System.out.println(steps);
        maze[row][col] = '.';//unmark location

    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-06-14T10:08:57+00:00Added an answer on June 14, 2026 at 10:08 am
    if( maze[exitRow][cols-1]==maze[row][col] )
    

    will return true if the current tile is of the same type as the exit tile. Since the start and the exit are both floor (.), the function returns immediately. Stick to this:

    if (row == exitRow && col == cols-1)
    

    It is the correct test to see if you have found the exit. Now for the overflow:

    You mark the tiles you enter as O, but you never test the mark if it’s present, so you walk north-south-north(at this point you remark)-south… . Since you already test for a wall (if (maze[row][col] == '#')), test for either

    • anything but unmarked floor: if (maze[row][col] != '.')
    • a wall or a mark: if (maze[row][col] == '#' || maze[row][col] == 'O')

    note that you can treat both cases the same, since the effect is the same – don’t go there.

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

Sidebar

Related Questions

I am having trouble coming up with a recursive method that can solve fully
Problem: I am having trouble implementing a recursive image lazy load in all relevant
I am having a lot of trouble with this basic recursion problem in java;
I'm having trouble getting my head around a problem on recursive nested lists. The
I have a recursive function with a bug that I am having trouble with.
I'm having trouble trying to understand how to use recursion with this problem. I'm
Having trouble with recursive methods in c#. When compiled it should just display the
Hey I'm having some trouble understanding how Recursive Algebraic Types work and how to
Having trouble with proper regex for RewriteCond RewriteCond %{REQUEST_URI} !^/foo/ Works as expected, that
I'm having trouble finding a simple statement to skip the duplicates for this recursive

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.