拼字游戏瓷砖检查

时间:2011-01-31 18:17:07

标签: string algorithm data-structures

对于拼字游戏中的拼贴检查,您可以制作四个5x5的字母总计100个拼贴。我想制作一个所有40个水平和垂直单词都有效的单词。可用图块集包含:

  • 12 x E
  • 9 x A,I
  • 8 x O
  • 6×N,R,T
  • 4 x D,L,S,U
  • 3 x G
  • 2 x B,C,F,H,M,P,V,W,Y,空白区块(通配符)
  • 1 x K,J,Q,X,Z

有效单词词典可用here(700KB)。大约有12,000个有效的5个字母单词。

以下是所有20个水平字有效的示例:

Z O W I E|P I N O T
Y O G I N|O C t A D   <= blank being used as 't'
X E B E C|N A L E D
W A I T E|M E R L E
V I N E R|L U T E A
---------+---------
U S N E A|K N O S P
T A V E R|J O L E D
S O F T A|I A M B I
R I D G Y|H A I T h   <= blank being used as 'h'
Q U R S H|G R O U F

我想创建一个所有垂直的也是有效的。你能帮帮我解决这个问题吗?这不是功课。这是朋友向我求助的问题。

5 个答案:

答案 0 :(得分:35)

最终编辑:已解决!以下是解决方案。

GNAWN|jOULE
RACHE|EUROS
IDIOT|STEAN
PINOT|TRAvE
TRIPY|SOLES
-----+-----
HOWFF|ZEBRA
AGILE|EQUID
CIVIL|BUXOM
EVENT|RIOJA
KEDGY|ADMAN

这是用我的拼字游戏设置的照片。 http://twitpic.com/3wn7iu

一旦我采用了正确的方法,这个很容易找到,所以我打赌你可以通过这种方式找到更多。请参阅下面的方法论。


从每个行和列的5个字母单词的字典构造前缀树。递归地,如果给定的图块位置为其列和行形成有效前缀,并且图块是可用的,并且下一个图块放置是有效的,则它是有效的。基本情况是,如果没有要放置的瓷砖,它是有效的。

找到所有有效的5x5电路板可能是有意义的,就像Glenn说的那样,看看它们中是否有任何四个可以合并。递归到100的深度听起来并不好玩。

编辑:这是我的代码的第2版。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

typedef union node node;
union node {
    node* child[26];
    char string[6];
};

typedef struct snap snap;
struct snap {
    node* rows[5];
    node* cols[5];
    char tiles[27];
    snap* next;
};

node* root;
node* vtrie[5];
node* htrie[5];
snap* head;

char bag[27] = {9,2,2,4,12,2,3,2,9,1,1,4,2,6,8,2,1,6,4,6,4,2,2,1,2,1,2};
const char full_bag[27] = {9,2,2,4,12,2,3,2,9,1,1,4,2,6,8,2,1,6,4,6,4,2,2,1,2,1,2};
const char order[26] = {16,23,9,25,21,22,5,10,1,6,7,12,15,2,24,3,20,13,19,11,8,17,14,0,18,4};

void insert(char* string){
    node* place = root;
    int i;
    for(i=0;i<5;i++){
        if(place->child[string[i] - 'A'] == NULL){
            int j;
            place->child[string[i] - 'A'] = malloc(sizeof(node));
            for(j=0;j<26;j++){
                place->child[string[i] - 'A']->child[j] = NULL;
            }
        }
        place = place->child[string[i] - 'A'];
    }
    memcpy(place->string, string, 6);
}

void check_four(){
    snap *a, *b, *c, *d;
    char two_total[27];
    char three_total[27];
    int i;
    bool match;
    a = head;
    for(b = a->next; b != NULL; b = b->next){
        for(i=0;i<27; i++)
            two_total[i] = a->tiles[i] + b->tiles[i];
        for(c = b->next; c != NULL; c = c->next){
            for(i=0;i<27; i++)
                three_total[i] = two_total[i] + c->tiles[i];
            for(d = c->next; d != NULL; d = d->next){
                match = true;
                for(i=0; i<27; i++){
                    if(three_total[i] + d->tiles[i] != full_bag[i]){
                        match = false;
                        break;
                    }
                }
                if(match){
                    printf("\nBoard Found!\n\n");
                    for(i=0;i<5;i++){
                        printf("%s\n", a->rows[i]->string);
                    }
                    printf("\n");
                    for(i=0;i<5;i++){
                        printf("%s\n", b->rows[i]->string);
                    }
                    printf("\n");
                    for(i=0;i<5;i++){
                        printf("%s\n", c->rows[i]->string);
                    }
                    printf("\n");
                    for(i=0;i<5;i++){
                        printf("%s\n", d->rows[i]->string);
                    }
                    exit(0);
                }
            }
        }
    }
}

