java计算递归步数
我想计算递归步骤的数量,并在达到一定限制时停止递归。
实际上,我正在处理河内塔问题,我想限制为解决该问题而执行的幻灯片数量。这是我的解决方案:
class HanoiNK{
public static void main(String args[]){
int n = 4;
int k = 5;
try{
slide(k, n, 'A', 'B', 'C');
}catch(Exception e){
System.out.println(e);
}
}
public static void slide(int counter, int height, char source,
char buffer, char destination) throws Exception{
if(counter > 0){
if(height == 1){
System.out.println("move "+ height +" from " +
source + " to " + destination);
}else{
counter--;
slide(counter, height - 1, source, destination, buffer);
System.out.println("move "+ hoehe +" from " +
source + " to " + destination);
counter--;
slide(counter, height - 1, buffer, source, destination);
}
}else{
throw new Exception("stop here");
}
}
}
这是实例: http://ideone.com/xeN4x
我的问题是我得到
move 1 from A to B
move 2 from A to C
move 1 from B to C
move 3 from A to B
move 1 from C to A
move 2 from C to B
java.lang.Exception: stop
了输出。但应该进行 5 张而不是 6 张幻灯片。有什么想法吗?
I want to count the number of recursion steps and stop the recursion when a certain limit is reached.
Actually I am dealing with the Tower of Hanoi problem and I want to limit the number of slides that are performed to solve the problem. Here is my solution:
class HanoiNK{
public static void main(String args[]){
int n = 4;
int k = 5;
try{
slide(k, n, 'A', 'B', 'C');
}catch(Exception e){
System.out.println(e);
}
}
public static void slide(int counter, int height, char source,
char buffer, char destination) throws Exception{
if(counter > 0){
if(height == 1){
System.out.println("move "+ height +" from " +
source + " to " + destination);
}else{
counter--;
slide(counter, height - 1, source, destination, buffer);
System.out.println("move "+ hoehe +" from " +
source + " to " + destination);
counter--;
slide(counter, height - 1, buffer, source, destination);
}
}else{
throw new Exception("stop here");
}
}
}
Here is the live example: http://ideone.com/xeN4x
My problem is that I get
move 1 from A to B
move 2 from A to C
move 1 from B to C
move 3 from A to B
move 1 from C to A
move 2 from C to B
java.lang.Exception: stop
as output. But 5 and not 6 slides should be performed. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题是您正在测试 counter 是否大于或等于 一,然后将其递减 二。
这里计数器可能会变为负数。你需要检查一下。
The problem is you are testing if counter is greater than or equal to one, but then decrementing it by two.
Here counter can go negative. You need to check for that.
由于您想要计算移动次数而不是递归深度,因此您需要存储每个步骤中进行的移动次数。像这样:
然后,您将获得预期的结果:
Since you want to count the number of moves and not the recursion depth, you need to store the number of moves that were made in each step. Something like this:
Then, you obtain the expected result:
递归调用
counter
次,其中有counter---;
两次,因此在许多情况下它不会满足条件这是一个示例方法,将在代码中
Here is an sample method that will be recursively get called
counter
timesin your code there are
counter--;
twice so it won't met condition in many case您必须在每次减量时测试您的计数器,如下所示:
You have to test for your counter at every decrements like this: