通过递归计算可能的坐标移动
我正在尝试编写一个执行以下操作的方法:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我遇到的麻烦是我总是落后+1。它期望 4,但我给它 5。
在位置
(x, y)
中,一般来说,我们有三种选择(x+=1
、x+=2
、y+= 1)。它们每个都会产生单独的路径,我们需要找出总共有多少条路径。
所以,这部分是错误的
这是一个提示,如果您需要更多,请告诉我。
另外,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
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 movex + 2
and 2 paths starting with first movey + 1
, then, obviously, there're3 + 2 + 2
paths total.你确定这条线是对的吗?
看起来它应该返回零。
在手机上,所以无法测试。
You sure this line is right?
It seems like it should return zero.
On a phone so I can't test it.
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.也许我不理解规则,但我认为这是第一个基本情况导致了你的问题。如果您已经到达该位置,那么您就不会采取行动。
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.