string - 拼字游戏检查

标签 string algorithm data-structures scrabble

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

  • 12 x E
  • 9 x A,我
  • 8 x O
  • 6 x N、R、T
  • 4 x 深、长、小、深
  • 3 克
  • 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

我想创建一个所有垂直的也都有效的。你能帮我解决这个问题吗?这不是家庭作业。这是一个 friend 向我求助的问题。

最佳答案

最终编辑:已解决!这是一个解决方案。

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 个字母单词的字典中构造一个前缀树。递归地,如果给定的图 block 放置为其列和行形成有效的前缀,并且该图 block 可用,并且下一个图 block 放置有效,则该图 block 放置是有效的。基本情况是,如果没有剩下要放置的图 block ,则它是有效的。

像 Glenn 所说的那样,只找到所有有效的 5x5 板,然后看看是否可以组合其中的任何四个板,这可能是有意义的。递归到 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 block 后,我修改了代码以列出所有有效的 5x5 block 。我现在有一个 150 MB 的文本文件,其中包含所有 4,430,974 个 5x5 解决方案。

我也尝试过只递归遍历全部 100 个图 block ,但它仍在运行。

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

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

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;
}

在找出有多少 block (将近 20 亿个,并且还在增加)之后,我转而尝试寻找某些类型的 block ,尤其是那些使用不常见的字母难以构建的 block 。我的希望是,如果我最终得到一组足够良性的字母进入最后一个 block ,那么广阔的有效 block 空间可能会有一个用于该组字母。

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

对于第一个 block ,我删除了空白 block ,认为最后一个 block 最需要这种灵 active 。让它运行一段时间后,我没有看到更好的方 block 出现,我选择了最好的方 block ,并从包中取出里面的方 block ,再次运行程序,得到了第二个方 block 。我在第三个街区重复了这个。然后对于最后一个 block ,我重新添加了空白并使用它找到的第一个有效 block 。

关于string - 拼字游戏检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4854478/

相关文章:

string - scala中原始字符串插值和三引号有什么区别

PHP 将除某些单词外的所有字母(包括斜线后)大写

JAVA - 在 30 个字符后的下一个空格处插入一个新行

javascript - 在数组中找到可以总和为目标值的可能数字

algorithm - 在熟人列表中查找姓名的复杂性如何

c - 发送到函数的简单字符指针不起作用

algorithm - 计算简单有向图的两个给定顶点之间的所有边不相交路径

c# - 3D 体素网格视线 Bresenham 算法

haskell - 转换为树之前的关联列表有多大

performance - 计算移动最大值