void snapshot(){
    snap* shot = malloc(sizeof(snap));
    int i;
    for(i=0;i<5;i++){
        printf("%s\n", htrie[i]->string);
        shot->rows[i] = htrie[i];
        shot->cols[i] = vtrie[i];
    }
    printf("\n");
    for(i=0;i<27;i++){
        shot->tiles[i] = full_bag[i] - bag[i];
    }
    bool transpose = false;
    snap* place = head;
    while(place != NULL && !transpose){
        transpose = true;
        for(i=0;i<5;i++){
            if(shot->rows[i] != place->cols[i]){
                transpose = false;
                break;
            }
        }
        place = place->next;
    }
    if(transpose){
        free(shot);
    }
    else {
        shot->next = head;
        head = shot;
        check_four();

    }
}

void pick(x, y){
    if(y==5){
        snapshot();
        return;
    }
    int i, tile,nextx, nexty, nextz;
    node* oldv = vtrie[x];
    node* oldh = htrie[y];
    if(x+1==5){
        nexty = y+1;
        nextx = 0;
    } else {
        nextx = x+1;
        nexty = y;
    }
    for(i=0;i<26;i++){
        if(vtrie[x]->child[order[i]]!=NULL &&
           htrie[y]->child[order[i]]!=NULL &&
           (tile = bag[i] ? i : bag[26] ? 26 : -1) + 1) {
                vtrie[x] = vtrie[x]->child[order[i]];
                htrie[y] = htrie[y]->child[order[i]];
                bag[tile]--;

                pick(nextx, nexty);

                vtrie[x] = oldv;
                htrie[y] = oldh;
                bag[tile]++;
           }
    }
}

int main(int argc, char** argv){
    root = malloc(sizeof(node));
    FILE* wordlist = fopen("sowpods5letters.txt", "r");
    head = NULL;
    int i;
    for(i=0;i<26;i++){
        root->child[i] = NULL;
    }
    for(i=0;i<5;i++){
        vtrie[i] = root;
        htrie[i] = root;
    }

    char* string = malloc(sizeof(char)*6);
    while(fscanf(wordlist, "%s", string) != EOF){
        insert(string);
    }
    free(string);
    fclose(wordlist);
    pick(0,0);

    return 0;
}

这首先尝试不常见的字母,我不再确定这是个好主意。它开始陷入困境,然后从x开始走出电路板。在看到有多少5x5块后,我改变了代码,只列出了所有有效的5x5块。我现在有一个150 MB的文本文件,包含所有4,430,974个5x5解决方案。

我还尝试了通过完整的100个瓷砖递归,并且仍在运行。

编辑2:这是我生成的所有有效5x5块的列表。 http://web.cs.sunyit.edu/~levyt/solutions.rar

编辑3:嗯,我的磁贴使用情况跟踪中似乎有一个错误,因为我刚在输出文件中找到了一个使用5个Z的块。

COSTE
ORCIN
SCUZZ
TIZZY
ENZYM

编辑4:这是最终产品。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

typedef union node node;
union node {
    node* child[26];
    char string[6];
};

node* root;
node* vtrie[5];
node* htrie[5];
int score;
int max_score;

char block_1[27] = {4,2,0,2, 2,0,0,0,2,1,0,0,2,1,2,0,1,2,0,0,2,0,0,1,0,1,0};//ZEBRA EQUID BUXOM RIOJA ADMAN
char block_2[27] = {1,0,1,1, 4,2,2,1,3,0,1,2,0,1,1,0,0,0,0,1,0,2,1,0,1,0,0};//HOWFF AGILE CIVIL EVENT KEDGY
char block_3[27] = {2,0,1,1, 1,0,1,1,4,0,0,0,0,3,2,2,0,2,0,3,0,0,1,0,1,0,0};//GNAWN RACHE IDIOT PINOT TRIPY
                                                                            //JOULE EUROS STEAN TRAVE SOLES
