Last active
January 1, 2016 14:59
-
-
Save KT-Yeh/8161578 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include <iostream> | |
#include <vector> | |
using namespace std; | |
void Hanoi(int,vector<int>&,vector<int>&,vector<int>&,vector<int> **); | |
void Move(vector<int>&,vector<int>&,vector<int> **); | |
int n,m; | |
int num; | |
int main() | |
{ | |
for(int i=0;;i++){ | |
scanf("%d %d",&n,&m); | |
if (n==0 && m==0) break; | |
num=0; | |
printf("Problem #%d\n\n",i+1); | |
vector<int> a,b,c; | |
vector<int> *manage[3]={&a,&b,&c}; //指向A,B,C(因為在函數裡面ABC順序會亂掉) | |
printf("A=> "); | |
for(int j=n;j>0;j--) {a.push_back(j); printf(" %d",j);} | |
printf("\nB=>\nC=>\n"); | |
Hanoi(n,a,b,c,manage); | |
printf("\n"); | |
} | |
return 0; | |
} | |
void Hanoi(int n,vector<int> &a,vector<int> &b,vector<int> &c,vector<int> *manage[]) | |
{ | |
if (n==1 && num<m) Move(a,c,manage); | |
else if(num<m){ | |
Hanoi(n-1,a,c,b,manage); | |
if (num>=m) return; | |
Move(a,c,manage); | |
if (num>=m) return; | |
Hanoi(n-1,b,a,c,manage); | |
} | |
} | |
void Move(vector<int> &a,vector<int> &c,vector<int> *manage[]) | |
{ | |
c.push_back(a.back()); | |
a.pop_back(); | |
printf("\n"); | |
printf("A=>"); if(manage[0]->size()) printf(" "); | |
for(int i=0;i<manage[0]->size();i++) printf(" %d",(*manage[0])[i]); printf("\n"); | |
printf("B=>"); if(manage[1]->size()) printf(" "); | |
for(int i=0;i<manage[1]->size();i++) printf(" %d",(*manage[1])[i]); printf("\n"); | |
printf("C=>"); if(manage[2]->size()) printf(" "); | |
for(int i=0;i<manage[2]->size();i++) printf(" %d",(*manage[2])[i]); printf("\n"); | |
num++; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment