易采站长站为您分析C++回溯法,实例讲述了回溯法的原理与实现方法,最后给出了回溯法解决八皇后的实例,需要的朋友可以参考下
本文实例讲述了C++的回溯法,之用。具体方法分析如下:
一般来说,回溯法是一种枚举状态空间中所有可能状态的系统方法,它是一个一般性的算法框架。
解向量a=(a1, a2, ..., an),其中每个元素ai取自一个有限序列集Si,这样的解向量可以表示一个排列,其中ai是排列中的第i个元素,也可以表示子集S,其中ai为真当且仅当全集中的第i个元素在S中;甚至可以表示游戏的行动序列或者图中的路径。
在回溯法的每一步,我们从一个给定的部分解a={a1, a2, ..., ak}开始,尝试在最后添加元素来扩展这个部分解,扩展之后,我们必须测试它是否为一个完整解,如果是的话,就输出这个解;如果不完整,我们必须检查这个部分解是否仍有可能扩展成完整解,如果有可能,递归下去;如果没可能,从a中删除新加入的最后一个元素,然后尝试该位置上的其他可能性。
用一个全局变量来控制回溯是否完成,这个变量设为finished,那么回溯框架如下,可谓是回溯大法之精髓与神器
#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;
本文实例讲述了C++的回溯法,之用。具体方法分析如下:
一般来说,回溯法是一种枚举状态空间中所有可能状态的系统方法,它是一个一般性的算法框架。
解向量a=(a1, a2, ..., an),其中每个元素ai取自一个有限序列集Si,这样的解向量可以表示一个排列,其中ai是排列中的第i个元素,也可以表示子集S,其中ai为真当且仅当全集中的第i个元素在S中;甚至可以表示游戏的行动序列或者图中的路径。
在回溯法的每一步,我们从一个给定的部分解a={a1, a2, ..., ak}开始,尝试在最后添加元素来扩展这个部分解,扩展之后,我们必须测试它是否为一个完整解,如果是的话,就输出这个解;如果不完整,我们必须检查这个部分解是否仍有可能扩展成完整解,如果有可能,递归下去;如果没可能,从a中删除新加入的最后一个元素,然后尝试该位置上的其他可能性。
用一个全局变量来控制回溯是否完成,这个变量设为finished,那么回溯框架如下,可谓是回溯大法之精髓与神器
#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;










