【经典汉诺塔的路径】

游戏地址:http://www.4399.com/flash/109504_1.htm
(玩一会就回来…)

//有三个杆子,A,B,C
//我们标号1-n,为n个环标号越大的在下面;
//如果可以在任意杆子穿梭
//我们从大局想想
//现在是有n个在最左边A上,肯定是要把前n-1个放到辅助杆子B上,然后把n放到最右边C
//现在是有n-1个在中间B上,肯定是要把前n-2这一堆放到A上,然后把n-1放到最右边C上,
//然后大家肯定明白了,肯定是把n-3这一堆放到B上,然后把n-2放到最右边C上。
//最终会达到A上只有n,直接放到最右边C上。

//所以移动分为两类
//第一类,移动除了n的那些盘
//第二类,移动最底端的那个盘n
//第三类,可能不是最后一个盘,还要让B上的借助C回到A上;
//第四类,只剩最后一个,直接A->C;

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <algorithm>
using namespace std;
#define LL long long
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
#define exp 1e-6

void Move(char a,char b)
{
    printf("%c-->%c\n",a,b);
}

void solve(int n,char one,char two,char three)
{
    if(n==1){
        Move(one,three);
    }
    else{
        solve(n-1,one,three,two);//A借助C到B;
        Move(one,three);         //将A上最底个放到C上
        solve(n-1,two,one,three);//将B上借助C到A上
    }
}

int main()
{
    int n;
    cin>>n;
    solve(n,'a','b','c');
}
posted @ 2016-07-08 18:53  see_you_later  阅读(209)  评论(0编辑  收藏  举报