answersLogoWhite

0

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

14y ago

What else can I help you with?

Related Questions

What are the metal towers called?

Metal towers can be called structures like radio towers, transmission towers, or lattice towers, depending on their specific function and design.


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 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.


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.


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

It varies depending on the cooling needs and plant design.


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


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 are the benefits and features of hyperboloid cooling towers in industrial applications?

Hyperboloid cooling towers offer several benefits in industrial applications. They have a unique design that allows for efficient cooling of water through increased air circulation. This design also helps in reducing noise levels and minimizing water evaporation. Additionally, hyperboloid cooling towers are known for their durability and ability to withstand harsh environmental conditions. Overall, these towers are a cost-effective and reliable solution for industrial cooling needs.


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.


What are the advantages of using parabolic cooling towers in industrial cooling systems?

Parabolic cooling towers offer advantages in industrial cooling systems due to their efficient heat dissipation, compact design, and ability to reduce water usage.