Algorithm Learning (2) A non-recursive algorithm for the full arrangement problem -- simulating a stack

Source: Internet
Author: User

Some time ago I summarized several recursive solutions to the full arrangement problem. Today I will summarize how to implement the full arrangement problem through non-recursive simulation of system stack behavior.

We use an array stack [] to represent a stack, a top pointer to represent the stack top, and a flags [] array to indicate the availability of each number; use I to indicate the current status.

The initial status is Top = 0; I =-1; all flags arrays are 1;

I increments. If I is not out of bounds and flags [I] = 1, I will be written to the stack and moved to the top of the stack; finally, the value of flags [I] is 0, and I is traced back to the initial state-1;

When the top of the stack is out of bounds, the information of the entire stack is printed, and the top pointer is rolled back to 1. I returns to the top of the stack, and flags [I] is marked as 1;

When I crosses the border, it means that the current top state of the stack has been exhausted. This element should pop up the stack, that is, top --; then I will return to the top state of the stack, flags [I] is marked as 1;

Finally, when all the elements in the stack Are popped up and the stack top and the stack bottom are met, all the statuses are exhausted.

The language description is a bit abstract. You can draw a picture on the paper. The whole process is the behavior process of the system stack. The main point is that the changes at the top of each stack are accompanied by changes in I, and I must be traced back to the previous state.

Finally, I put the C ++ code and can refer to it for further understanding. My ability to express myself is indeed a little anxious...

 1 #include <iostream> 2  3 using namespace std; 4  5 int stack[100]; 6 int flags[50]; 7  8 void printStack(int* stack,int n){ 9     for (int i=0; i<n; i++) {10         cout<<stack[i]+1;11     }12     cout<<endl;13 }14 15 void perm(int n){16     int top=0;17     int i=-1;18     while (true) {19         i++;20         if (i==n) {21             if (top==0) {22                 break;23             }else{24                 top--;25                 i=stack[top];26                 flags[i]=1;27             }28         }else if(flags[i]==1){29             flags[i]=0;30             stack[top]=i;31             top++;32             i=-1;33         }34         if(top==n){35             top--;36             i=stack[top];37             flags[i]=1;38             printStack(stack, n);39         }40     }41 }42 43 int main(int argc, const char * argv[])44 {45     int n=4;46     for(int i=0;i<50;i++){47         flags[i]=1;48     }49     perm(n);50     return 0;51 }

 

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.