Recursion troubles

From: BCC (bryan_at_akanta.com)
Date: 06/24/04


Date: Wed, 23 Jun 2004 23:37:09 GMT

Hello, this one is killing me...

Heres what I have (in C++ btw):

int depth = 2;
int list_size = 5;
int* array = new int[list_size];
for (int i = 0; i < list_size; ++i) array[i] = 0;
vector<int *> all_list;

Generate(all_list, list_size, depth, array);

// Process array

delete [] array;

void Generate(std::vector<int *>& res, int list_size, int depth, int*
in_list)
{
  if (depth == 0)
  {
    res.push_back(in_list);
  }
  else {
    for (int i = 0; i < list_size; ++i) {
      if (in_list[i] > 0) continue; // We have already included this seq,
skip
      ++in_list[i];
      Generate(res, list_size, depth - 1, in_list);
    }
  }
}

What I need it to have is in my resulting list is all unique possible
combinations of 1's and 2's:
for 1's
10000
01000
00100
00010
00001
-And-
for 2's
11000
10100
10010
10001
01100
01010
01001
00110
00101
00011

I believe this is all of them. And I need to be able to do this for any
size list and any depth...

I think the function is pretty close, but I am still not getting it. What
am I missing? (hopefully this makes sense!)

Thanks,
Bryan



Relevant Pages