creating all permutations of a grid?

I have a list of 2d vectors of characters, and I need to create and save all possible combinations of characters to this list.
I already have some characters filled in and characters that need to be filled are identified by '.'.
What is the best way to implement this?

My current idea is to iterate through the list, then through each position in the grid, identify the '.', and for each letter in the alphabet, create a copy of the grid and push it back (either directly or store them and push back later.)
The problem with this is it only fills one blank at a time, so if there are two blanks, I end up with:

a b
a .

a b
b .
etc

a b
. a

a b
. b
etc

I think a better solution may be recursive, but I don't know how to do this.
EDIT: Solved recursively like so:
void fillRemaining(list<vector<vector<char> > >& solutions,
const vector<vector<char> >& grid) {
vector<vector<char> >temp;
string alp = string("abcdefghijklmnopqrstuvwxyz");
int row,col;
if (!blankspace(grid,row,col)) {
solutions.push_back(grid);
} else {

for (unsigned int i = 0; i<alp.size();i++) {
temp = grid;
temp[row][col] = alp[i];
fillRemaining(solutions,temp);
}
}
}


Last edited on
The problem with this is it only fills one blank at a time ... so if there are two blanks
so in your proposed algorithm if there were more than one blank on what basis would the subsequent blank(s) be filled after filling the first blank? would it a with a char not in the grid && not already used to fill the preceding blank(s) etc or some other rule?
be careful with terms here. The perms and the combinations are not equal.
also, both comb and perm explode into a LOT of data, so you need to be ready to handle that or you might be sitting for a while waiting on the solution.

iteration works, whether recursive or not.
The bigger one, perms, is easier because you don't have to track and drop duplicates, you just do every possibility in turn via raw iteration. I assume you want to ignore dupes and meant perms.

recursion, you would call yourself until you hit the deepest, last, whatever word . and iterate it to all letters, then pop out, iterate the current . character once and call the deepest again, all the way back out, until all the . chars have hit z, something like that.

the subsequent blanks should also be filled with all permutations from a-z
so for two blanks, there are 26^2 solutions
the algorithm should always output 26^(num blanks) solutions
yea that is what I got also, assuming you didn't want caps and lowers in there also, which doubles the base again. I agree, its why I said it would get big fast...

busting out the old hp-11c..
perm of 26, 10 is 1.9 e^13
and
comb of 26,10 is ~ 5.5M
huge difference...!
Last edited on
Topic archived. No new replies allowed.