2016年11月10日 星期四

TIOJ 1355 - 河內之塔-蘿莉塔

http://tioj.infor.org/problems/1355

普通到不能再普通的河內塔(?



 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
#include<bits/stdc++.h>
using namespace std;

inline int rit(){
    int t=0,k=1;
    char c;
    do{
        c=getchar();
        if(c=='-')k=-1;
    }while(c<'0'||c>'9');
    do{
        t=t*10+c-'0';
        c=getchar();
    }while(c>='0'&&c<='9');
    return t*k;
}

int n;
int cnt;

void o(int a,int b){
    printf("#%d : move the dish from #%d to #%d\n",cnt++,a,b);
}

void Hn(int from,int dist,int step,int tmp){
    if(step==1){
        o(from,dist);
        return;
    }
    Hn(from,tmp,step-1,dist);
    o(from,dist);
    Hn(tmp,dist,step-1,from);
    return;
}

int main(){
    cin>>n;
    cnt=1;
    Hn(1,3,n,2);
}

沒有留言:

張貼留言