Solution Review: Recursive Towers of Hanoi
Follow the step-by-step instructions to find a solution for three disks.
We'll cover the following...
Solution
See the code given below!
Press + to interact
#include <stdio.h>void move ( int, char, char, char ) ;int main( ){int n = 3 ;move ( n, 'A', 'B', 'C' ) ;return 0 ;}void move ( int n, char sp, char ap, char ep ){if ( n == 1 )printf ( "Move from %c to %c\n", sp, ep ) ;else{move ( n - 1, sp, ep, ap ) ;move ( 1, sp,' ', ep ) ;move ( n - 1, ap, sp, ep ) ;}}
Explanation
The code given above shows the prototype of the move( )
and the first call to move( )
.
n
...
Access this course and 1400+ top-rated courses and projects.