本文目錄一覽:
用java實現漢諾塔的程序是啥呀?
其實不知道你到底是想要代碼還是要什麼
給你帖的示範代碼吧:
漢諾塔問題的遞歸Java語言實現
public
class
Hanoi
{/**
*
*
@param
n
*
盤子的數目
*
@param
origin
*
源座
*
@param
assist
*
輔助座
*
@param
destination
*
目的座
*/
public
void
hanoi(int
n,
char
origin,
char
assist,
char
destination)
{
if
(n
==
1)
{
move(origin,
destination);
}
else
{
hanoi(n
–
1,
origin,
destination,
assist);
move(origin,
destination);
hanoi(n
–
1,
assist,
origin,
destination);
}
}
//
the
route
of
the
movement
private
void
move(char
origin,
char
destination)
{
System.out.println(“Direction:”
+
origin
+
“—“
+
destination);
}
public
static
void
main(String[]
args)
{
Hanoi
hanoi
=
new
Hanoi();
hanoi.hanoi(3,
‘A’,
‘B’,
‘C’);
}
}
java中漢諾塔的演算法問題
class HanRuoTa {
static long s=0;
public static void main(String args[]) {
int n =3;
System.out.println(“漢諾塔層數為” + n);
System.out.println(“移動方案為:” );
hanoi(n, ‘a’, ‘b’, ‘c’);
System.out.println(“需要移動次數:”+s);
}
static void hanoi(int n, char a, char b, char c) {
if (n 0) {
hanoi(n – 1, a, c, b);
move(a, b);
hanoi(n – 1, c, b, a);
s++;
}
}
static void move(char x, char y) {
System.out.println(x + “-” + y + “\t”);
}
}
運行結果:
漢諾塔層數為3
移動方案為:
a-b
a-c
b-c
a-b
c-a
c-b
a-b
需要移動次數:7
用java編寫hanoi塔的非遞歸演算法。
這是個好問題,很少看到有人寫漢諾塔的非遞歸…其實只要先寫出遞歸,然後把遞歸的每一步要做的事情記錄在一個棧裡面就可以了
public class Test {
private static void emitStep(int source, int dest) {
System.out.println(source + ” – ” + dest);
}
static class Step {
Step(int n, int s, int d, int t) {
this.n = n;
source = s;
dest = d;
temp = t;
}
int n, source, dest, temp;
}
private static void hanoi(int n, int source, int dest, int temp) {
java.util.StackStep steps = new java.util.StackStep();
steps.add(new Step(n, source, dest, temp));
while (steps.empty() == false) {
Step step = steps.pop();
if (step.n == 1) {
emitStep(step.source, step.dest);
continue;
}
steps.push(new Step(step.n – 1, step.temp, step.dest, step.source));
steps.push(new Step(1, step.source, step.dest, 0));
steps.push(new Step(step.n – 1, step.source, step.temp, step.dest));
}
}
public static void main(String[] args) {
hanoi(3, 1, 3, 2);
}
}
漢諾塔問題?
漢諾塔(又稱河內塔)問題是印度的一個古老的傳說。開天闢地的神勃拉瑪在一個廟裡留下了三根金剛石的棒,第一根上面套著64個圓的金片,最大的一個在底下,其餘一個比一個小,依次疊上去,廟裡的眾僧不倦地把它們一個個地從這根棒搬到另一根棒上,規定可利用中間的一根棒作為幫助,但每次只能搬一個,而且大的不能放在小的上面。解答結果請自己運行計算,程序見尾部。面對龐大的數字(移動圓片的次數)18446744073709551615,看來,眾僧們耗盡畢生精力也不可能完成金片的移動。
後來,這個傳說就演變為漢諾塔遊戲:
1.有三根杆子A,B,C。A桿上有若干碟子
2.每次移動一塊碟子,小的只能疊在大的上面
3.把所有碟子從A桿全部移到C桿上
經過研究發現,漢諾塔的破解很簡單,就是按照移動規則向一個方向移動金片:
如3階漢諾塔的移動:A→C,A→B,C→B,A→C,B→A,B→C,A→C
此外,漢諾塔問題也是程序設計中的經典遞歸問題。
演算法思路:
1.如果只有一個金片,則把該金片從源移動到目標棒,結束。
2.如果有n個金片,則把前n-1個金片移動到輔助的棒,然後把自己移動到目標棒,最後再把前n-1個移動到目標棒
(非專業人士可以忽略以下內容)
補充:漢諾塔的演算法實現(c++)
#include
#include
using namespace std;
ofstream fout(“out.txt”);
void Move(int n,char x,char y)
{
fout”把”n”號從”x”挪動到”yendl;
}
void Hannoi(int n,char a,char b,char c)
{
if(n==1)
Move(1,a,c);
else
{
Hannoi(n-1,a,c,b);
Move(n,a,c);
Hannoi(n-1,b,a,c);
}
}
int main()
{
fout”以下是7層漢諾塔的解法:”endl;
Hannoi(7,’a’,’b’,’c’);
fout.close();
cout”輸出完畢!”endl;
return 0;
}
C語言精簡演算法
/* Copyrighter by SS7E */
#include /* Copyrighter by SS7E */
void hanoi(int n,char A,char B,char C) /* Copyrighter by SS7E */
{
if(n==1)
{
printf(“Move disk %d from %c to %c\n”,n,A,C);
}
else
{
hanoi(n-1,A,C,B); /* Copyrighter by SS7E */
printf(“Move disk %d from %c to %c\n”,n,A,C);
hanoi(n-1,B,A,C); /* Copyrighter by SS7E */
}
}
main() /* Copyrighter by SS7E */
{
int n;
printf(“請輸入數字n以解決n階漢諾塔問題:\n”);
scanf(“%d”,n);
hanoi(n,’A’,’B’,’C’);
}/* Copyrighter by SS7E */
PHP演算法:
?php
function hanoi($n,$x,$y,$z){
if($n==1){
move($x,1,$z);
}else{
hanoi($n-1,$x,$z,$y);
move($x,$n,$z);
hanoi($n-1,$y,$x,$z);
}
}
function move($x,$n,$z){
echo ‘move disk ‘.$n.’ from ‘.$x.’ to ‘.$z.’
‘;
}
hanoi(10,’x’,’y’,’z’);
?
JAVA演算法:
public class Haniojava
{
public static void main(String args[])
{
byte n=2;
char a=’A’,b=’B’,c=’C’;
hanio(n,a,b,c);
}
public static void hanio(byte n,char a,char b,char c)
{
if(n==1)
System.out.println(“move “+a+” to “+b);
else
{
hanio((byte)(n-1),a,c,b);
System.out.println(“move “+a+” to “+b);
hanio((byte)(n-1),c,b,a);
}
}
}
#include
void move(char ch1, char ch2) {
coutch1″ch2′ ‘;
}
void hanoi(int n, char a, char b, char c) {
if (n==1)
move (a,c);
else {
hanoi (n-1,a,c,b);
move (a,c);
hanoi (n-1,b,a,c);
}
}
void main() {
int m;
cout”Enter the number of disk to move:\n”;
cinm;
cout”The step to moving “m” disk:\n”;
hanoi (m,’A’,’B’,’C’);
cinm;
}
用不了這麼複雜
,設A上有n個盤子。
如果n=1,則將圓盤從A直接移動到C。
如果n=2,則:
1.將A上的n-1(等於1)個圓盤移到B上;
2.再將A上的一個圓盤移到C上;
3.最後將B上的n-1(等於1)個圓盤移到C上。
如果n=3,則:
A. 將A上的n-1(等於2,令其為n`)個圓盤移到B(藉助於C),步驟如下:
(1)將A上的n`-1(等於1)個圓盤移到C上。
(2)將A上的一個圓盤移到B。
(3)將C上的n`-1(等於1)個圓盤移到B。
B. 將A上的一個圓盤移到C。
C. 將B上的n-1(等於2,令其為n`)個圓盤移到C(藉助A),步驟如下:
(1)將B上的n`-1(等於1)個圓盤移到A。
(2)將B上的一個盤子移到C。
(3)將A上的n`-1(等於1)個圓盤移到C。
到此,完成了三個圓盤的移動過程。
從上面分析可以看出,當n大於等於2時,移動的過程可分解為三個步驟:
第一步 把A上的n-1個圓盤移到B上;
第二步 把A上的一個圓盤移到C上;
第三步 把B上的n-1個圓盤移到C上;其中第一步和第三步是類同的。
當n=3時,第一步和第三步又分解為類同的三步,即把n`-1個圓盤從一個針移到另一個針上,這裡的n`=n-1。 顯然這是一個遞歸過程,據此演算法可編程如下:
move(int n,int x,int y,int z)
{
if(n==1)
printf(“%c–%c\n”,x,z);
else
{
move(n-1,x,z,y);
printf(“%c–%c\n”,x,z);
move(n-1,y,x,z);
}
}
main()
{
int h;
printf(“\ninput number:\n”);
scanf(“%d”,h);
printf(“the step to moving %2d diskes:\n”,h);
move(h,’a’,’b’,’c’);
}
原創文章,作者:XBVF,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/133593.html