For the stack some problems are not very familiar with, so temporarily need some time to understand, need to write more code to realize,, Stack has an important application is to implement recursion in the programming language, so this time is mainly about recursive implementation, we are familiar with the factorial function, 2-order Fibonacci series and Ackerman functions, followed by some data structures, such as binary tree, generalized table, and so on, due to the inherent recursive nature of the structure, their operations can be recursive description, there is a class of problems, although the problem itself has no obvious recursive structure, But recursive solution is more simple than iterative solution, such as eight queen problem, Hanoi tower problem, etc.
Recursion is a mathematically divide-and-conquer thought, which transforms large complex problems into the same but smaller ones that are the same as the original problem, and is mathematically expressed as follows:
Let me take a eight queen question about the recursion here, in an 8x8 chess, there are 8 queens, each queen occupying a lattice, asking the Queen to not have a mutual "attack" phenomenon, that is, cannot have two queens on the same row, the same column or the same diagonal. So how do you come to realize this idea? Here is my algorithm idea: first give two, variable i,j assignment is 1, starting from line I, restore the current value of J, determine the J position. 1. Position J can be placed in Queen, Mark position (i,j), i++,j = 1, 2, Position J cannot be placed Queen, J++,i = 1; 3, when J>8, J--, continue the above cycle; 4, eighth row can be placed in the Queen.
The implementation code is as follows:
1#include <stdio.h>2 #defineN 83 4typedefstruct_tag_pos5 {6 intiOS;7 intJos;8 } Pos;9 Ten Static Charboard[n+2][n+2]; One StaticPos pos[] = {{-1, -1}, {-1,0}, {-1,1} }; A Static intCount =0; - - voidInit () the { - inti =0; - intj =0; - for(i=0; i<n+2; i++) + { -board[0][i] ='#'; +board[n+1][i] ='#'; Aboard[i][0] ='#'; atboard[i][n+1] ='#'; - } - for(i=1; i<=n; i++) - { - for(j=1; j<=n; J + +) - { inBOARD[I][J] =' '; - } to } + } - the voiddisplay () * { $ inti =0;Panax Notoginseng intj =1; - for(i=0; i<n+2; i++) the { + for(j=0; j<n+2; J + +) A { theprintf"%c", Board[i][j]); + } -printf"\ n"); $ } $ } - - intCheckintIintj) the { - intRET =1;Wuyi intp =0; the for(p=0; p<3; p++) - { Wu intNI =i; - intNJ =J; About while(Ret && (BOARD[NI][NJ]! ='#') ) $ { -Ni = ni +Pos[p].ios; -NJ = NJ +Pos[p].jos; -RET = RET && (BOARD[NI][NJ]! ='*'); A } + } the - returnret; $ } the the voidFindinti) the { the intj =0; - if(I >N) in { thecount++; theprintf"Solution:%d\n", Count); About display (); the GetChar (); the } the Else + { - for(j=1; j<=n; J + +) the {Bayi if(Check (i, j)) the { theBOARD[I][J] ='*'; -Find (i+1); -BOARD[I][J] =' '; the } the } the } the } - the intMain () the { the init ();94Find1); the the return 0; the}
Queen
Compile results such as:
When you press ENTER constantly, will appear solution until Solutioin92, and then press ENTER to exit. Such as
Summary:
Recursion is a kind of thinking that divide and conquer the problem, the first thing to solve the problem is to establish a recursive model;
If the Solution92 to the end of the time, so solve the problem of recursion first must have boundary conditions, otherwise will be dead loop;
Implementation of stack and recursion