Instantly share code, notes, and snippets.

# Abhishek-dev2/restricted_toh.c Last active Nov 17, 2016

Tower of Hanoi problem with restriction on movement
 #include int count = 0;//For counting the number of steps void toh(int from, int to, int via, int n); void toh_2(int from, int to, int via, int n); void toh(int from, int to, int via, int n) { //Base condition 1 if(n == 0) ; //Base condition 2 else if(n == 1) { if(from == 0) { printf("%d. Move disk from %d to %d\n",++count, from, 2); if(to == 1) printf("%d. Move disk from %d to %d\n",++count, 2, to); } else if(from == 2) { printf("%d. Move disk from %d to %d\n",++count, from, 1); if(to == 0) printf("%d. Move disk from %d to %d\n",++count, 1, to); } else { printf("%d. Move disk from %d to %d\n",++count, from, 0); if(to == 2) printf("%d. Move disk from %d to %d\n",++count, 0, to); } } //Recursve call here else { toh(from, to, via, n - 1); int i = -2; if(from == 0) { printf("%d. Move disk from %d to %d\n",++count, from, 2); i = 2; if(via == 1) { printf("%d. Move disk from %d to %d\n",++count, 2, via); i = via; } } else if(from == 2) { printf("%d. Move disk from %d to %d\n",++count, from, 1); i = 1; if(via == 0) { printf("%d. Move disk from %d to %d\n",++count, 1, via); i = via; } } else { printf("%d. Move disk from %d to %d\n",++count, from, 0); i = 0; if(via == 2) { printf("%d. Move disk from %d to %d\n",++count, 0, via); i = via; } } toh_2(to, from, via, n - 1); if(i == 0) { printf("%d. Move disk from %d to %d\n",++count, i, 2); if(to == 1) printf("%d. Move disk from %d to %d\n",++count, 2, to); } else if(i == 2) { printf("%d. Move disk from %d to %d\n",++count, i, 1); if(to == 0) printf("%d. Move disk from %d to %d\n",++count, 1, to); } else { printf("%d. Move disk from %d to %d\n",++count, i, 0); if(to == 2) printf("%d. Move disk from %d to %d\n",++count, 0, to); } toh(from, to, via, n - 1); } } void toh_2(int from, int to, int via, int n) { //Base condition 1 if(n == 0) ; //Base condition 2 else if(n == 1) { if(from == 0) { printf("%d. Move disk from %d to %d\n",++count, from, 2); if(to == 1) printf("%d. Move disk from %d to %d\n",++count, 2, to); } else if(from == 2) { printf("%d. Move disk from %d to %d\n",++count, from, 1); if(to == 0) printf("%d. Move disk from %d to %d\n",++count, 1, to); } else { printf("%d. Move disk from %d to %d\n",++count, from, 0); if(to == 2) printf("%d. Move disk from %d to %d\n",++count, 0, to); } } //Recursve call here else { toh(from, via, to, n - 1); if(from == 0) { printf("%d. Move disk from %d to %d\n",++count, from, 2); if(to == 1) printf("%d. Move disk from %d to %d\n",++count, 2, to); } else if(from == 2) { printf("%d. Move disk from %d to %d\n",++count, from, 1); if(to == 0) printf("%d. Move disk from %d to %d\n",++count, 1, to); } else { printf("%d. Move disk from %d to %d\n",++count, from, 0); if(to == 2) printf("%d. Move disk from %d to %d\n",++count, 0, to); } toh(via, to, from, n - 1); } } int main() { int n; //Scanning number of disks printf("Enter the number of disks : "); scanf(" %d", &n); //Calling recursive function toh(1, 2, 0, n); return 0; }