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

  • Home
  • SEARCH
  • 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 3342862
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:53:18+00:00 2026-05-18T00:53:18+00:00

I just can’t get the hang of recursion, especially with complicated examples. I would

  • 0

I just can’t get the hang of recursion, especially with complicated examples. I would really appreciate it if someone would take some time to explain it. I literally have 4 pieces of paper all filled with me tracing this function, but I have no idea how to put it together.

public static String shortestPath(int x, int y, int tX, int tY,boolean blocked[][]) {

        if(x>blocked.length-1 || y>blocked[0].length-1 || x<0 || y<0 )
            return null;
        if(blocked[x][y]==true)
            return null;
        if(x==tX && y==tY)
            return "";

        String paths[]=new String[4];
        blocked[x][y]=true; //this just means this coordinate is blocked, so dont use it
        paths[0]=shortestPath(x, y+1, tX, tY, blocked);
        paths[1]=shortestPath(x, y-1, tX, tY, blocked);
        paths[2]=shortestPath(x+1, y, tX, tY, blocked);
        paths[3]=shortestPath(x-1, y, tX, tY, blocked);
        blocked[x][y] = false;
        int result=findShortestString(paths, 0, 3); 
//findShortestString just takes an array of strings, 
//with 0 being the lo index and 3 being the hi, 
//and returns the index that contains the string with the shortest length.
        //5
        if(paths[result]==null)
           return null;
        else{

           if(result==0)
                return 'N' + paths[result];
           if(result==1)
                return 'S' + paths[result];
           if(result==2)
                return 'E' + paths[result];
           if(result==3)
                return 'W' + paths[result];}

        return paths[result];

So what this code does is, given an x and y parameter, it tells you the shortest combination of moves you would have to make (NSWE for north, south, west, east) in order to reach the tX and tY parameters. The code works perfectly, but I have no idea how.

When I try to trace what paths[0] computes, it always comes out to null, because y will always keep incrementing until it goes out of bounds, in which it returns null. This is the same case for paths[1] [2] and [3], they all return to null, don’t they? So then how the heck is this function working?

  • 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-18T00:53:19+00:00Added an answer on May 18, 2026 at 12:53 am

    First try it with a trivially small example – a 1×1 grid with no blocked cells. As expected, there are no moves to be made. x == tX and y == tY so you return empty string. Good so far.

    Now look at a 2×2 grid where you are in the NW corner and the destination is NE.

    | @ | ^ |
    | o | o |
    

    When you try to go east and set paths[0] it invokes shortestPath, blocking off your current cell and setting your new location to the one below you. Now you have

    | X | ^ |
    | @ | o |
    

    In that invocation, it’s going to try to go N, W, S and E. Ignore for simplicity that going east happens before west, so we can finish up the other 3 directions right away – they all invoke again shortestPath on an invalid location (2 out of bounds, 1 you’ve been to) and return null immediately. You are left going east with a new grid and location like this:

    | X | ^ |
    | X | @ |
    

    Again, 3 of the directions return null. Only north will give you the end result you want. When you try to go there, you once again invoke shortestPath which immediately returns empty string because the board now looks like this:

    | X | @^ |
    | X | X  |
    

    Now we get to wrap up the call stack:

    1. Because paths[1] was empty string and the other 3 were null, result is 1 (I assume that’s how your string function works). So you return "N" to the previous call.
    2. The previous call is going to show that paths[0] == null, paths[1] == null, paths[3] == null but critically paths[2] is "N". Therefore result will be 2, and you will return "EN" to the earlier call.

    Since now you are returning to the very first invocation of shortestPath, that wraps up the first choice we made – going south from the start position. But we also had 1 more choice – go east. So you follow that tree out and it is simply "".

    Then comes the final step, where you see which string is smaller and get that "" is of course smaller than "EN". So result is 2, and therefore you prefix the string with "E", and "E" is your final answer.

    Now use that to figure out the larger examples. It helps to draw a decision tree and the state of the board at each node. And as you get to the leaves, draw arrows going back up to the parent node representing return values. That will help immensely.

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

Sidebar

Related Questions

No doubt I'm missing something really simple here but I just can't see the
This shouldn't be so hard but I just can't seem to get this to
Seems like as really simple thing to do, but I just can't track it
I just can't get this thing to work in javascript. So, I have a
I just can't seem to figure this out - I'm trying to get the
I just can't remember the terminology used for this and other related properties. EDIT
Today somebody told me that interface implementation in C# is just Can-Do relationship, not
I'm using GDI+ in a C++/MFC application and I just can't seem to avoid
I know this maybe a basic question but I just can't seem to find
I personally used Mercurial and Subversion in a limited way and I just can't

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.