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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:42:51+00:00 2026-05-18T03:42:51+00:00

I’m trying to write a method that does the following: public class GridCounting {

  • 0

I’m trying to write a method that does the following:

public class GridCounting {
    /** Returns the total number of possible routes (paths) from
     * (x,y) to (tx,ty).
     * There are only three valid kinds of moves:
     *  Increment x by one.
     *  Increment x by two.
     *  Increment y by one.
     *
     *  Hint: You'll need to two base cases.
     */
    public static int count(int x,int y, int tx, int ty) {
        if (x==tx && y==ty)
            return 1;
        if (x>tx || y>ty)
            return 0;
        if (x<tx)
            return 1 + count(x+1, y, tx, ty);
        if (x<tx) //if x is still less, then we can do another move
            return 1 + count(x+2, y, tx, ty);
        if (y<ty){
            return 1 + count(x, y+1, tx, ty);
        }
        return 0;
    }
}

The trouble that I’m having is that I’m always off by +1. It expects 4 but I give it 5. The confusing part is that if I feed the function count(10,15,10,15), then that still counts as 1 move. I dont know how to account for this.

Also y++ then x++ counts as 1 move, and x++ then y++ counts as another move.
Edit: Fixed code:

public static int count(int x,int y, int tx, int ty) {
    if (x==tx && y==ty)
        return 1;
    if (x>tx || y>ty)
        return 0;
    if (x<tx)
        return count(x+1, y, tx, ty) + count(x+2, y, tx, ty) + count(x,y+1,tx,ty);
    if (y<ty) {
        return count(x, y+1, tx, ty); // what does this do?
    }
    return 0;
}
  • 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-18T03:42:51+00:00Added an answer on May 18, 2026 at 3:42 am

    The trouble that I’m having is that I’m always off by +1. It expects 4 but I give it 5.
    In position (x, y) we, generally speaking, have three choices (x+=1, x+=2, y+=1). Each of them produces separate paths and we need to find how many paths there’re in total.
    So, this part is wrong

            if (x<tx)
                return 1+ count(x+1, y, tx, ty);
            if (x<tx)
                return 1+ count(x+2, y, tx, ty);
            if (y<ty){
                return 1+ count(x, y+1, tx, ty);
            }
            return 0;
    

    That was a hint, let me know if you need more.

    Also y++ then x++ counts as 1 move, and x++ then y++ counts as another move.
    Well, that sounds like two different paths to me.

    edit
    Ok, return count(x + 1, y, tx, ty) + count(x + 2, y, tx, ty) + count(x, y + 1, tx, ty); is all you need.
    If there’re 3 paths starting with first move x + 1, 2 paths starting with first move x + 2 and 2 paths starting with first move y + 1, then, obviously, there’re 3 + 2 + 2 paths total.

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

Sidebar

Related Questions

No related questions found

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.