【问题标题】:Recursion function to count no. of steps in tower of hanoi递归函数计数。河内塔的台阶
【发布时间】:2020-05-16 07:20:49
【问题描述】:

编写一个递归函数,返回解决 N 个盘的河内塔问题所需的步数。

输入 n=3 输出 7

这是代码-

private static int counttoh(int n,String T1,String T2,String T3)
 {int count=0;

     if(n==0)
     return 1; 


     count+=counttoh(n-1,T1,T3,T2);
     count+=counttoh(n-1,T3,T2,T1);

     return count;
 }

我的函数 counttoh 给出 4 作为输出,如果我正在执行 return count-1 它给出 1 ,任何人都可以帮助我解决这个问题。

【问题讨论】:

    标签: recursion towers-of-hanoi


    【解决方案1】:

    为了解决河内塔,需要将所有环移动到底部的顶部(n - 1),然后移动底部的环(+ 1),然后将所有其他环移回顶部(再次n - 1) .

    public static int countMoves(int n) {
    
        if (n == 0) return 0;
    
        // count(n-1) + 1 + count(n-1)
        return 2 * countMoves(n - 1) + 1;
    }
    

    【讨论】:

      【解决方案2】:
      #include <stdio.h>
      
      int move(int n, char source, char destination, char spare)
      { int count = 1;
      if (n==1){
          printf("\n\nMove a disc from %c to %c", source, destination);
      }
      else{
          count += move(n-1, source, spare, destination);
                   move(1, source, destination, spare);
          count += move(n-1, spare, destination, source);
      }
      return count;
      }
      
      int main()
      {
      int n, steps;
      char A, B, C;
      printf("Enter the number of disc :- ");
      scanf("%d", &n);
      printf("\nHere A is source, B is destination, C is spare.");
      steps = move(n, 'A', 'B', 'C');
      printf("\n\nNumber of steps taken is %d", steps);
      return 0;
      }
      

      【讨论】:

      • 虽然这段代码 sn-p 可以解决问题,但它没有解释为什么或如何回答这个问题。请include an explanation for your code,因为这确实有助于提高您的帖子质量。请记住,您正在为将来的读者回答问题,而这些人可能不知道您的代码建议的原因。您可以使用edit 按钮改进此答案以获得更多选票和声誉!
      【解决方案3】:

      具有步数的河内塔 - 递归方法 代码 C++

      long long toh(int N, int from, int to, int aux) {
          int count=1;
          if(N==0){
              return 0;
          }
          count+=toh(N-1, from, aux, to);
          cout<<"move disk "<<N<<" from rod "<<from<<" to rod "<<to<<endl;
          count+=toh(N-1, aux, to, from);
      
          return count;
      
      }
      

      【讨论】:

        【解决方案4】:
        public long toh(int N, int from, int to, int aux) 
        {
            int count = 1;
                
            if(N == 1)
            {
                System.out.println("move disk "+N+" from rod "+from+" to rod "+to);
                return count;
            }
            else
            {
                count +=toh(N-1, from, aux, to);
                System.out.println("move disk "+N+" from rod "+from+" to rod "+to);
                count +=toh(N-1, aux, to, from);
            }
            return count;
        }
        

        【讨论】:

        • 虽然这段代码可以回答这个问题,但最好包含一些上下文,解释它是如何工作的以及何时使用它。从长远来看,纯代码的答案没有用处。
        【解决方案5】:

        long long toh(int N, int s, int d, int h) { 长长cnt=1; // 计算步数

            if(N==0) {
                return 0;
            }
            cnt += toh(N-1,s,h,d);
            cout<<"move disk "<<N<<" from rod "<<s<< " to rod "<<d<<endl;
            cnt += toh(N-1,h,d,s);
        
            return cnt; 
        }
        

        【讨论】:

        • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-11
        • 1970-01-01
        • 1970-01-01
        • 2013-08-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多