Recursive method to solve the Towers of Hanoi (illustration)

Recursive method to solve the Towers of Hanoi (illustration)

Problem Description
has three rods right and left (left, mid, right), in which one (from) the bottom up, are placed in descending order num gold plate. Goal of the game: the golden plate, the rod (from) all moved to another bar (to) on, and still maintain the original order folded.
Operation rules: a plate can only be moved, and the three bars is always maintained below the market during the movement, on the small cap.
As shown: the upper left pole num discs moved to the right pole.
From top to bottom as 1, 2, 3, ..., num-1, num a plate Here Insert Picture Description
Here Insert Picture Description
for example
from as left, to as mid, num 3. (i.e., three plates moving from left to center)
moving method:
Move. 1 from left to MID
Move 2 from left to right
Move. 1 from MID to right
Move. 3 from left to MID
Move. 1 from right to left
Move 2 from right MID to
Move. 1 from left to MID
recursive method
(bottom recursive) recursive head: when these num left a direct target to move on a pole, and then return.
Recursive body: from there (the starting rod) and to (termination bar), the other pole is disposed Another, num-1 will be moved from FROM Another discs, then the remaining one of the largest num disc to move, Finally, the disks were moved to the num-1 to ...
, and an output
Enter the number of mobile plate num, and start rod and from the rod to the target;
output moving method and a mobile number.
Reference Code

import java.util.Scanner;
public class HanNuoTa {
 public static void main(String[] args) {
  Scanner cs = new Scanner(System.in);
  System.out.print("盘子个数: "); 
  int num =cs.nextInt();           //输入盘子个数
  System.out.print("起始位置: ");
  String from = cs.next();         //输入起始杆,from,mid 或 right
  System.out.print("终止位置: ");
  String to = cs.next();           //输入目标杆,from,mid 或 right
  System.out.println(Pro(num,"left","mid","right",from,to));
  
 }
  static int Pro(int num, String left, String mid, String right, String from, String to) {
  if(num<1) return 0;
  else return process(num,left,mid,right,from,to);
  }
  static int process(int num, String left, String mid, String right, String from, String to) {
   if(num==1) {
    System.out.println("move "+ num +" from "+ from +" to "+ to);
    return 1;  //返回移动次数
   }
   //下面为递归体,先要找到除from和to的另一个杆子
   //我分为两步去找,如果from和to有一个是mid杆子,则另一个杆子不是left就是right
   //否则,另一个杆子就是mid
   //然后开始移动
   if(from.equals(mid)||to.equals(mid)) {
    String another = (from.equals(left)||to.equals(left))?right:left;
    //找到另一个杆子another
    int part1 = process(num-1,left,mid,right,from,another);
    //将num-1个圆盘从from移到another
    int part2 = 1;
    System.out.println("move "+ num +" from "+ from +" to "+ mid);
    //将剩余一个最大的圆盘num移到to
    int part3 = process(num-1,left,mid,right,another,to);
    //最后将num-1个圆盘从another移到to
    return part1+part2+part3;  //返回移动次数
   }
   else { 
    //此时另一个圆盘就是mid
    int part1 = process(num-1,left,mid,right,from,mid);
    int part2 = 1;
    System.out.println("move "+ num +" from "+ from +" to "+ to);
    int part3 = process(num-1,left,mid,right,mid,to);
    return part1+part2+part3;
   }
  }
}

Run
Here Insert Picture Description
think
now modify the rules of the game: now limit can not be moved from the left-most column directly to the far right, do not move directly from the far right to the far left, but must go through the middle. When seeking tower N layer when the printing movement and most optimum total number of steps moved. ( Click here to view )

Personal Learning lessons learned, any questions, please correct me.

Released four original articles · won praise 3 · Views 158

Guess you like

Origin blog.csdn.net/weixin_44272266/article/details/105086437