char bag[27] =     {9,2,2,4,12,2,3,2,9,1,1,4,2,6,8,2,1,6,4,6,4,2,2,1,2,1,2};
const char full_bag[27] = {9,2,2,4,12,2,3,2,9,1,1,4,2,6,8,2,1,6,4,6,4,2,2,1,2,1,2};
const char order[26] = {16,23,9,25,21,22,5,10,1,6,7,12,15,2,24,3,20,13,19,11,8,17,14,0,18,4};
const int value[27] = {244,862,678,564,226,1309,844,765,363,4656,909,414,691,463,333,687,11998,329,218,423,536,1944,1244,4673,639,3363,0};

void insert(char* string){
    node* place = root;
    int i;
    for(i=0;i<5;i++){
        if(place->child[string[i] - 'A'] == NULL){
            int j;
            place->child[string[i] - 'A'] = malloc(sizeof(node));
            for(j=0;j<26;j++){
                place->child[string[i] - 'A']->child[j] = NULL;
            }
        }
        place = place->child[string[i] - 'A'];
    }
    memcpy(place->string, string, 6);
}

void snapshot(){
    static int count = 0;
    int i;
    for(i=0;i<5;i++){
        printf("%s\n", htrie[i]->string);
    }
    for(i=0;i<27;i++){
            printf("%c%d ", 'A'+i, bag[i]);
    }
    printf("\n");
    if(++count>=1000){
        exit(0);
    }
}


void pick(x, y){
    if(y==5){
        if(score>max_score){
            snapshot();
            max_score = score;
        }
        return;
    }
    int i, tile,nextx, nexty;
    node* oldv = vtrie[x];
    node* oldh = htrie[y];
    if(x+1==5){
        nextx = 0;
        nexty = y+1;
    } else {
        nextx = x+1;
        nexty = y;
    }
    for(i=0;i<26;i++){
        if(vtrie[x]->child[order[i]]!=NULL &&
           htrie[y]->child[order[i]]!=NULL &&
           (tile = bag[order[i]] ? order[i] : bag[26] ? 26 : -1) + 1) {
                vtrie[x] = vtrie[x]->child[order[i]];
                htrie[y] = htrie[y]->child[order[i]];
                bag[tile]--;
                score+=value[tile];

                pick(nextx, nexty);

                vtrie[x] = oldv;
                htrie[y] = oldh;
                bag[tile]++;
                score-=value[tile];
           }
    }
}

int main(int argc, char** argv){
    root = malloc(sizeof(node));
    FILE* wordlist = fopen("sowpods5letters.txt", "r");
    score = 0;
    max_score = 0;
    int i;
    for(i=0;i<26;i++){
        root->child[i] = NULL;
    }
    for(i=0;i<5;i++){
        vtrie[i] = root;
        htrie[i] = root;
    }
    for(i=0;i<27;i++){
        bag[i] = bag[i] - block_1[i];
        bag[i] = bag[i] - block_2[i];
        bag[i] = bag[i] - block_3[i];

        printf("%c%d ", 'A'+i, bag[i]);
    }

    char* string = malloc(sizeof(char)*6);
    while(fscanf(wordlist, "%s", string) != EOF){
        insert(string);
    }
    free(string);
    fclose(wordlist);
    pick(0,0);

    return 0;
}

在找出有多少块(近20亿并且还在计数)之后,我转而试图找到某些类型的块,特别是难以使用不常见的字母构造块。我的希望是,如果我最后用一组良性的字母进入最后一个区块,那么有效区块的大量空间可能会有一组用于那组字母。

我为每个图块分配了一个与其出现的5个字母单词的数量成反比的值。然后,当我找到一个有效的块时,我会总结出图块值,如果得分是我见过的最好的,我打印出来的。

对于第一个块,我删除了空白区块,确定最后一个区块最需要这种灵活性。让它运行直到我没有看到更好的块出现一段时间后,我选择了最好的块,并从包中取出了它的瓷砖,然后再次运行程序,获得第二个块。我重复了第3次这个。然后对于最后一个块,我将空白添加回来并使用它找到的第一个有效块。

答案 1 :(得分:2)

我会采取一种悲观的看法来解决这个问题(天真地,确定)。我试图证明没有5x5解决方案,因此肯定不是四个 5x5解决方案。为了证明没有5x5解决方案,我试图从所有可能性构建一个。如果我的猜想失败并且我能够构建一个5x5解决方案,那么,我有办法构建5x5解决方案,我会尝试构建所有(独立)5x5解决方案。如果至少有4个,那么我将确定某些组合是否满足字母数限制。

