I think I'm missing something. The algorithm shuffles to prevent words in adjacent squares from being the same? Why not just draw from the set of 3-permutations and if satisfies that adjacency constraint, remove from the pool of available permutations (conditional draw without replacement). A solution would only have to be found once anyway, right?