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?

Continue Learning about Engineering

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


The difference between Induced and forced cooling towers?

Induced cooling towers utilize fans to draw ambient air through the fill and across the water, enhancing evaporation and cooling efficiency. In contrast, forced cooling towers use fans to push air into the tower, also promoting cooling but with a different airflow dynamic. While induced towers typically rely on natural draft principles, forced towers actively control airflow direction and volume. This distinction affects their design, efficiency, and operational characteristics in various applications.


How were the twin towers designed?

The Twin Towers of the World Trade Center were designed by architect Minoru Yamasaki, with structural engineering by Leslie E. Robertson Associates. They featured a unique &quot;tube&quot; structure, which utilized a reinforced concrete core and a lightweight, flexible outer shell of steel columns, allowing for large open floor spaces. The design emphasized both aesthetics and functionality, incorporating a minimalist approach with a focus on height and the iconic silhouette of the towers. Additionally, the towers incorporated advanced safety features for their time, including fireproofing and emergency evacuation systems.


What were some of the challenges involved in building the petronas towers?

glass and concrete


How does a lift bridge work?

There are 2 towers. The segment in the middle is on a rail on the towers. Ropes goes from the ends of the middle segment up to a pulleys on the towers and from there to motors at the ends of the bridge. When the motors run the pull the middle segment up.

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 answer to Puzzle 7 in Professor Layton and the Curious Village 7?

In Puzzle 7 of Professor Layton and the Curious Village, the solution involves determining the correct arrangement of the three towers (A, B, and C) based on the clues provided. The answer is that the height of Tower C is 2, Tower A is 3, and Tower B is 1. This satisfies all the conditions laid out in the puzzle.


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.