[编辑] Null Set已确定存在“4,430,974 5x5解决方案”。这些有效吗? 我的意思是我们可以使用的字母数量有限制。此限制可以表示为对应于A,B,C等限制的边界向量BV = [9,2,2,4,...](您可以在Null Set的代码中看到此向量)。如果其字母计数向量的每个项小于BV中的相应项,则5x5解是有效的。很容易检查5x5解决方案是否在创建时有效。也许可以减少4,430,974的数字,比如N.

无论如何,我们可以将问题说明为:找到N中的四个字母计数向量,其总和等于BV。有(N,4)个可能的总和(“N选择4”)。 N等于400万,这仍然是10 ^ 25的数量级 - 不是一个令人鼓舞的数字。也许你可以搜索四个第一项总和为9的四个,如果是,那么检查它们的第二个项总和为2,等等。

我注意到从N中选择4后,计算是独立的,所以如果你有一台多核机器,你可以通过并行解决方案加快速度。

[Edit2]并行化可能不会产生太大的影响。在这一点上,我可能会持乐观态度:肯定有比我预期更多的5x5解决方案,因此可能会有比预期更多的最终解决方案。也许你可能不需要深入到10 ^ 25来击中一个。

答案 2 :(得分:2)

以下是我试试这个的方法。首先构造一个前缀树。

选择一个单词并将其水平放置在顶部。选择一个单词并垂直放置。交替使用它们直到用尽选项。通过交替,你开始修复第一个字母并消除许多不匹配的单词。如果你真的找到这样的方块,那么检查它们是否可以用这些方块制作。

对于5x5正方形:经过一些思考后,对于随机文本单词,它不能比O(12000!/ 11990!)差。但是考虑一下它。每当你修改一个字母(正常文本)时,你就会消除你的单词的大约90%(一个乐观的猜测)。这意味着经过三次迭代,你有12个单词。所以实际速度是

O(n * n/10 * n/10 * n/100 * n/100 * n/1000 * n/1000 ...
which for 12000 elements acts something like n^4 algorithm

这不是那么糟糕。

可能有人可以对问题进行更好的分析。但是搜索单词仍然应该很快收敛。

通过滥用不常见的字母可以做更多的消除。基本上找到所有不常见字母的单词。尝试为每个字母创建匹配的位置。为每个职位构建一组有效的字母。

例如,假设我们有四个字母Q在其中。

 AQFED, ZQABE, EDQDE, ELQUO

 this means there are two valid positionings of those:

 xZxxx
 AQFED
 xAxxx   ---> this limits our search for words that contain [ABDEFZ] as the second letter
 xBxxx
 xExxx

 same for the other

 EDQDE   ---> this limits our search for words that contain [EDLU] as the third letter
 ELQUO

 all appropriate words are in union of those two conditions

所以基本上,如果我们在位置N的单词S中有多个包含不常字母X的单词,则意味着该矩阵中的其他单词必须具有位于位置n的S中的字母。

公式:

  • 在位置1找到包含不常用字母X的所有单词(下一次迭代2,3 ......)
  • 用这些词中的字母组成一组
  • 只保留字典中包含来自集合A的字母位置1
  • 的字样
  • 尝试将它们放入矩阵中(使用第一种方法)
  • 重复第2位

答案 3 :(得分:0)

我从更简单的事情开始。

目前为止有一些结果:

   3736 2x2 solutions
8812672 3x3 solutions

The 1000th 4x4 solution is
   A A H S
   A C A I
   L A I R
   S I R E

The 1000th 5x5 solution is
   A A H E D
   A B U N A
   H U R S T
   E N S U E
   D A T E D

The 1000th 2x4x4 solution is
   A A H S | A A H S
   A B A C | A B A C
   H A I R | L E K U
   S C R Y | S T E D
   --------+--------
   D E E D | D E E M
   E I N E | I N T I
   E N O L | O V E R
   T E L T | L Y N E

请注意,转置“A”和用作“A”的空白应视为相同的解决方案。但是,使用列转置行应该被视为不同的解决方案。我希望这是有道理的。

答案 4 :(得分:-1)

这里有很多预先计算好的5x5。作为练习留给读者找到4个兼容的: - )

http://www.gtoal.com/wordgames/wordsquare/all5

相关问题