Home >Computer Tutorials >Computer Knowledge >How to write a C language program to solve the Tower of Hanoi problem

How to write a C language program to solve the Tower of Hanoi problem

PHPz
PHPzforward
2024-01-05 20:18:24960browse

How to write a C language program for the Tower of Hanoi problem

In fact, there are three main steps:

First, move the n-1 disks on a to b through c.

Second, move the bottom disk on a to c.

Third, since the first n-1 plates are all on pillar b, we only need to treat pillar b as pillar a and repeat the above steps.

#include

Let's learn how to solve the puzzle. During the puzzle we can use a function to move the plate. The prototype of the function is "void move(int n, char a, char b, char c)". Inside the function, we can use recursion to move the plate. By specifying the number of plates n and the identifiers of the three pillars a, b, c, we can move the plates from pillar a to pillar c in sequence. Thoughts on recursion

{

if(n==1)

printf("\t%c->%c\n", a, c); // When there is only one plate, move the plate directly from a to c

else

{

In order to help players who have not passed the level yet, let us learn about the specific puzzle solving methods. In the process of solving puzzles, we can use recursive methods. The specific operations are as follows: 1. Move the n-1th plate from pillar a to pillar c: move(n-1, a, c, b); 2. Move the nth plate from pillar a to pillar b; 3. Add the n

In order to help players who have not solved the puzzle yet, we can use the following code to solve the problem: printf("\t%c->%c\n",a,c);

In order to help players who have not passed this level yet, let’s take a look at the specific steps to solve the puzzle. First, we can move the first n-1 plates from column B to column A, so that column B becomes the starting column, and then move the plates on column B to column C through column A.

}

}

int main()

{

int n;

printf ("Please enter the number of blocks to be moved:");

scanf("%d",&n);

move(n,'a','b','c');

return 0;

}

Compile a Tower of Hanoi game using C language

I just made it yesterday.

#include void hanoi(int n,char one,char two,char three);

void main() { int m; printf("Please enter the number of floors of the tower:"); scanf("%d",&m); printf("Please follow the steps below:\n"); hanoi(m,'1','2','3'); }

void hanoi(int n,char one,char two,char three) { void move(char x,char y); if(n==1) move(one,three); else { hanoi(n-1 ,one,three,two); move(one,three); hanoi(n-1,two,one,three); } }

I will share with you a simple movement function that can help you perform movement operations in the game. The function is as follows: void move(char x, char y) { printf("%c--->%c\n", x, y); } This function accepts two parameters, representing the current position and the target position. It will print out the moving process so you can track the moving path. You can call this function to perform movement operations as needed. I hope this function can help you pass the level!

How to program Luohan Tower in C language

You can read the comments here:

My program is below:

Below is a function to help you solve the puzzle, it can be used to move the top sub-block from one tower to another. ```C void NuoYiWei(int FromTa, int ToTa) { //Write your code here to move the top sub-block from FromTa to ToTa. } ``` You can operate on the basis of this function and write your puzzle-solving method according to specific game rules and puzzle requirements. Remember to follow the actual

{

TopPoint[FromTa] is an array recording the height of each tower level. When an element is removed from a tower, the tower's height is reduced by one unit. The purpose of this array is to help us keep track of the current height of each tower.

To solve this problem, we can use the following code to record the data of three towers and move the data from one tower to another: ```python DuiZhan[ToTa][TopPoint[ToTa]] = DuiZhan[FromTa][TopPoint[FromTa]] ``` This line of code will take the data from the top of the moved tower (FromTa) and transfer it to the top of the moved tower (ToTa). This way we can record and move data on the tower.

DuiZhan[FromTa][TopPoint[FromTa]] = 0; //Restore the top level of the originally moved tower to the null value. Now let us take a look at the specific puzzle solving methods to help those players who have not yet passed the level.

In order to help those players who have not passed the level yet, let us learn about the specific puzzle solving methods. One of the key steps is to move the tower to the target location. The specific operation is to increase the number of floors of the target tower by 1 through the code "TopPoint[ToTa];". This step is an important part of the puzzle-solving process, and I hope it will help everyone pass the level smoothly.

}

void Nuo(int FromTa, int MidTa, int ToTa, int NeedMove) function is the main solution to the Tower of Hanoi problem. We can use this function to solve the Tower of Hanoi problem given the initial conditions and the height of the tower.

{

Let's take a look at the specific puzzle solving methods. When the height of the tower that needs to be moved is greater than or equal to 2 floors, we can take the following operations:

{

Nuo(FromTa, ToTa, MidTa, (NeedMove-1)); //First move all the top N-1 except the bottom one to the middle tower. This step is achieved by recursively calling the function. During the puzzle, we need to use three towers (FromTa, ToTa and MidTa) to move the plates. First, we move the top N-1 plates from FromTa to MidTa (implemented by calling the function recursively). Then, move the bottom plate from FromTa to ToTa. Finally, move the N-1 plates on MidTa to ToTa (also through recursive transfer

NuoYiWei(FromTa,ToTa); //Move the bottom plate from the starting tower to the target tower.

Nuo(MidTa,FromTa,ToTa,(NeedMove-1)); //Finally, move all the N-1 items moved to the middle tower to the target tower (assuming this function can achieve this function)

}

else

{

In order to help players who have not passed the level yet, let us learn about the specific puzzle solving methods. If there's only one block left that needs to be moved, just move it.

}

}

