0% found this document useful (0 votes)
103 views6 pages

Lovely Professional University: Design Problem-1

The document describes the Tower of Hanoi puzzle, including its origins in a Vietnamese temple legend and mathematical solutions. It provides the rules of the puzzle, which involve moving disks on rods subject to rules of disk size. Algorithms described include a simple alternating smallest/non-smallest disk approach and a recursive solution that breaks the problem down into moving n-1 disks, the nth disk, and then the remaining n-1 disks. Source code in Java is also presented and outputs the disk moves for the 3 disk puzzle.

Uploaded by

sonamnegi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views6 pages

Lovely Professional University: Design Problem-1

The document describes the Tower of Hanoi puzzle, including its origins in a Vietnamese temple legend and mathematical solutions. It provides the rules of the puzzle, which involve moving disks on rods subject to rules of disk size. Algorithms described include a simple alternating smallest/non-smallest disk approach and a recursive solution that breaks the problem down into moving n-1 disks, the nth disk, and then the remaining n-1 disks. Source code in Java is also presented and outputs the disk moves for the 3 disk puzzle.

Uploaded by

sonamnegi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

LOVELY PROFESSIONAL UNIVERSITY

Design problem-1

CSE 360

SUBMITTED TO:- SUBMITTED BY:-

Mr. Buddhadeva Das SONAM NEGI

ROLL NO-08

SEC-A2701

BTECH (H)-MBA IT
INTRODUCTION:

The tower of Hanoi (commonly also known as the "towers of Hanoi"), is a puzzle invented by E. Lucas in
1883. Given a stack of disks arranged from largest on the bottom to smallest on top placed on a rod,
together with two empty rods, the towers of Hanoi puzzle asks for the minimum number of moves
required to move the stack from one rod to another, where moves are allowed only if they place smaller
disks on top of larger disks.

The objective of the puzzle is to move the entire stack to another rod, obeying the following rules:

 Only one disk may be moved at a time.


 Each move consists of taking the upper disk from one of the rods and sliding it onto another rod,
on top of the other disks that may already be present on that rod.
 No disk may be placed on top of a smaller disk.

ORIGINS:

The puzzle was invented by the French mathematician Édouard Lucas in 1883. There is a legend about a
Vietnamese temple which contains a large room with three time-worn posts in it surrounded by 64
golden disks. The priests of Hanoi, acting out the command of an ancient prophecy, have been moving
these disks, in accordance with the rules of the puzzle, since that time. The puzzle is therefore also
known as the Tower of Brahma puzzle. According to the legend, when the last move of the puzzle is
completed, the world will end. It is not clear whether Lucas invented this legend or was inspired by it.

If the legend were true, and if the priests were able to move disks at a rate of one per second, using the
smallest number of moves, it would take them 264−1 seconds or roughly 584.38 billion years;[1] it
would take 18,446,744,073,709,551,615 turns to finish.

There are many variations on this legend. For instance, in some tellings, the temple is a monastery and
the priests are monks. The temple or monastery may be said to be in different parts of the world —
including Hanoi, Vietnam, and may be associated with any religion. In some versions, other elements are
introduced, such as the fact that the tower was created at the beginning of the world, or that the priests
or monks may make only one move per day.

The Flag Tower of Hanoi may have served as the inspiration for the name.

SOLUTION:

The puzzle can be played with any number of disks, although many toy versions have around seven to
nine of them. The game seems impossible to many novices, yet is solvable with a simple algorithm.

Simple solution

The following solution is a simple solution for the toy puzzle.


Alternate moves between the smallest piece and a non-smallest piece. When moving the smallest piece,
always move it in the same direction (to the right if the starting number of pieces is even, to the left if
the starting number of pieces is odd). If there is no tower in the chosen direction, move the piece to the
opposite end, but then continue to move in the correct direction. For example, if you started with three
pieces, you would move the smallest piece to the opposite end, then continue in the left direction after
that. When the turn is to move the non-smallest piece, there is only one legal move. Doing this should
complete the puzzle using the least amount of moves to do so.[2]

Another simple solution

For an even number of disks:

 make the legal move between pegs A and B


 make the legal move between pegs A and C
 make the legal move between pegs B and C
 repeat until complete

For an odd number of disks:

 make the legal move between pegs A and C


 make the legal move between pegs A and B
 make the legal move between pegs B and C
 repeat until complete

Recursive solution

A key to solving this puzzle is to recognize that it can be solved by breaking the problem down into a
collection of smaller problems and further breaking those problems down into even smaller problems
until a solution is reached. The following procedure demonstrates this approach.

 label the pegs A, B, C—these labels may move at different steps


 let n be the total number of discs
 number the discs from 1 (smallest, topmost) to n (largest, bottommost)
 To move n discs from peg A to peg C:
 move n−1 discs from A to B. This leaves disc #n alone on peg A
 move disc #n from A to C
 move n−1 discs from B to C so they sit on disc #n

The above is a recursive algorithm: to carry out steps 1 and 3, apply the same algorithm again for n−1.
The entire procedure is a finite number of steps, since at some point the algorithm will be required for n
= 1. This step, moving a single disc from peg A to peg B, is trivial.

The Tower of Hanoi is often used as an example of a simple recursive algorithm when teaching
introductory programming. Recursive solutions typically resemble the following python code:

def Hanoi(n, A, C, B):


if n != 0:

Hanoi(n - 1, A, B, C)

print 'Move the plate from', A, 'to', C

Hanoi(n - 1, B, C, A)

SCREEN SHOT OF MY PROGRAM:


SOURCE CODE OF TOWER OF HANOI:

public class TowerOfhanoi{


public static void main(String[] args) {
hanoi(3,'A','B','C');//i have taken 3 disks
}
private static void hanoi(int n,char x,char y,char z)
{
java.util.Stack<Quad> stack=new java.util.Stack<Quad>();
stack.push(new Quad(n,x,y,z));
while(!stack.empty())
{
Quad quad=stack.pop();
n=quad.n;
x=quad.a;
y=quad.b;
z=quad.c;
if(n==1)System.out.println("Move top disk from peg " + quad.a+" to
peg "+quad.c);
else
{
stack.push(new Quad(n-1,y,x,z));
stack.push(new Quad(1,x,y,z));
stack.push(new Quad(n-1,x,z,y));
}
}
}
}
class Quad {
public int n;
public char a,b,c;
public Quad(int n,char a,char b,char c){
this.n=n;
this.a=a;
this.b=b;
this.c=c;
}
}

OUTPUT:

Move top disk from peg A to peg C


Move top disk from peg A to peg B
Move top disk from peg C to peg B
Move top disk from peg A to peg C
Move top disk from peg B to peg A
Move top disk from peg B to peg C
Move top disk from peg A to peg C

You might also like