Hanoi Tower problem Algorithm (C language Console animated demo version) __ Block chain

Source: Internet
Author: User
Tags clear screen

Recursive thinking is very simple and one of the most common algorithms, one of the representative example is the Hanoi tower problem.

The algorithm for solving problems 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);
}
}

On top of this, a simple stack is built to simulate the placement and removal of columns and discs. Use a display function to convert the stack data structure to a simple graphic, which is displayed once every time the disk is moved, creating a simple animation effect.

File: Hanoi.c//function: Hanoi tower problem algorithm Console animation demo program//run: Compile with C compiler run (already run test via MinGW version of GCC)//Author: Miao//Time: 2010-3-23 #include < Stdio.h> #define MAX 5//Tower maximum height (layer), is also the maximum number of discs and the maximum size//a stack, used to simulate the column struct stack {int space[max];//stack space, for each layer of the column, The number stored represents the size of the disk int p;//stack top pointer}stacks[3];//three column char star[max+1]; An array of stars (used to denote a disk) Char space[max+1];//an array of spaces//into the stack, representing a disk: Stacknum: Tower, Number: Disk size void push (Stacknum,number) {if ( Stacks[stacknum].p>=max) {printf (Error: Unable to stack, reached stack top. /n "); Exit (0); } Stacks[stacknum].space[stacks[stacknum].p++]=number; //out stack, indicating the removal of a disk: Sstacknum: Tower, Return:: Disk size int pop (stacknum) {if (stacks[stacknum].p<=0) {printf (Error: Unable to stack, reached bottom of stack.) /n "); Exit (0); return STACKS[STACKNUM].SPACE[--STACKS[STACKNUM].P]; }//shows the status of the current tower Void Show () {int charcount;//statistics how many characters are plotted on the current row int i,j; system ("CLS");//clear screen function//top level start drawing tower int tap= MAX; while (t ap--{for (i=0;i<3;i++) {charcount = 0;//If this layer has a pie, draw if (tap<=stacks[i].p-1) {printf ("%s", &star[max-stacks[i ].SPACE[TAP]]); CHARCOUNT+=STACKS[I].SPACE[TAP]; //Full space printf ("%s|", &space[charcount]); printf ("n"); Sleep (500); }//Move round cake: column a-> column B void moves (int a,int b) {push (B,pop (a));}//Hanoi main algorithm: N: Pie count, A~c: three column void Hanoi (int n,int a,int b,int c) {if (n>0) {Hanoi (n-1,a,c,b); move (a,b);//Mobile Pie Show ();//display of the Nottingham Tower Hanoi (n-1,c,b,a);} Initialize some variables void init () {STACKS[0].P = STACKS[1].P = STACKS[2].P =0; int i; for (i=0;i<max;i++) {star[i]= ' * ';//Create an array of stars , easy to draw space[i]= '; Create an array of spaces to facilitate drawing} space[i]=star[i]= '/0 '; int main () {init ();//Place round cake: Put in column 0th, put in order the size of the 5~1 round cake push (0,5), push (0,4), push (0,3), push (0,2), push (0,1), show (),//display into the round cake After the Hanoi Hanoi (5,0,1,2);//execute Hanoi algorithm return 0; }

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.