CS302 - Data Structures using C++ Topic: The Towers of Hanoi - - PowerPoint PPT Presentation

cs302 data structures using c
SMART_READER_LITE
LIVE PREVIEW

CS302 - Data Structures using C++ Topic: The Towers of Hanoi - - PowerPoint PPT Presentation

CS302 - Data Structures using C++ Topic: The Towers of Hanoi Kostas Alexis The Towers of Hanoi Introduction to the problem The Towers of Hanoi is a mathematical puzzle where one has three pegs and n disks and the goal is to move


slide-1
SLIDE 1

CS302 - Data Structures using C++

Kostas Alexis

Topic: The Towers of Hanoi

slide-2
SLIDE 2

The Towers of Hanoi

  • Introduction to the problem
  • The “Towers of Hanoi” is a mathematical puzzle where one has three pegs and n disks

and the goal is to move the entire stack to another rod, obeying a set of rules.

  • The Rules of the “Towers of Hanoi”
  • Only one disk may be moved at a time
  • Each move consists of taking the upper disk from one of the stacks and placing it on

top of another stack: a disk can only be moved if it is the uppermost disk on a stack.

  • No disk may be placed on top of a smaller disk.
slide-3
SLIDE 3

The Towers of Hanoi

  • Problem Statement
  • Beginning with n disks on pole A and zero disks on poles B and C, solve

towers(n,A,B,C)

slide-4
SLIDE 4

The Towers of Hanoi

  • Problem Statement
  • Beginning with n disks on pole A and zero disks on poles B and C, solve

towers(n,A,B,C)

  • Solution
  • With all disks on A, solve towers(n-1,A,C,B)
  • With the largest disk on pole A and all the others on pole C, solve towers(n-1,A,B,C)
  • With the largest disk on pole B and all the other disks on pole C, solve towers(n-

1,C,B,A)

slide-5
SLIDE 5

The Towers of Hanoi

slide-6
SLIDE 6

The Towers of Hanoi

slide-7
SLIDE 7

The Towers of Hanoi

slide-8
SLIDE 8

The Towers of Hanoi

slide-9
SLIDE 9

The Towers of Hanoi

slide-10
SLIDE 10

The Towers of Hanoi

slide-11
SLIDE 11

The Towers of Hanoi

slide-12
SLIDE 12

The Towers of Hanoi

slide-13
SLIDE 13

The Towers of Hanoi

slide-14
SLIDE 14

The Towers of Hanoi

n+1

slide-15
SLIDE 15

The Towers of Hanoi

n+1

slide-16
SLIDE 16

The Towers of Hanoi

n+1

slide-17
SLIDE 17

The Towers of Hanoi

n+1

slide-18
SLIDE 18

The Towers of Hanoi

n+1

slide-19
SLIDE 19

The Towers of Hanoi

#include <iostream> using namespace std; void solveTowers(int n, char from_rod, char to_rod, char aux_rod) { if (n == 1) { // Move from SRC to DST cout << "Move disk 1 from rod " << from_rod << " to rod " << to_rod << endl; return; } solveTowers(n-1, from_rod, aux_rod, to_rod); // Move from SRC: FROM_ROD to DST: TO_ROD with SPARE: AUX_ROD cout << "Move disk " << n << " from rod " << from_rod << " to rod " << to_rod << endl; // Move from SRC: AUX_ROD to DST: TO_ROD with SPARE: FROM_ROD solveTowers(n-1, aux_rod, to_rod, from_rod); } int main() { int n = 3; // Number of disks solveTowers(n, 'A', 'C', 'B'); // A, B and C are names of rods return 0; }

slide-20
SLIDE 20

The Towers of Hanoi

#include <iostream> using namespace std; void solveTowers(int n, char from_rod, char to_rod, char aux_rod) { if (n == 1) { // Move from SRC to DST cout << "Move disk 1 from rod " << from_rod << " to rod " << to_rod << endl; return; } solveTowers(n-1, from_rod, aux_rod, to_rod); // Move from SRC: FROM_ROD to DST: TO_ROD with SPARE: AUX_ROD cout << "Move disk " << n << " from rod " << from_rod << " to rod " << to_rod << endl; // Move from SRC: AUX_ROD to DST: TO_ROD with SPARE: FROM_ROD solveTowers(n-1, aux_rod, to_rod, from_rod); } int main() { int n = 3; // Number of disks solveTowers(n, 'A', 'C', 'B'); // A, B and C are names of rods return 0; }

slide-21
SLIDE 21

The Towers of Hanoi

Move disk 1 from rod A to rod C Move disk 2 from rod A to rod B Move disk 1 from rod C to rod B Move disk 3 from rod A to rod C Move disk 1 from rod B to rod A Move disk 2 from rod B to rod C Move disk 1 from rod A to rod C

#include <iostream> using namespace std; void solveTowers(int n, char from_rod, char to_rod, char aux_rod) { if (n == 1) { // Move from SRC to DST cout << "Move disk 1 from rod " << from_rod << " to rod " << to_rod << endl; return; } solveTowers(n-1, from_rod, aux_rod, to_rod); // Move from SRC: FROM_ROD to DST: TO_ROD with SPARE: AUX_ROD cout << "Move disk " << n << " from rod " << from_rod << " to rod " << to_rod << endl; // Move from SRC: AUX_ROD to DST: TO_ROD with SPARE: FROM_ROD solveTowers(n-1, aux_rod, to_rod, from_rod); } int main() { int n = 3; // Number of disks solveTowers(n, 'A', 'C', 'B'); // A, B and C are names of rods return 0; }

slide-22
SLIDE 22

The Towers of Hanoi

  • Order of recursive calls that results from solveTowers(3,A,B,C)
slide-23
SLIDE 23

Thank you