Java Hanoi Hanoi recursive, non-recursive (imitation system recursive) and non-recursive law implementation code _java

Source: Internet
Author: User
The procedure is as follows:
Copy Code code as follows:

View Code
/*
* Hanoi Tower Game Problem Description:
* Hanoi: Hanoi (also known as Heneta) is a puzzle toy from an ancient Indian legend.
* When the great Brahma created the world, he made three diamond pillars, on a pillar, down the
* The order of magnitude is stacked with 64 pieces of gold discs. Brahma commands the Brahman to set the disc from below to the size
* The order is rearranged on another pillar. It is also stipulated that the disc cannot be enlarged on a small disc,
* Only one disc can be moved between three pillars at a time.
*
* Fuction: Realize Hanoi Tower
* 1. Recursive implementation
* 2. Non-recursive implementation
* Author:igeneral
* date:2013.04.26
*
* EXPE:
* 1. Note: The status of the tower: when Status=1, you can move the disk directly to the target tower
* Instead of using disk ID to determine output
* 2.SYSTEM.OUT.PRINTLN ();
SYSTEM.OUT.PRINTLN ((int) 3.3%3);
Without (int), Output: 0.299999
Plus (int), output: 0
*/
Package part03.chapter10;

Import Java.util.Scanner;

public class _2exercise {

public static void Main (string[] args) {

Scanner Scanner = new Scanner (system.in);
System.out.println ("Please enter the number of Hanoi dishes:");
int disknum = Scanner.nextint ();
Hanoi Hanoi = new Hanoi ();
SYSTEM.OUT.PRINTLN ("Recursive implementation:");
Hanoi.play_recursive (Disknum, ' A ', ' B ', ' C ');
SYSTEM.OUT.PRINTLN ("Non-recursive implementation (imitating recursive thought)");
Hanoi.play_non_recursive (Disknum);
SYSTEM.OUT.PRINTLN ("Non-recursive implementation (according to Hanoi Law):");
Hanoi.play_regular (Disknum);

}

}

