java计算递归步数

发布于 2024-10-09 13:39:19 字数 1563 浏览 3 评论 0原文

我想计算递归步骤的数量,并在达到一定限制时停止递归。

实际上,我正在处理河内塔问题,我想限制为解决该问题而执行的幻灯片数量。这是我的解决方案:

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 技术交流群。

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

发布评论

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

评论(4

难理解 2024-10-16 13:39:19

问题是您正在测试 counter 是否大于或等于 ,然后将其递减

counter--;
// ...
counter--;

这里计数器可能会变为负数。你需要检查一下。

The problem is you are testing if counter is greater than or equal to one, but then decrementing it by two.

counter--;
// ...
counter--;

Here counter can go negative. You need to check for that.

羅雙樹 2024-10-16 13:39:19

由于您想要计算移动次数而不是递归深度,因此您需要存储每个步骤中进行的移动次数。像这样:

    public static int slide(int counter, int hoehe, char quelle,                           char ablage, char ziel)
throws Exception{         
    if (hoehe == 1) {          
        System.out.println("move "+ hoehe +" from " +                                             
                quelle + " to " + ziel);   
        if (--counter == 0) throw new Exception("hier stoppen"); 
    } else {     
        counter = slide(counter, hoehe - 1, quelle, ziel, ablage);     
        System.out.println("move "+ hoehe +" from " +          
                quelle + " to " + ziel);             
        if (--counter == 0) throw new Exception("hier stoppen"); 
        counter = slide(counter, hoehe - 1, ablage, quelle, ziel);       
    }     
    return counter;
}

然后,您将获得预期的结果:

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
java.lang.Exception: hier stoppen

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:

    public static int slide(int counter, int hoehe, char quelle,                           char ablage, char ziel)
throws Exception{         
    if (hoehe == 1) {          
        System.out.println("move "+ hoehe +" from " +                                             
                quelle + " to " + ziel);   
        if (--counter == 0) throw new Exception("hier stoppen"); 
    } else {     
        counter = slide(counter, hoehe - 1, quelle, ziel, ablage);     
        System.out.println("move "+ hoehe +" from " +          
                quelle + " to " + ziel);             
        if (--counter == 0) throw new Exception("hier stoppen"); 
        counter = slide(counter, hoehe - 1, ablage, quelle, ziel);       
    }     
    return counter;
}

Then, you obtain the expected result:

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
java.lang.Exception: hier stoppen
美人骨 2024-10-16 13:39:19

递归调用 counter 次,其中有 counter---; 两次,因此在许多情况下它不会满足条件

public void callMe(int counter){
      if(counter == 1 ){
             return;
      }else{
             callMe(--counter);
      }

}

这是一个示例方法,将在代码中

Here is an sample method that will be recursively get called counter times

public void callMe(int counter){
      if(counter == 1 ){
             return;
      }else{
             callMe(--counter);
      }

}

in your code there are counter--; twice so it won't met condition in many case

凉薄对峙 2024-10-16 13:39:19

您必须在每次减量时测试您的计数器,如下所示:

public static void slide(int counter, int hoehe, char quelle,
                      char ablage, char ziel) throws Exception{
    if(hoehe == 1){
        System.out.println("move "+ hoehe +" from " +
                                        quelle + " to " + ziel);
    }else{
        if (--counter == 0) throw new Exception("hier stoppen");
        slide(counter, hoehe - 1, quelle, ziel, ablage);
        System.out.println("move "+ hoehe +" from " +
                                        quelle + " to " + ziel);
        if (--counter == 0) throw new Exception("hier stoppen");
        slide(counter, hoehe - 1, ablage, quelle, ziel);
    }
}

You have to test for your counter at every decrements like this:

public static void slide(int counter, int hoehe, char quelle,
                      char ablage, char ziel) throws Exception{
    if(hoehe == 1){
        System.out.println("move "+ hoehe +" from " +
                                        quelle + " to " + ziel);
    }else{
        if (--counter == 0) throw new Exception("hier stoppen");
        slide(counter, hoehe - 1, quelle, ziel, ablage);
        System.out.println("move "+ hoehe +" from " +
                                        quelle + " to " + ziel);
        if (--counter == 0) throw new Exception("hier stoppen");
        slide(counter, hoehe - 1, ablage, quelle, ziel);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文