1. 程式人生 > >漢諾塔-遞歸實現

漢諾塔-遞歸實現

ret SM 遞歸實現 ng-click AI CP stat trac pos

#include<stdio.h>
void move(char x,char y)
{
	printf("%c->%c\n",x,y);
}
//將n個盤子從1中借助2移動到3
void hanoi(int n,char one,char two,char three)
{
	if(n==1)
		move(one,three);
	else
	{
		hanoi(n-1,one,two,three);
		move(one,three);
		hanoi(n-1,two,one,three);

	}
}
int main()
{
	int m;//盤子個數
	scanf("%d",&m);
	hanoi(m,‘A‘,‘B‘,‘C‘);
	return 0;
}

漢諾塔-遞歸實現