欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++回溯法實例分析

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

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

一般來說,回溯法是一種枚舉狀態(tài)空間中所有可能狀態(tài)的系統(tǒng)方法,它是一個一般性的算法框架。

解向量a=(a1, a2, ..., an),其中每個元素ai取自一個有限序列集Si,這樣的解向量可以表示一個排列,其中ai是排列中的第i個元素,也可以表示子集S,其中ai為真當(dāng)且僅當(dāng)全集中的第i個元素在S中;甚至可以表示游戲的行動序列或者圖中的路徑。

在回溯法的每一步,我們從一個給定的部分解a={a1, a2, ..., ak}開始,嘗試在最后添加元素來擴(kuò)展這個部分解,擴(kuò)展之后,我們必須測試它是否為一個完整解,如果是的話,就輸出這個解;如果不完整,我們必須檢查這個部分解是否仍有可能擴(kuò)展成完整解,如果有可能,遞歸下去;如果沒可能,從a中刪除新加入的最后一個元素,然后嘗試該位置上的其他可能性。

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

采用回溯法框架來計算字典序排列:

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

對比上述兩種情形,可以發(fā)現(xiàn)唯一的區(qū)別在于全排列對當(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;
}

希望本文所述對大家C++程序算法設(shè)計的學(xué)習(xí)有所幫助。

相關(guān)文章

  • 一張圖總結(jié)C++中關(guān)于指針的那些事

    一張圖總結(jié)C++中關(guān)于指針的那些事

    今天小編就為大家分享一篇關(guān)于一圖總結(jié)C++中關(guān)于指針的那些事,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-02-02
  • 一文帶你了解C++中queue的使用

    一文帶你了解C++中queue的使用

    C++中的queue是一種容器,用于在FIFO(先進(jìn)先出)原則下存儲和管理元素。本篇文章將深入探討C++中的queue,包括它的定義、使用、原理和示例,感興趣的可以了解一下
    2023-04-04
  • 詳解C++中的增量運(yùn)算符++和減量運(yùn)算符--的用法

    詳解C++中的增量運(yùn)算符++和減量運(yùn)算符--的用法

    這篇文章主要介紹了C++中的增量運(yùn)算符++和減量運(yùn)算符--的用法,分為前綴情況和后綴情況來講,需要的朋友可以參考下
    2016-01-01
  • 使用C++中的ADO對SQLite進(jìn)行增刪改查

    使用C++中的ADO對SQLite進(jìn)行增刪改查

    本文將介紹如何使用C++的ADO (ActiveX Data Objects)對SQLite數(shù)據(jù)庫進(jìn)行增刪改查操作,文中有詳細(xì)的代碼示例,需要的朋友可以參考下
    2023-06-06
  • C語言掃雷游戲的實現(xiàn)方法

    C語言掃雷游戲的實現(xiàn)方法

    這篇文章主要為大家詳細(xì)介紹了C語言掃雷游戲的實現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • C++實現(xiàn)歸并排序算法

    C++實現(xiàn)歸并排序算法

    這篇文章主要為大家詳細(xì)介紹了C++實現(xiàn)歸并排序算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • 判斷本機(jī)office安裝版本的方法分享

    判斷本機(jī)office安裝版本的方法分享

    這篇文章主要介紹了判斷本機(jī)office安裝版本的方法分享,需要的朋友可以參考下
    2014-01-01
  • VC程序在Win32環(huán)境下動態(tài)鏈接庫(DLL)編程原理

    VC程序在Win32環(huán)境下動態(tài)鏈接庫(DLL)編程原理

    這篇文章主要介紹了VC程序在Win32環(huán)境下動態(tài)鏈接庫(DLL)編程原理,包括了dll文件的原理與具體實現(xiàn)過程,對于深入掌握VC程序設(shè)計具有很好的參考借鑒價值,需要的朋友可以參考下
    2014-10-10
  • 簡單比較C語言中的execl()函數(shù)與execlp()函數(shù)

    簡單比較C語言中的execl()函數(shù)與execlp()函數(shù)

    這篇文章主要介紹了C語言中的execl()函數(shù)與execlp()函數(shù)的簡單比較,是C語言入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-08-08
  • 關(guān)于C++中數(shù)據(jù)16進(jìn)制輸出的方法

    關(guān)于C++中數(shù)據(jù)16進(jìn)制輸出的方法

    本文主要介紹了關(guān)于C++中數(shù)據(jù)16進(jìn)制輸出的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03

最新評論