Class Hanoi {

Recursive implementation
public void play_recursive (int num, char A, Char B, char C) {
if (num = 1) {
System.out.println (A + "->" + C);
Return
} else {
Play_recursive (Num-1, A, C, B);
System.out.println (A + "->" + C);
Play_recursive (Num-1, B, A, C);
}

}

Non-recursive implementation: imitating recursive thought
public void play_non_recursive (int disknum) {
Stack stack = new stack ();
Stack.push (New Disk (Disknum, ' A ', ' B ', ' C '));
Disk popdisk = null;
while ((Popdisk = Stack.pop ())!= null) {
if (popdisk.status = = 1) {
System.out.println (popdisk.a + "->" + popdisk.c);
} else {
Reverse order Add
Add the next disk to the stack to perform the move Popdisk
Stack.push (New Disk (Popdisk.status-1, popdisk.b, POPDISK.A,
POPDISK.C));
Add a disk with a status of "1" and the same move order as Popdisk to the stack
Stack.push (New Disk (1, popdisk.a, popdisk.b, popdisk.c));
Add disk to stack in the previous step of performing a move Popdisk
Stack.push (New Disk (Popdisk.status-1, popdisk.a, POPDISK.C,
POPDISK.B));
}
}
}

Non-recursive implementation: according to Hanoi law
public void play_regular (int disknum) {

According to the law, it is necessary to adjust the position of multiple towers according to the number of Disk
When the number of towers is even, the three towers are arranged in the order of "a->b->c" into triangles.
When the number of towers is odd, the three towers are arranged in the order of "a->c->b" into triangles.
Place the Disknum disk in a tower (stack implementation) in the order of "Up and down", while placing the B and C towers empty
Stack_play_regular a = new Stack_play_regular (' a ');
Stack_play_regular B = new Stack_play_regular (' B ');
Stack_play_regular C = new Stack_play_regular (' C ');
for (int i = Disknum i > 0; i--) {
A.push (i);
}
Simulate three towers into a triangular shape arrangement
Stack_play_regular[] Towers = new stack_play_regular[3];
Towers[0] = A;
if (disknum% 2 = 0) {
TOWERS[1] = B;
TOWERS[2] = C;
} else {
TOWERS[1] = C;
TOWERS[2] = B;
}
The smallest dish is located in the tower, through which the tower in the towers
int towerofminimundisk = 0;
According to the proof: N disk movement completed at least need 2^n-1 time
The following two steps are constantly alternating
Move the smallest disk down to the next tower in order of the tower above
In addition to the other two towers in the tower where the smallest disk is located, two scenarios may occur
Situation one: There is no disk in a tower, and at this time the topmost disk of the existing disk is moved to the tower without disk.
Situation two: Two Towers have disk, at this time to compare their top tower, the smaller disk to move to the larger disk
There will be no two towers without disk, unless the move is completed or not started or only one plate is moved
int II = 0;
for (int i = 0; I < (Math.pow (2, Disknum)-1);) {//--------------Note that i++ is not done here
Remove three towers to make the code clearer
Stack_play_regular tower = Towers[towerofminimundisk];
Stack_play_regular tower_1 = towers[(int) ((towerofminimundisk + 1)% 3)];
Stack_play_regular tower_2 = towers[(int) ((Towerofminimundisk + 2)% 3)];
Move the smallest plate
System.out.println (tower.name + "->" + tower_1.name);
Tower_1.push (Tower.pop ());
i++;//--------------pay attention to i++ in this place
Towerofminimundisk = (int) ((towerofminimundisk + 1)% 3);
------------Note that the three tower are now being assigned a value
Tower = Towers[towerofminimundisk];
tower_1 = towers[(int) ((towerofminimundisk + 1)% 3)];
tower_2 = towers[(int) ((Towerofminimundisk + 2)% 3)];
Processing of the other two towers
if ((Tower_2.gettop ()!=-1 && (Tower_1.showtopdisk () > Tower_2
. Showtopdisk ()))
--------------Note to add tower_2.gettop ()!=-1
To make a judgment, otherwise the array access may be out of bounds
|| (Tower_1.gettop () = = 1 && tower_2.gettop ()!=-1)) {
System.out.println (tower_2.name + "->" + tower_1.name);
Tower_1.push (Tower_2.pop ());
i++;//--------------pay attention to i++ in this place
else if ((Tower_1.gettop ()!=-1 && tower_1.showtopdisk () < tower_2
. Showtopdisk ()))
--------------Note to add tower_1.gettop ()!=-1
To make a judgment, otherwise the array access may be out of bounds
|| (Tower_1.gettop ()!=-1 && tower_2.gettop () = = 1)) {
System.out.println (tower_1.name + "->" + tower_2.name);
Tower_2.push (Tower_1.pop ());
i++;//--------------pay attention to i++ in this place
}
II = i;
}
SYSTEM.OUT.PRINTLN (ii);
}

}

Structure for storing information
Class Disk {
Move from tower A through tower B to Tower C.
Char A;
Char B;
Char C;
The state of a tower: when Status=1, it means that the disk can be moved directly to the target tower.
int status;

overriding constructors
public Disk (int status, Char A, Char B, char C) {
This.status = status;
This. A = A;
This. b = b;
This. c = C;
}
}

Stacks for storing disk
Class Stack {
An array to store the plates
disk[] disks = new disk[10000];
Tower
private int top = 0;

View top of Stack
Public Disk Stacktop () {
return disks[top];
}

Out Stack
Public Disk pop () {
if (top!= 0) {
top--;
return disks[top + 1];
} else {
return null;
}
}

Into the stack
public void push (disk disk) {
top++;
Disks[top] = disk;
}
}

Stack class created for play_regular (int disknum)
To represent a Disk object in DiskId
Class Stack_play_regular {
Tower Name
char name;
Tower
private int top =-1;

public int GetTop () {
return top;
}

Stack is implemented through arrays, up to 64 disk
int[] stack = new int[64];

Rewrite the constructor to initialize the name of the tower
Public Stack_play_regular (char name) {
THIS.name = name;
}

View top of Stack
public int Showtopdisk () {
if (top = = 1) {
return-1;
}
return stack[top];
}

Into the stack
public void push (int diskId) {
Stack[++top] = diskId;
}

Out Stack
public int pop () {
return stack[top--];
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.