Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std ;
- void give(int from , int to , int k) ;
- void tar(int from , int to , int k) ;
- void tarplace(int from ,int to ,int k) ;
- int count=0 ;
- int a=1,b=2 ,c=3 ;
- void give(int from , int to , int k){
- cout << "ring " << k << " : " << char('a'- 1+ from ) << " -> " << char('a'-1+to) << "\n" ;
- count++ ;
- }
- void tar(int from , int to , int k){
- int last ;
- last = 6- from - to ;
- if (k >1){
- tar(from , last , k-1) ;
- give( from , to , k) ;
- tar(last,to , k-1) ;
- }
- else give( from , to , k) ;
- }
- /*
- 1.整疊少一送到last
- 2.底盤送到to
- 3.last留兩個 (k-3層)回去from
- 4.留下的上面那一個去to
- */
- void tarplace(int from ,int to ,int k){
- int last = 6- from - to;
- if (k >2){
- tar(from , last , k-1) ;
- give(from , to , k) ;
- if (k >3 ) tar(last , from , k-3) ;
- give(last , to , k-2) ;
- tarplace(from,char('a'+4-to),k-3) ;
- }
- else if (k==2){
- give(from , last , k-1) ;
- give(from , to , k) ;
- }
- else if (k==1) give(from , to , k) ;
- }
- /*
- void tareven(int from ,int to ,int k){
- int last = 6- from - to;
- tar(from , last , k-1) ;
- cout << "ring " << k << " : " << char('a'-1+from) << " -> " << char('a'-1+to) << endl ;
- count++ ;
- tarodd(last,to , k-1) ;
- }
- void tarodd(int from,int to,int k){
- int last ;
- last = 6- from - to ;
- if (k > 1){
- tar(from , last , k-1) ;
- cout << "ring " << k << " : " << char('a'-1+from) << " -> " << char('a'-1+to) << endl ;
- count++ ;
- tareven(last,to , k-1) ;
- }
- else cout << "ring " << k << " : " << char('a'-1+from) << " -> " << char('a'-1+to) << endl ,count++ ;
- }
- */
- int main() {
- ios::sync_with_stdio(false);
- int n= 0 ;
- cin >> n ;
- tarplace(a,c,n) ;
- cout << "共移動了 " << count << " 步" << "\n" ;
- }
Advertisement
Add Comment
Please, Sign In to add comment