To clarify the problem, assume we are trying to arrange 7 tiles
that already have been chosen.
ABCDEFG generates 7! = 5,040 unique permutations
ABCDEF? generates 115,920 unique permutations and not
6!*26=18,720
ABCDE?? <-- This is the stumper!
The keyword is "unique" permutations.
For example, permuting ABC? where ? represents a blank tile A..Z
results in 624 arrangements but only 588 are unique permutations.
Duplicate arrangements like AABC AABC AACB AACB ABAC ABAC ABBC ABBC
... must be culled to get the unique set.
Algorithm-Loops
has a neat permute function which I used to check racks with one
blank tile.
It generates the unique permutations that I am looking for.
Here is an example for a simple rack AB?:
use strict;
use Algorithm::Loops qw( NextPermute );
my @list= sort ('A'..'B'); # Find unique permutations for AB?
my $cnt;
my @list1;
# $l represents one blank tile cycling thru all letter values
for my $l ('A'..'Z') {
@list1 = sort(@list,$l); # Very important to sort
print"@list1\n"; # Show what's happening
do {
printf"%5d. ", ++$cnt;
print"@list1\n"; # Display permutations
} while( NextPermute( @list1 ) );
}
print"Counted $cnt unique permutations";
print $/;
prints:
A A B
1. A A B
2. A B A
3. B A A
A B B
4. A B B
5. B A B
6. B B A
A B C
7. A B C
8. A C B
9. B A C
10. B C A
11. C A B
12. C B A
A B D
13. A B D
14. A D B
15. B A D
16. B D A
17. D A B
18. D B A
...
...
...
Counted 150 unique permutations
Any suggestions on how to code this for 2 blank tiles without
getting "Out of memory" failure? |