in C: Tower of Hanoi: A very popular mathematical game or puzzle is referred to
ID: 3884934 • Letter: I
Question
in C:
Tower of Hanoi: A very popular mathematical game or puzzle is referred to as the Tower of Hanoi. The idea behind the game is to find an efficient method for moving disks between three posts. Each disk has a different diameter, but all of them can be placed on the available posts. The goal of the game is to move all of the disks from one post to the another according the following rules:
1. Only one disk may be transferred at a time
2. Only the top disk on any post may be accessed at a given time
3. No disk may be placed on top of a smaller disk at any point
At the start of the game, all of the disks must originally be placed such that the largest disk is on the bottom of the stack of one post, and the smallest is on the top of the stack on the same post. The disks should form a cone shape. Write a program to simulate the Tower of Hanoi game. For each move print the post number (1 – 3) from which the disk is taken, the diameter of the disk, and the resulting post on which the disk is placed. Also, show the current diameter of the disks on each post. You must use stacks to solve this problem! Initially start with three disks in your game. Note: if you visit https://www.youtube.com/watch?v=3I3iVQUWLjo, you will find an animation of how Tower of Hanoi should run.
Explanation / Answer
program:
#include<stdio.h> //header file for standard input and output
// recursive function for solving towers of hanoi problem
void tower_of_hanoi(char from,char to,char between,int n){
//the function need not return any thing so it is void and n is the total number of rods. As there are three rods from is the initial rod where all the disks are present and to is the destination rod and between is the rod using which we will move the disks from source to destination
if(n==1) { //base condition where we have only one disk
printf(" Move disk 1 from %c to %c",from,to); //if there is only one rod we move it directly from source to destination
return; //problem solved
}
tower_of_hanoi(from,between,to,n-1); //calling recursive function.As we have to move the rod first from from rod to between rod
printf(" Move disk %d from %c to %c",n,from,to); // displaying how to move the rod and which rod to move
tower_of_hanoi(between,to,from,n-1); //again calling recursive function for inserting from between rod to destination rod
}
int main(){
int n; //for storing no of disks
scanf("%d",&n); //input from user
tower_of_hanoi('A','C','B',n); //calling funtion
return 0;
}
output:
4
Move disk 1 from A to B
Move disk 2 from A to C
Move disk 1 from B to C
Move disk 3 from A to B
Move disk 1 from C to A
Move disk 2 from C to B
Move disk 1 from A to B
Move disk 4 from A to C
Move disk 1 from B to C
Move disk 2 from B to A
Move disk 1 from C to A
Move disk 3 from B to C
Move disk 1 from A to B
Move disk 2 from A to C
Move disk 1 from B to C
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.