answersLogoWhite

0


Best Answer

public class TowersOfHanoi{

public static void main(String []args){

new TowersOfHanoi().start();

}

public void start(){

String []tOH=showSteps(4);//if there are 4 disks

System.out.println("Towers of Hanoi step by step!");

for(int k=0;k<tOH.length;k++){

System.out.println("Step "+(k+1)+": Move a disk from "+tOH[k].charAt(0)+" to "+tOH[k].charAt(1));

}

}

public String []changeString(String []array,char a, char b){

for(int i=0;i<array.length;i++){

for(int j=0;j<array[i].length();j++){

if(array[i].charAt(j)==b){

array[i]=array[i].substring(0,j)+a+array[i].substring(j+1);

} else if(array[i].charAt(j)==a){

array[i]=array[i].substring(0,j)+b+array[i].substring(j+1);

}

}

}

return array;

}

public String []showSteps(int n){//how many n disks are there?

String []data={"A","B","C"};

String []Array=new String[(int)(Math.pow(2,n))-1];

for(int i=1;i<=Array.length;i=i*2+1){

int middle=(i-1)/2;

Array[middle]="AC";

String []tempArray=new String[middle];

for(int left=0;left<middle;left++){

tempArray[left]=Array[left];

}

tempArray=changeString(tempArray,'C','B');

for(int o=0;o<middle;o++){

Array[o]=tempArray[o];

}

tempArray=changeString(tempArray,'B','A');

tempArray=changeString(tempArray,'A','C');

for(int o=middle+1;o<i;o++){

Array[o]=tempArray[o-middle-1];

}

}

return Array;

}

}

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

The following program is written in C++ but should be easy enough to adapt to any language.

The recursive algorithm can be found in the move() function.

#include<iostream>

#include<string>

#include<sstream>

#include<vector>

// Typedef: associates a character (label) with an array

using Peg=std::pair<char,std::vector<size_t>>;

// Globals:

Peg A, B, C;

size_t moves;

void print_peg (const Peg& peg)

{

std::cout<<peg.first<<": ";

for (auto disc:peg.second)

std::cout<<disc<<' ';

std::cout<<std::endl;

}

void print_all_pegs ()

{

print_peg (A);

print_peg (B);

print_peg (C);

std::cout<<std::endl;

}

// Moves n discs from peg X to peg Y using peg Z as spare.

void move (size_t n, Peg& X, Peg& Y, Peg& Z)

{

// Stop recursions when there's less than 1 disc to move.

if (n<1) return;

// Move n-1 discs from peg X to peg Z using peg Y as spare.

move (n-1, X, Z, Y);

// Store disc n.

const size_t disc {X.second.back()};

// Move disc n from peg X to peg Y.

X.second.pop_back();

Y.second.push_back (disc);

// Print the move.

std::cout

<< "#" << ++moves << ": "

<< "move disc " << disc << ' '

<< "from " << X.first << ' '

<< "to " << Y.first << std::endl;

print_all_pegs();

// Move n-1 discs from peg Z to peg Y using peg X as spare.

move (n-1, Z, Y, X);

}

size_t enter_discs ()

{

size_t discs {};

while (true)

{

std::string input {};

std::cout<<"How many discs would you like to play with? ";

std::cin>>input;

std::stringstream ss {};

ss<<input;

if (ss>>discs && 0<discs)

break;

std::cout<<"Invalid input."<<std::endl;

}

std::cout<<std::endl;

return discs;

}

bool play_again()

{

std::string valid {"YNyn"};

char input {};

while (valid.find(input)==valid.npos)

{

std::cout<<"Play again? ";

std::cin>>input;

}

return valid.find(input)%2==0;

}

int main()

{

// Label the pegs.

A.first='A';

B.first='B';

C.first='C';

while (true)

{

// Input no. of discs to play with.

size_t discs {enter_discs()};

// Initialise globals.

moves=0;

for (size_t disc{0}; disc<discs; ++disc)

A.second.push_back (discs-disc);

// Print the initial state of the pegs.

std::cout<<"Start:"<<std::endl;

print_all_pegs();

// Move all discs from peg A to peg C using peg B as spare.

move (discs, A, C, B);

if (!play_again()) break;

C.second.clear();

}

return 0;

}

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

recursive:

solve (int n, int i, int j, int k)

{

. if (n==1) printf ("Move one from %d to %d\n", i, j);

. else {

. . solve (n-1, i, k); /* 1st step */

. . solve (1, i, j); /* 2nd step */

. . solve (n-1, k, j); /* 3rd step */

. }

}

non-recursive:

solve (int n, int i, int j, int k)

{

. push (n, i, j, k);

. while (! empty) {

. . pop (&n, &i, &j, &k);

. . if (n==1) printf ("Move one from %d to %d\n", i, j);

. . else {

. . . push (n-1, k, j); /* 3rd step */

. . . push (1, i, j); /* 2nd step */

. . . push (n-1, i, k); /* 1st step */

. . }

. }

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

I don't want to spoil your fun, let it be sufficient to say that it is not trivial, but not that hard either. If you are to lazy to do it, consult wikipedia (attached link).

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Design a non recursive algorithm for the towers of hanoi puzzle?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How would you design an algorithm for reversing two adjacent entries on a stack if you were given three stacks and you were only allowed to move entries one at a time from one stack to another?

Research Towers Of Hanoi http://en.wikipedia.org/wiki/Tower_of_Hanoi You will find your answer


Towers of hanoi?

Putting a question mark after the name of a game or puzzle does not make it a sensible question.


Who created Kuwait water towers?

Built in 1979, After the Swedish Design


What holds up power lines?

Electrical transmission towers are what holds power lines up. These towers are made of steel and have an openwork lattice design.


What are facts about the Petronas Twin Towers?

The Petronas Twin Towers has Islamic motifs in its architecture. It's meant to reflect Malaysia's national religion, Islam. The cross-section of the towers also incorporates the Rub el Hizb design.


How many cooling towers are in a pressurized nuclear water reactor?

It varies depending on the cooling needs and plant design.


What is the link between the number of discs and the minimum moves in the towers of hanoi puzzle and if i was given the number of discs could I work out the minimum moves without doing the puzzle?

If there are N discs, the minimum number of moves required is 2N - 1.


Who decided to build the twin towers?

Although the World Trade Center Complex itself was proposed in 1943, it was the chosen architect for the WTC, Minoru Yamasaki, who proposed the design involving twin towers in 1969.


What has the author Don Daso written?

Don Daso has written: 'Antenna towers for radio amateurs' -- subject(s): Antennas (Electronics), Radio and television towers, Radio, Amateurs' manuals, Design and construction


What is the history of Balsa wood towers?

the best way to build a balsa wood tower is to use as many triangle design as you can.


How long would it take to the hanoi puzzle with 6 7 8 discs?

For any n-disc version of the Tower of Hanoi, the optimum solution for the puzzle takes a minimum of 2n-1 moves. In the case of 6, 7, 8-sized Towers of Hanoi, the puzzle would take: 26-1 = 63, 27-1 = 127, 28-1 = 255 moves.


Are the twin towers the tallest towers in the world?

No, they werent. The tallest towers were the Petronas towers.