產生所有錯位排列的演算法

來源:互聯網
上載者:User

標籤:include   ace   vector   圖片   img   style   cup   color   define   

所謂N元錯位排列,就是指對應於1,2,--,N的N元排列Im(m=1,2,---,N),滿足Im!=m,演算法的目的是構造出所有這樣的錯位排列,依據的基本思想是回溯法,在沿棧向下試探的過程中逐步擴大部分錯位排列的規模,當發現無法找到下一個部分錯位排列的元素時就向上回溯,繼續試探,當棧空間首元素stack[0]==N+1時,表明首元素為2,--,N的所有錯位排列已全部試探得到,錯位排列產生完畢,退出迴圈。這裡給出演算法,該演算法產生對應於1,2,---,N的所有N元錯位排列

代碼(C++)

 

  1 #include "stdafx.h"  2 #include <vector>  3 #include <iostream>  4 using namespace std;  5   6 #define N 5   //問題規模,產生對應於1,2,---,N的所有N元錯位排列  7   8 int Search(const vector<bool> &occupy, int i ,int j)  9 { 10     for (; i < occupy.size(); ++i) 11     { 12         if (i + 1 != j && occupy[i] == false) 13         { 14             return i + 1; 15         } 16     } 17     return 0; 18 } 19  20 int main() 21 { 22     vector<int> a(N, 0); 23     vector<bool> occupy(N, false);  //對於棧中已產生的錯位排列中的任意值i,occupy[i-1]==true,對於1,2---,N中不在錯位排列棧中的任意元素i,occupy[i-1]=false 24     bool TF = true;  //迴圈過程中回溯至本層為false,前進至本層為true 25     int i = 0;  //存放錯位排列的棧的棧頂指標 26     int count = 0;  //統計錯位排列數 27     while (true) 28     { 29         if (i == 0) 30         { 31             if (TF == true) 32             { 33                 a[i] = 2; 34                 occupy[a[i] - 1] = true; 35                 ++i; 36             } 37             else 38             { 39                 occupy[a[i] - 1] = false; 40                 ++a[i]; 41                 if (a[i] > N) 42                     break; 43                 occupy[a[i] - 1] = true; 44                 ++i; 45                 TF = true; 46             } 47         } 48         else 49         { 50             int temp; 51             if (TF == true) 52             { 53                 if ((temp = Search(occupy, 0, i + 1)) == 0) 54                 { 55                     --i; 56                     TF = false; 57                     continue; 58                 } 59                 else 60                 { 61                     a[i] = temp; 62                     occupy[temp-1] = true; 63                 } 64             } 65             else 66             { 67                 if ((temp = Search(occupy, a[i], i + 1)) == 0) 68                 { 69                     occupy[a[i] - 1] = false; 70                     --i; 71                     continue; 72                 } 73                 else 74                 { 75                     occupy[a[i] - 1] = false; 76                     a[i] = temp; 77                     occupy[temp - 1] = true; 78                 } 79             } 80  81             if (i != a.size() - 1) 82             { 83                 ++i; 84                 TF = true; 85             } 86             else 87             { 88                 ++count; 89                 cout << "第"<<count<<"個錯位排列:" << endl; 90                 for (const int &m : a) 91                 { 92                     cout << m << " "; 93                 } 94                 cout << endl; 95                 for (int j = 1; j <= N; ++j) 96                 { 97                     cout << j << " "; 98                 } 99                 cout << endl;100                 cout << endl;101                 TF = false;102             }103 104         }105     }106     return 0;107 }

 

運行結果(N=5時)

產生所有錯位排列的演算法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.