C++回溯法實(shí)例分析
本文實(shí)例講述了C++的回溯法,分享給大家供大家參考之用。具體方法分析如下:
一般來(lái)說(shuō),回溯法是一種枚舉狀態(tài)空間中所有可能狀態(tài)的系統(tǒng)方法,它是一個(gè)一般性的算法框架。
解向量a=(a1, a2, ..., an),其中每個(gè)元素ai取自一個(gè)有限序列集Si,這樣的解向量可以表示一個(gè)排列,其中ai是排列中的第i個(gè)元素,也可以表示子集S,其中ai為真當(dāng)且僅當(dāng)全集中的第i個(gè)元素在S中;甚至可以表示游戲的行動(dòng)序列或者圖中的路徑。
在回溯法的每一步,我們從一個(gè)給定的部分解a={a1, a2, ..., ak}開(kāi)始,嘗試在最后添加元素來(lái)擴(kuò)展這個(gè)部分解,擴(kuò)展之后,我們必須測(cè)試它是否為一個(gè)完整解,如果是的話(huà),就輸出這個(gè)解;如果不完整,我們必須檢查這個(gè)部分解是否仍有可能擴(kuò)展成完整解,如果有可能,遞歸下去;如果沒(méi)可能,從a中刪除新加入的最后一個(gè)元素,然后嘗試該位置上的其他可能性。
用一個(gè)全局變量來(lái)控制回溯是否完成,這個(gè)變量設(shè)為finished,那么回溯框架如下,可謂是回溯大法之精髓與神器
bool finished = false; void backTack(int input[], int inputSize, int index, int states[], int stateSize) { int candidates[MAXCANDIDATE]; int ncandidates; if (isSolution(input, inputSize, index) == true) { processSolution(input, inputSize, index); } else { constructCandidate(input, inputSize, index, candidates, &ncandidates); for (int i = 0; i < ncandidates; i++) { input[index] = candidates[i]; backTack(input, inputSize, index + 1); if (finished) return; } } }
不拘泥于框架的形式,我們可以編寫(xiě)出如下代碼:
#include <iostream> using namespace std; char str[] = "abc"; const int size = 3; int constructCandidate(bool *flag, int size = 2) { flag[0] = true; flag[1] = false; return 2; } void printCombine(const char *str, bool *flag, int pos, int size) { if (str == NULL || flag == NULL || size <= 0) return; if (pos == size) { cout << "{ "; for (int i = 0; i < size; i++) { if (flag[i] == true) cout << str[i] << " "; } cout << "}" << endl; } else { bool candidates[2]; int number = constructCandidate(candidates); for (int j = 0; j < number; j++) { flag[pos] = candidates[j]; printCombine(str, flag, pos + 1, size); } } } void main() { bool *flag = new bool[size]; if (flag == NULL) return; printCombine(str, flag, 0, size); delete []flag; }
采用回溯法框架來(lái)計(jì)算字典序排列:
#include <iostream> using namespace std; char str[] = "abc"; const int size = 3; void constructCandidate(char *input, int inputSize, int index, char *states, char *candidates, int *ncandidates) { if (input == NULL || inputSize <= 0 || index < 0 || candidates == NULL || ncandidates == NULL) return; bool buff[256]; for (int i = 0; i < 256; i++) buff[i] = false; int count = 0; for (int i = 0; i < index; i++) { buff[states[i]] = true; } for (int i = 0; i < inputSize; i++) { if (buff[input[i]] == false) candidates[count++] = input[i]; } *ncandidates = count; return; } bool isSolution(int index, int inputSize) { if (index == inputSize) return true; else return false; } void processSolution(char *input, int inputSize) { if (input == NULL || inputSize <= 0) return; for (int i = 0; i < inputSize; i++) cout << input[i]; cout << endl; } void backTack(char *input, int inputSize, int index, char *states, int stateSize) { if (input == NULL || inputSize <= 0 || index < 0 || states == NULL || stateSize <= 0) return; char candidates[100]; int ncandidates; if (isSolution(index, inputSize) == true) { processSolution(states, inputSize); return; } else { constructCandidate(input, inputSize, index, states, candidates, &ncandidates); for (int i = 0; i < ncandidates; i++) { states[index] = candidates[i]; backTack(input, inputSize, index + 1, states, stateSize); } } } void main() { char *candidates = new char[size]; if (candidates == NULL) return; backTack(str, size, 0, candidates, size); delete []candidates; }
對(duì)比上述兩種情形,可以發(fā)現(xiàn)唯一的區(qū)別在于全排列對(duì)當(dāng)前解向量沒(méi)有要求,而字典序?qū)Ξ?dāng)前解向量是有要求的,需要知道當(dāng)前解的狀態(tài)!
八皇后回溯法求解:
#include <iostream> using namespace std; int position[8]; void constructCandidate(int *input, int inputSize, int index, int *states, int *candidates, int *ncandidates) { if (input == NULL || inputSize <= 0 || index < 0 || candidates == NULL || ncandidates == NULL) return; *ncandidates = 0; bool flag; for (int i = 0; i < inputSize; i++) { flag = true; for (int j = 0; j < index; j++) { if (abs(index - j) == abs(i - states[j])) flag = false; if (i == states[j]) flag = false; } if (flag == true) { candidates[*ncandidates] = i; *ncandidates = *ncandidates + 1; } } /* cout << "ncandidates = " << *ncandidates << endl; system("pause");*/ return; } bool isSolution(int index, int inputSize) { if (index == inputSize) return true; else return false; } void processSolution(int &count) { count++; } void backTack(int *input, int inputSize, int index, int *states, int stateSize, int &count) { if (input == NULL || inputSize <= 0 || index < 0 || states == NULL || stateSize <= 0) return; int candidates[8]; int ncandidates; if (isSolution(index, inputSize) == true) { processSolution(count); } else { constructCandidate(input, inputSize, index, states, candidates, &ncandidates); for (int i = 0; i < ncandidates; i++) { states[index] = candidates[i]; backTack(input, inputSize, index + 1, states, stateSize, count); } } } void main() { //初始化棋局 for (int i = 0; i < 8; i++) position[i] = i; int states[8]; int count = 0; backTack(position, 8, 0, states, 8, count); cout << "count = " << count << endl; }
希望本文所述對(duì)大家C++程序算法設(shè)計(jì)的學(xué)習(xí)有所幫助。
您可能感興趣的文章
- 04-02c語(yǔ)言編寫(xiě)函數(shù)冒泡排序 c語(yǔ)言冒泡排序法函數(shù)
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
- 01-10c語(yǔ)言求1+2+...+n的解決方法
- 01-10求子數(shù)組最大和的解決方法詳解
- 01-10深入理解約瑟夫環(huán)的數(shù)學(xué)優(yōu)化方法
- 01-10c語(yǔ)言 跳臺(tái)階問(wèn)題的解決方法
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)-用棧實(shí)現(xiàn)表達(dá)式求值的方法詳解
- 01-10深入理解C++中常見(jiàn)的關(guān)鍵字含義
- 01-10用貪心法求解背包問(wèn)題的解決方法
- 01-10求斐波那契(Fibonacci)數(shù)列通項(xiàng)的七種實(shí)現(xiàn)方法


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02c語(yǔ)言函數(shù)調(diào)用后清空內(nèi)存 c語(yǔ)言調(diào)用
- 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
- 04-02c語(yǔ)言的正則匹配函數(shù) c語(yǔ)言正則表達(dá)
- 04-02c語(yǔ)言用函數(shù)寫(xiě)分段 用c語(yǔ)言表示分段
- 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)
- 04-02c語(yǔ)言編寫(xiě)函數(shù)冒泡排序 c語(yǔ)言冒泡排
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
- 04-02c語(yǔ)言分段函數(shù)怎么求 用c語(yǔ)言求分段
- 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎
- 04-02c語(yǔ)言調(diào)用函數(shù)求fibo C語(yǔ)言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改