通过递归计算可能的坐标移动

发布于 2024-10-01 12:14:50 字数 1360 浏览 0 评论 0原文

我正在尝试编写一个执行以下操作的方法:

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;
    }
}

我遇到的麻烦是我总是落后+1。它期望 4,但我给它 5。令人困惑的部分是,如果我输入函数 count(10,15,10,15),那么这仍然算作 1 次移动。我不知道如何解释这一点。

另外,先 y++ 然后 x++ 算作 1 步,先 x++ 然后 y++ 算作另一步。 编辑:固定代码:

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;
}

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夏の忆 2024-10-08 12:14:50

我遇到的麻烦是我总是落后+1。它期望 4,但我给它 5。
在位置 (x, y) 中,一般来说,我们有三种选择(x+=1x+=2y+= 1)。它们每个都会产生单独的路径,我们需要找出总共有多少条路径。
所以,这部分是错误的

        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;

这是一个提示,如果您需要更多,请告诉我。

另外,y++ 然后 x++ 算作 1 步,x++ 然后 y++ 算作另一步。
嗯,这对我来说听起来像是两条不同的道路。

编辑
好的,返回 count(x + 1, y, tx, ty) + count(x + 2, y, tx, ty) + count(x, y + 1, tx, ty); 是你所需要的一切。
如果有 3 条从第一次移动 x + 1 开始的路径,有 2 条从第一次移动 x + 2 开始的路径,以及 2 条从第一次移动 y + 1 开始的路径,那么显然总共有 3 + 2 + 2 条路径。

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.

三五鸿雁 2024-10-08 12:14:50

你确定这条线是对的吗?

if (x==tx && y==ty)
  return 1;

看起来它应该返回零。

在手机上,所以无法测试。

You sure this line is right?

if (x==tx && y==ty)
  return 1;

It seems like it should return zero.

On a phone so I can't test it.

半世蒼涼 2024-10-08 12:14:50

if (x==tx && y==ty) return 1; 听起来不正确。如果您已经到达目的地,则没有更多路线。

if (x==tx && y==ty) return 1; doesn't sound correct. If you're already at the destination, there are no more routes.

将军与妓 2024-10-08 12:14:50

也许我不理解规则,但我认为这是第一个基本情况导致了你的问题。如果您已经到达该位置,那么您就不会采取行动。

Maybe I not understanding the rules, but I think it's that first base case that's causing your problem. If you're already at the location then you're not making a move.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文