C++ C+中的主脑循环递归算法+;

C++ C+中的主脑循环递归算法+;,c++,eclipse,algorithm,for-loop,recursion,C++,Eclipse,Algorithm,For Loop,Recursion,我想为智囊团算法()创建一个数字池。该池应由大小为n的策划人代码的所有可能组合组成。因此,例如,当n=4时,池应如下所示: [0000][0001][0002]。。。。[5555] 当n=5时 [00000]。。。。[55555] 其中,每个数字代表一种来自智囊团解决方案的颜色。例如[3101]是红、蓝、白、蓝 我创建了一个函数来创建一个n=4的池: vector<string> createPool4() { vector<string> pool;

我想为智囊团算法()创建一个数字池。该池应由大小为n的策划人代码的所有可能组合组成。因此,例如,当n=4时,池应如下所示:

[0000][0001][0002]。。。。[5555]

当n=5时

[00000]。。。。[55555]

其中,每个数字代表一种来自智囊团解决方案的颜色。例如[3101]是红、蓝、白、蓝

我创建了一个函数来创建一个n=4的池:

vector<string> createPool4()
{
    vector<string> pool;

    for (int i = 0; i < colours; i++)
        for (int j = 0; j < colours; j++)
            for (int k = 0; k < colours; k++)
                for (int l = 0; l < colours; l++)
                    pool.push_back(to_string(i) + to_string(j) + to_string(k) + to_string(l));

    return pool;
}
vector createPool4()
{
向量池;
对于(int i=0;i
我当时尝试的是将此函数转换为某种递归嵌套for循环,但是,请自行查找:

vector<string> fillPool(int n, vector<string> pool, const string& s) {

    if (n == 0) {
        pool.push_back(s);
        s.clear();
        return pool;

    }

    for (int i = 0; i < n; i++) {
        s += to_string(i);
        pool = fillPool(n-1,pool,s);
    }
}

向量填充池(int n,向量池,常量字符串&s){
如果(n==0){
池。推回(s);
s、 清除();
回流池;
}
对于(int i=0;i
这段代码不起作用,它应该只显示我的方向

最后,我需要一个函数,它可以取n维,然后创建一个可能的代码列表。到目前为止,我一直在使用字符串向量,但我很高兴听到其他可能性

也许有人能帮我,因为我知道在外面的某个地方,有一个很好的解决办法


谢谢

这可以通过组合和排列来解决

其想法是:

  • 为给定的
    n
    numberofcolors
    创建所有可能的组合
  • 为每个组合创建所有不同的排列,将其附加到组合中,然后删除任何重复项
  • 这对于如何进行组合有一些很好的答案。将此和来自组合和置换的Rosetta代码的此分别放在一起,可以通过以下方式解决问题:

    #include <algorithm>
    #include <iostream>
    #include <memory>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    void permutation(string str, vector<string> *result) {
      auto strCopy = str;
      std::sort(str.begin(), str.end());
      do {
        result->push_back(str);
      } while (std::next_permutation(str.begin(), str.end()));
    
      // To avoid duplication 
      result->erase(std::remove(result->begin(), result->end(), strCopy), result->end());
    }
    
    void combination(int n, int numberOfColours, vector<string> *result) {
      vector<string> colours;
      for (int i = 0; i < numberOfColours; i++)
        colours.push_back(to_string(i));
      auto s = colours.size() - 1;
      vector<int> v(n + 1, 0);
      while (true) {
        for (int i = 0; i < n; ++i) {
          if (v[i] > s) {
            v[i + 1] += 1;
            for (int k = i; k >= 0; --k) {
              v[k] = v[i + 1];
            }
          }
        }
    
        if (v[n] > 0)
          break;
        string str{};
        for (size_t i = 0; i < n; ++i)
          str.append(colours[v[i]]);
        result->push_back(str);
        v[0] += 1;
      }
    }
    
    void createPoolN(int n, int numberOfColours, vector<string> *pool) {
      combination(n, numberOfColours, pool);
      auto perms = make_unique<vector<string>>();
      for (const auto &p : *pool) {
        permutation(p, perms.get());
      }
      pool->insert(pool->end(), perms->begin(), perms->end());
      sort(pool->begin(), pool->end());
    }
    
    int main() {
      int n = 5;
      int numberOfColours = 10;
      auto pool = make_unique<vector<string>>();
      createPoolN(n, numberOfColours, pool.get());
      for (const auto &p : *pool)
        cout << "[" << p << "]\n";
      return 0;
    }
    

    您使用池作为输入并用结果覆盖它。。。如果这不是以一种很酷的方式进行优化,那么您将拥有大量的池副本。也许最好有一个对池的引用来修改。我不认为把
    s
    作为常量引用有什么意义。您正在修改字符串(通过添加另一个字符),使其不是常量引用。我认为你应该按价值来看待它。
    [00000]
    [00001]
    [00002]
    ...
    [99997]
    [99998]
    [99999]