Linglong tower problem algorithm (C language console animation demo version)

Source: Internet
Author: User

Recursive thinking is very simple and the most commonAlgorithmOne of the examples is the qingta issue.

The problem-solving algorithm is simple:

Void Hanoi (int n, int A, int B, int C)
{
If (n> 0)
{
Hanoi (n-1, A, C, B );
Move (A, B );

Show ();

Hanoi (n-1, C, B, );
}
}

A simple stack is created to simulate the insertion and removal of the column and disc. Then, a display function is used to convert the stack data structure into a simple image, which is displayed every time the disc is moved, forming a simple animation effect.

// File: Hanoi. c <br/> // function: Console animation demonstration of the qingta problem algorithm <SPAN class = 'wp _ keywordlink '> Program </span> <br/> // run: compile and run the program using the C compiler (the test has been run through the GCC compilation of the mingw version) <br/>/author: Miao <br/> // time: 2010-3-23 <br/> # include <stdio. h> <br/> # define max 5 // The maximum height (number of layers) of the tower, which is also the maximum number and size of the disc <br/> // a stack, used to simulate the tower column <br/> struct stack <br/> {<br/> int space [Max]; // stack space, used for each layer of the tower column, the number indicates the disk size. <br/> int P; // stack top pointer <br/>} stacks [3]; // three columns </P> <p> char star [Max + 1]; // array for storing stars (used to represent disks)) <Br/> char space [Max + 1]; // array for storing spaces <br/> // stack entry, indicating to put a disc: stacknum: Tower number, Number: disk size <br/> void push (stacknum, number) <br/> {<br/> If (stacks [stacknum]. p> = max) <br/>{< br/> printf ("error: unable to import to stack, reached the top of stack. /N "); <br/> exit (0); <br/>}< br/> stacks [stacknum]. space [stacks [stacknum]. P ++] = number; <br/>}< br/> // The output stack, indicating to retrieve a disc: sstacknum: Tower number, return :: disk size <br/> int POP (stacknum) <br/>{< br/> If (stacks [stacknum]. P <= 0) <br/>{< br/> printf ("error: Unable to exit the stack, reached the bottom of the stack. /N "); <br/> exit (0); <br/>}< br/> return stacks [stacknum]. space [-- stacks [stacknum]. p]; <br/>}< br/> // display the status of the current tower <br/> void show () <br/>{< br/> int charcount; // count the number of characters drawn in the current row <br/> int I, j; <br/> system ("CLS "); // screen clearing function <br/> // The top layer starts to draw a tower chart layer by layer <br/> int tap = max; <br/> while (tap --) <br/> {<br/> for (I = 0; I <3; I ++) <br/> {<br/> charcount = 0; <br/> // if this layer has a circle, draw <br/> If (TAP <= stacks [I]. p-1) <br/> {<br/> printf ("% S", & star [Max-stacks [I]. space [tap]); <br/> charcount + = stacks [I]. space [tap]; <br/>}< br/> // fill in spaces <br/> printf ("% S |", & space [charcount]); <br/>}< br/> printf ("/N"); <br/>}< br/> sleep (500 ); <br/>}< br/> // move the pie: Column A-> Column B <br/> void move (int A, int B) <br/>{< br/> push (B, pop (a); <br/>}< br/> // Tower Master algorithm: N: Number of circles, A ~ C: three columns <br/> void Hanoi (int n, int A, int B, int c) <br/>{< br/> If (n> 0) <br/>{< br/> Hanoi (n-1, A, C, B); <br/> move (A, B ); // move the pie chart <br/> show (); // display the pie chart <br/> Hanoi (n-1, C, B, ); <br/>}< br/> // initialize some variables <br/> void Init () <br/> {<br/> stacks [0]. P = stacks [1]. P = stacks [2]. P = 0; <br/> int I; <br/> for (I = 0; I <Max; I ++) <br/>{< br/> Star [I] = '*'; // create a star array for easy plotting <br/> space [I] = ''; // create a space array for drawing <br/>}< B R/> space [I] = star [I] = '/0'; <br/>}< br/> int main () <br/>{< br/> Init (); <br/> // place the pie: place the pie on the column no., and the size is 5 ~ 1. <br/> push ); <br/> push (); <br/> show (); // display the pie chart tower after the pie chart <br/> Hanoi ); // execute the tower of shards algorithm <br/> return 0; <br/>}

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.