最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++回溯法實(shí)例分析

 更新時(shí)間:2014年09月19日 09:31:25   投稿:shichen2014  
這篇文章主要介紹了C++回溯法,實(shí)例講述了回溯法的原理與實(shí)現(xiàn)方法,最后給出了回溯法解決八皇后的實(shí)例,需要的朋友可以參考下

本文實(shí)例講述了C++的回溯法,分享給大家供大家參考之用。具體方法分析如下:

一般來說,回溯法是一種枚舉狀態(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}開始,嘗試在最后添加元素來擴(kuò)展這個(gè)部分解,擴(kuò)展之后,我們必須測試它是否為一個(gè)完整解,如果是的話,就輸出這個(gè)解;如果不完整,我們必須檢查這個(gè)部分解是否仍有可能擴(kuò)展成完整解,如果有可能,遞歸下去;如果沒可能,從a中刪除新加入的最后一個(gè)元素,然后嘗試該位置上的其他可能性。

用一個(gè)全局變量來控制回溯是否完成,這個(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;
 }
 }
}

不拘泥于框架的形式,我們可以編寫出如下代碼:

#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;
}

采用回溯法框架來計(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)前解向量沒有要求,而字典序?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í)有所幫助。

相關(guān)文章

  • C++ 中構(gòu)造函數(shù)的實(shí)例詳解

    C++ 中構(gòu)造函數(shù)的實(shí)例詳解

    這篇文章主要介紹了C++ 中構(gòu)造函數(shù)的實(shí)例詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下
    2017-10-10
  • C語言中的getchar()使用詳解

    C語言中的getchar()使用詳解

    大家好,本篇文章主要講的是C語言中的getchar()使用詳解,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下
    2022-01-01
  • C++實(shí)現(xiàn)圖書館案例

    C++實(shí)現(xiàn)圖書館案例

    這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)圖書館案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • C語言時(shí)間處理實(shí)例分享

    C語言時(shí)間處理實(shí)例分享

    這篇文章主要介紹了C語言時(shí)間處理實(shí)例分享的相關(guān)資料,需要的朋友可以參考下
    2015-07-07
  • C++鏈表實(shí)現(xiàn)通訊錄管理系統(tǒng)

    C++鏈表實(shí)現(xiàn)通訊錄管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C++鏈表實(shí)現(xiàn)通訊錄管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • 詳談C語言指針

    詳談C語言指針

    這篇文章主要介紹了C語言的指針,介紹了其相關(guān)概念,然后分享了幾種用法,具有一定參考價(jià)值。需要的朋友可以了解下
    2021-10-10
  • C語言實(shí)現(xiàn)井字棋(三子棋)

    C語言實(shí)現(xiàn)井字棋(三子棋)

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)井字棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • C++流程控制中用于跳轉(zhuǎn)的return和goto語句學(xué)習(xí)教程

    C++流程控制中用于跳轉(zhuǎn)的return和goto語句學(xué)習(xí)教程

    這篇文章主要介紹了C++流程控制中用于跳轉(zhuǎn)的return和goto語句學(xué)習(xí)教程,是C++入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2016-01-01
  • C語言菜鳥基礎(chǔ)教程之單精度浮點(diǎn)數(shù)與雙精度浮點(diǎn)數(shù)

    C語言菜鳥基礎(chǔ)教程之單精度浮點(diǎn)數(shù)與雙精度浮點(diǎn)數(shù)

    在C語言中,單精度浮點(diǎn)數(shù)(float)和雙精度浮點(diǎn)數(shù)(double)類型都是用來儲(chǔ)存實(shí)數(shù)的,雙精度是用記憶較多,有效數(shù)字較多,數(shù)值范圍較大。
    2017-10-10
  • 利用C++簡單實(shí)現(xiàn)順序表和單鏈表的示例代碼

    利用C++簡單實(shí)現(xiàn)順序表和單鏈表的示例代碼

    這篇文章主要給大家介紹了關(guān)于利用C++簡單實(shí)現(xiàn)順序表和單鏈表的方法,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),需要的朋友可以參考借鑒,下面來跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。
    2017-08-08

最新評(píng)論

巫山县| 信阳市| 荔浦县| 涞水县| 阿克陶县| 陇西县| 蒲城县| 麻江县| 左贡县| 肇州县| 轮台县| 莱阳市| 廊坊市| 祁阳县| 于田县| 车险| 呼伦贝尔市| 满洲里市| 尼勒克县| 瓦房店市| 浦东新区| 乐业县| 灵川县| 巴青县| 锦屏县| 安远县| 济源市| 遵化市| 阿瓦提县| 大兴区| 银川市| 阳西县| 平泉县| 班戈县| 大丰市| 灵石县| 梅州市| 会宁县| 阿拉尔市| 巢湖市| 左权县|