The Tower of Hanoi is a puzzle game with three rods and n disks, each a different size.
All the disks start off on the first rod in a stack. They are ordered by size, with the largest disk on the bottom and the smallest one at the top.
The goal of this puzzle is to move all the disks from the first rod to the last rod while following these rules:
1.You can only move one disk at a time.
2.A move consists of taking the uppermost disk from one of the stacks and placing it on top of another stack.
3.You cannot place a larger disk on top of a smaller disk.
Write a function that prints out all the steps necessary to complete the Tower of Hanoi. You should assume that the rods are numbered, with the first rod being 1, the second (auxiliary) rod being 2, and the last (goal) rod being 3.
For example, with n = 3, we can do this in 7 moves:
Move 1 to 3
Move 1 to 2
Move 3 to 2
Move 1 to 3
Move 2 to 1
Move 2 to 3
Move 1 to 3
Example:
Input: n=3
Output:
Move from 1 to 2 Move from 1 to 3 Move from 2 to 3 Move from 1 to 2 Move from 3 to 1 Move from 3 to 2 Move from 1 to 2
Approach:
C++
#include <bits/stdc++.h>using namespace std;//function to find the moves in tower of hanoi problemvoid move(int n, char source, char destination, char inter){//base case if only one leftif (n == 1)cout << "Move from " << source << " to " <<destination << "\n";//else call for n-1else{move(n - 1, source, inter, destination);move(1, source, destination, inter);move(n - 1, inter, destination, source);}}int main(){int n = 3;//call for n,source,destination,intermediatemove(n, '1', '2', '3');return 0;}
No comments:
Post a Comment