Today, a problem suddenly comes to mind, and sometimes there are multiple responses to the same event, especially in game AI, where the most appropriate scenario needs to be taken, which is best suited to the outcome or impact of each scenario, so that you can choose the most appropriate one. The most basic is a permutation combination method, the various scenarios are combined out. So wrote a basic n number permutation combination applet!
Development tools: Visual Studio 2012
ctestpermutation::~ctestpermutation () {cout<<">>>>>>>>>>>>>>>>>>>>>>>> Ctestpermutation::~ctestpermutation ()"<<Endl;}voidctestpermutation::D otest () {cout<<"-------The number of n is arranged in combination-------"<<endl<<Endl; Vector<int>vecnums,vecpermutated; cout<<"Sample Data:"; for(inti =1; I <5; i++) {vecnums.push_back (i); cout<<i<<" "; } cout<<Endl; cout<<"permutation combination Result:"<<Endl; Permutation (vecpermutated,vecnums);}/****************************************************** @ functionnmae:permutation @ Function: Arrange the n number in combination @ vecpermutated: already arranged series @ Vecwaitpermuta: number to be arranged ***************************************** ***************/voidCtestpermutation::P ermutation (vector<int> vecpermutated,vector<int>Vecwaitpermuta) { if(Vecwaitpermuta.size () >0 ) { for(vector<int>::iterator itnum = Vecwaitpermuta.begin (); Itnum! = Vecwaitpermuta.end (); + +itnum) {Vector<int> vecpermutatedtmp =vecpermutated; Vecpermutatedtmp.push_back (*itnum); Vector<int> vecwaitpermutatmp =Vecwaitpermuta; //Delete elements that are currently sortedvector<int>::iterator Retfind = Find (Vecwaitpermutatmp.begin (), Vecwaitpermutatmp.end (), *itnum); if(Vecwaitpermutatmp.end ()! =retfind) {vecwaitpermutatmp.erase (retfind); } //continuation of recursive call permutation algorithmpermutation (vecpermutatedtmp,vecwaitpermutatmp); } } Else //a set of arrangements is complete { //Print Arrangement Resultscout<<"\ t"; for(inti =0; I < vecpermutated.size (); + +i) {cout<<vecpermutated.at (i) <<" "; } cout<<Endl; }}
Execution Result:
C + + Write a permutation combination applet