java优秀算法河内之塔
At the time of formation of the world, Brahma made a precious stone tower with 64 discs made of gold in it. The disks were of decreasing size and were stacked on the tower in an order (the biggest will be at the base and the following littler in size will be over the keep going etc).
Other than this tower there were two other towers. Since the creation, Brahman ministers have been endeavoring to move the circles from tower A to tower C utilizing tower B for middle stockpiling.As the disks are very heavy, they can be moved only one at a time.
Likewise, at no time can a bigger plate be on top of a littler circle. As indicated by Brahma, the world will reach an end when the ministers have finished their errand.
The most captivating thing about this riddle is the legend can be demonstrated deductively genuine. On the off chance that the ministers had the capacity to move circles at a rate of one for each second, utilizing the littlest number of moves, it would take them (2^64)-1 seconds (same no. of moves) or about 585 billion years to complete, which is around 127 times the present age of the sun.
Java Program for Tower of Hanoi Problem
Before we dissect the project, I would like to give few attributes with respect to the calculated model of the towers. I’ve picked utilizing a non-recursive model as it would be somewhat easy to clarify and get it.
We have three towers named t1, t2, and t3.They are represented utilizing Arrays such that the first element of every tower is zero. On the off chance that the tower is unfilled it will be comprising of all 0’s.
You can look at the image to grab a better perspective of the towers.
I declared a class for it that exposes methods to swap discs between the towers, print the status of each tower and to initialize the towers.
First is the initialization of towers which will be accomplished by the parameterized constructor as shown below. The starting tower consists of all the discs in order hence it is a separate case.
The next part is just swapping all the discs until we transfer all the discs from tower 1 to tower 3.
This will be done by the following if else loop.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
int x=0;
if((input%2)==1){
while(t3.discCount!=input){
t1.swap(t3);
System.out.println("nSwap between Tower 1 and 3");
x++;
if(t3.discCount!=input){
t1.swap(t2);
System.out.println("Swap between Tower 1 and 2");
x++;
}
if(t3.discCount!=input) {
t2.swap(t3);
System.out.println("Swap between Tower 2 and 3");
x++;
}
System.out.println("nStatus:");
System.out.println("Tower1:t"); t1.print();
System.out.println("Tower2:t"); t2.print();
System.out.println("Tower3:t"); t3.print();
}
}
else{
while(t3.discCount!=input){
t1.swap(t2);
System.out.println("nSwap between Tower 1 and 2");
x++;
if(t3.discCount!=input) {
t1.swap(t3);
System.out.println("Swap between Tower 1 and 3");
x++;
}
if(t3.discCount!=input) {
t2.swap(t3);
System.out.println("Swap between Tower 2 and 3");
x++;
}
System.out.println("nStatus:");
System.out.println("Tower1:t"); t1.print();
System.out.println("Tower2:t"); t2.print();
System.out.println("Tower3:t"); t3.print();
}
}
|
Actually if you have observed I’ve used a sequence here for even and odd number of discs.
Algorithm for Swapping Discs
The perfect algorithm for moving the towers is given below.
For an even number of discs
1. Make the move of disc between t1 and t2. 2. Make the move of disc between t1 and t3. 3. Make the move of disc between t2 and t3. 4. Repeat until complete.
For an odd number of discs
1. Make the move of disc between t1 and t3. 2. Make the move of disc between t1 and t2. 3. Make the move of disc between t3 and t2. 4. Repeat until complete.
The final program is given below.
Sample Output
在世界形成之时,梵天制造了一座带有64个金制圆盘的宝石塔。 磁盘的大小逐渐减小,并按顺序堆叠在塔上(最大的磁盘在底部,而随后的较小磁盘将超过继续运行的大小等)。
除了这座塔外,还有另外两座塔。 自创建以来,婆罗门牧师一直在努力利用B座进行中间存储,将圆圈从A座移动到C座。由于磁盘很重,一次只能移动一个。
同样,任何时候都不能将更大的盘子放在一个较小的圆圈上。 正如梵天所指出的那样,当部长们完成任务后,世界将终结。
关于这个谜语,最引人入胜的是传说可以被演绎为真实。 部长们有机会以最小的移动速度每秒移动一圈,这将使他们花费(2 ^ 64)-1秒(相同的移动次数)或大约一秒钟完成5850亿年的时间,大约是太阳当前年龄的127倍。
河内塔问题的Java程序
在剖析项目之前,我想对塔的计算模型给出一些属性。 我选择使用非递归模型,因为它很容易澄清和得到它。
我们有三个分别名为t1,t2和t3的塔,它们使用数组表示,每个塔的第一个元素为零。 如果塔未充满,则它将由全0组成。
您可以查看图像以更好地了解塔楼。
我为此声明了一个类,该类公开了在塔之间交换磁盘,打印每个塔的状态以及初始化塔的方法。
首先是塔的初始化,这将由参数化构造函数完成,如下所示。 起始塔按顺序包括所有圆盘,因此是单独的情况。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
Tower ( int disc , boolean initial ) {
seq = new int [ disc + 1 ] ;
if ( initial ) {
discCount = disc ;
for ( int i = 1 ; i <= discCount ; i ++ ) {
seq [ i ] = disc ;
disc -- ;
}
top = seq [ discCount ] ;
}
else {
discCount = 0 ;
top = 0 ;
}
}
|
下一部分只是交换所有光盘,直到我们将所有光盘从塔1转移到塔3。
这将通过以下if else循环来完成。
实际上,如果您已经观察到,我在这里对偶数和奇数个光盘使用了一个序列。
交换光盘的算法
下面给出了移动塔的最佳算法。
对于偶数个光盘
1.在t1和t2之间移动光盘。 2.在t1和t3之间移动光盘。 3.在t2和t3之间移动光盘。 4.重复直到完成。
对于奇数张光盘
1.在t1和t3之间移动光盘。 2.在t1和t2之间移动光盘。 3.在t3和t2之间移动光盘。 4.重复直到完成。
最终程序如下。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
import java . util . Scanner ;
class Tower {
int discCount ;
int seq [ ] ;
int top ;
Tower ( int disc , boolean initial ) {
seq = new int [ disc + 1 ] ;
if ( initial ) {
discCount = disc ;
for ( int i = 1 ; i <= discCount ; i ++ ) {
seq [ i ] = disc ;
disc -- ;
}
top = seq [ discCount ] ;
}
else {
discCount = 0 ;
top = 0 ;
}
}
void print ( ) {
for ( int i = 0 ; i <= discCount ; i ++ ) {
System . out . print ( seq [ i ] + " " ) ;
}
System . out . println ( "top=" + top ) ;
}
void swap ( Tower temp ) {
if ( this . top != 0 && temp . top != 0 ) {
if ( this . top < temp . top ) {
temp . discCount ++ ;
temp . seq [ temp . discCount ] = this . top ;
this . seq [ this . discCount ] = 0 ;
this . discCount -- ;
}
else {
this . discCount ++ ;
this . seq [ this . discCount ] = temp . top ;
temp . seq [ temp . discCount ] = 0 ;
temp . discCount -- ;
}
}
if ( temp . top == 0 && this . top != 0 ) {
temp . discCount ++ ;
temp . seq [ temp . discCount ] = this . top ;
this . seq [ this . discCount ] = 0 ;
this . discCount -- ;
}
if ( temp . top != 0 && this . top == 0 ) {
this . discCount ++ ;
this . seq [ this . discCount ] = temp . top ;
temp . seq [ temp . discCount ] = 0 ;
temp . discCount -- ;
}
if ( discCount != 0 )
top = seq [ discCount ] ;
else
top = 0 ;
if ( temp . discCount != 0 )
temp . top = temp . seq [ temp . discCount ] ;
else
temp . top = 0 ;
}
}
class Towers_brahma {
public static void main ( String args [ ] ) {
int input ;
//get the no. of discs from user
System . out . println ( "Enter the no. of discs" ) ;
Scanner in = new Scanner ( System . in ) ;
input = in . nextInt ( ) ;
in . close ( ) ;
Tower t1 = new Tower ( input , true ) ;
Tower t2 = new Tower ( input , false ) ;
Tower t3 = new Tower ( input , false ) ;
//starting swapping the discs, every iteration swaps one disc from each tower
int x = 0 ;
if ( ( input % 2 ) == 1 ) {
while ( t3 . discCount != input ) {
t1 . swap ( t3 ) ;
System . out . println ( "nSwap between Tower 1 and 3" ) ;
x ++ ;
if ( t3 . discCount != input ) {
t1 . swap ( t2 ) ;
System . out . println ( "Swap between Tower 1 and 2" ) ;
x ++ ;
}
if ( t3 . discCount != input ) {
t2 . swap ( t3 ) ;
System . out . println ( "Swap between Tower 2 and 3" ) ;
x ++ ;
}
System . out . println ( "nStatus:" ) ;
System . out . println ( "Tower1:t" ) ; t1 . print ( ) ;
System . out . println ( "Tower2:t" ) ; t2 . print ( ) ;
System . out . println ( "Tower3:t" ) ; t3 . print ( ) ;
}
}
else {
while ( t3 . discCount != input ) {
t1 . swap ( t2 ) ;
System . out . println ( "nSwap between Tower 1 and 2" ) ;
x ++ ;
if ( t3 . discCount != input ) {
t1 . swap ( t3 ) ;
System . out . println ( "Swap between Tower 1 and 3" ) ;
x ++ ;
}
if ( t3 . discCount != input ) {
t2 . swap ( t3 ) ;
System . out . println ( "Swap between Tower 2 and 3" ) ;
x ++ ;
}
System . out . println ( "nStatus:" ) ;
System . out . println ( "Tower1:t" ) ; t1 . print ( ) ;
System . out . println ( "Tower2:t" ) ; t2 . print ( ) ;
System . out . println ( "Tower3:t" ) ; t3 . print ( ) ;
}
}
System . out . println ( "nNo. of swappings=" + x ) ;
}
}
|
样本输出
翻译自: https://www.thecrazyprogrammer.com/2015/06/java-program-for-tower-of-hanoi-problem.html
java优秀算法河内之塔