This function is written in C. Please note that if you use C language, you need to pay attention to some details.

I have a Tower of Hanoi program written in C language. If you need it, you can send me an email to sxt9840210@163.com and explain clearly what kind of program you need.

How to write a C language program to solve the Tower of Hanoi problem

Tower of Hanoi c language algorithm

I previously collected someone else’s answer, please take a look:

The core idea of ​​the recursive algorithm is to use the solution goal as the starting point and gradually call its own solution process to solve the unknown items until the boundary of the recursion is reached, which is the initial condition. This method is different from the traditional starting from initial conditions, but it is one of the effective ways to solve the problem. Through recursive algorithms, we can gradually decompose complex problems into simple sub-problems and finally obtain the global answer. The key to recursion is to clarify the recursion boundary and ensure that the recursive process can be done correctly

The key to solving the Tower of Hanoi problem lies in analyzing movement rules, finding patterns and determining boundary conditions.

If you want to move n plates from A to C, you can follow the following steps: (1) Move n-1 plates from A to B; (2) Move the nth plate from A to C; (3) Move n-1 plates from B to C. By calling the function recursively, other plates on A can continue to move according to the above three steps until the boundary condition n=1 is reached. In this way, the task can be successfully completed! I hope this puzzle method can be helpful to players who have not passed the level yet.

When your thinking is clear, the program becomes easier to understand. In the program, the key is to analyze the parameters each time the movement function is called and the relationship between towers A, B, and C. Let's illustrate with a practical example below.

In order to help players who have not solved the puzzle yet, let’s take a closer look at how to solve the puzzle. First, we can use the "move(int n, int x, int y, int z)" function to perform moving operations.

②{

③ if (n==1)

In order to help players who have not passed the level yet, let us learn about the specific puzzle solving methods. I have prepared the following steps for you: 1. First, find a function named printf. 2. Then, enter "%c-->%c\n" within the brackets of the printf function, which will print two characters and wrap the line. 3. Inside the brackets, pass x and z as arguments to the printf function.

⑤ else

⑥ {

⑦ move(n-1,x,z,y);

In order to help players pass the level, let’s take a look at the specific puzzle-solving methods. In the code, there is a line of code that is "printf("%c-->%c\n",x,z);". The function of this line of code is to print out the relationship between character x and character z. By observing the printed results, we can get some clues to help us solve the puzzle.

⑨ {getchar();}//Is this sentence necessary? I feel like it can be removed

⑩ move(n-1,y,x,z);

}

}

For example, there are 4 plates, and now they are all placed on tower A. The radius of the plates is larger according to the numbers 1, 2, 3, and 4. Now move the 4 plates to C and arrange them in the original order. First, we consider how to move No. 4 to C? We need to use B as the intermediary, and first move the above three to B. The operation of this step is that ① in the program begins to call the move function (the first call is recorded as one). Of course, n=4 now, and then it is judged that ③n!=1, so ④ is not executed but ⑤ is called again ( Marked as two) Consider how to move 3 disks to B. This is a recursive call, so we go back to ① again and start calling the move function, but the corresponding parameters have changed, because this time we need to consider not moving 4 disks from A to C, but how to move from A. Move 3 disks to B. Because n=3, it cannot be moved directly and requires the help of C as an intermediary. First consider the method of moving two to C, so go to ⑤ and call the move function recursively (marked as three) again. In the same way, two disks still cannot be moved directly from A to C, so the process of moving 1 to B must be considered using B as an intermediary. This time it uses B as the intermediary and moves to C as the purpose. Next, call the move function recursively (marked as four) again, which means moving to B, which can be done directly. The program executes sentences ③ and ④. The program jumps out of the innermost call (that is, jumps out of the fourth call) and returns to the previous one (the third time), and continues downward from the third call to the move function, that is, ⑧, that is, 2 Moved to C, then continued down to

⑩, and then move the one that has been moved to B back to C, thus returning to the second recursion (the time when 3 disks were moved to B using C as an intermediary). Execute ⑧, move the third disk from A to B, and then enter ⑩. In this call, because the two disks on C are moved to B with A as the intermediary, another recursive call is required, corresponding to The parameter transfer must be analyzed clearly, who is the original tower, who is the target tower, and who is the intermediary tower. The process is similar to the above analysis and will not be repeated here.

The above is the detailed content of How to write a C language program to solve the Tower of Hanoi problem. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:docexcel.net. If there is any infringement, please contact admin@php.cn delete