Word Puzzles(字典树)

Description

Word puzzles are usually simple and very entertaining for all ages. They are so entertaining that Pizza-Hut company started using table covers with word puzzles printed on them, possibly with the intent to minimise their client‘s perception of any possible
delay in bringing them their order.

Even though word puzzles may be entertaining to solve by hand, they may become boring when they get very large. Computers do not yet get bored in solving tasks, therefore we thought you could devise a program to speedup (hopefully!) solution finding in such
puzzles.

The following figure illustrates the PizzaHut puzzle. The names of the pizzas to be found in the puzzle are: MARGARITA, ALEMA, BARBECUE, TROPICAL, SUPREMA, LOUISIANA, CHEESEHAM, EUROPA, HAVAIANA, CAMPONESA.

Your task is to produce a program that given the word puzzle and words to be found in the puzzle, determines, for each word, the position of the first letter and its orientation in the puzzle.

You can assume that the left upper corner of the puzzle is the origin, (0,0). Furthemore, the orientation of the word is marked clockwise starting with letter A for north (note: there are 8 possible directions in total).

Input

The first line of input consists of three positive numbers, the number of lines, 0 < L <= 1000, the number of columns, 0 < C <= 1000, and the number of words to be found, 0 < W <= 1000. The following L input lines, each one of size C characters, contain the
word puzzle. Then at last the W words are input one per line.

Output

Your program should output, for each word (using the same order as the words were input) a triplet defining the coordinates, line and column, where the first letter of the word appears, followed by a letter indicating the orientation of the word according to
the rules define above. Each value in the triplet must be separated by one space only.

Sample Input

20 20 10
QWSPILAATIRAGRAMYKEI
AGTRCLQAXLPOIJLFVBUQ
TQTKAZXVMRWALEMAPKCW
LIEACNKAZXKPOTPIZCEO
FGKLSTCBTROPICALBLBC
JEWHJEEWSMLPOEKORORA
LUPQWRNJOAAGJKMUSJAE
KRQEIOLOAOQPRTVILCBZ
QOPUCAJSPPOUTMTSLPSF
LPOUYTRFGMMLKIUISXSW
WAHCPOIYTGAKLMNAHBVA
EIAKHPLBGSMCLOGNGJML
LDTIKENVCSWQAZUAOEAL
HOPLPGEJKMNUTIIORMNC
LOIUFTGSQACAXMOPBEIO
QOASDHOPEPNBUYUYOBXB
IONIAELOJHSWASMOUTRK
HPOIYTJPLNAQWDRIBITG
LPOINUYMRTEMPTMLMNBO
PAFCOPLHAVAIANALBPFS
MARGARITA
ALEMA
BARBECUE
TROPICAL
SUPREMA
LOUISIANA
CHEESEHAM
EUROPA
HAVAIANA
CAMPONESA

Sample Output

0 15 G
2 11 C
7 18 A
4 8 C
16 13 B
4 15 E
10 3 D
5 1 E
19 7 C
11 11 H

解题思路:

输入需要查找的单词时时,为该单词建立字典树,在单词的结尾处记录该单词的编号,然后对整个图进行暴搜,以图中的每个点作为起点,从八个方向分别查找。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
const int maxn = 26;
char map[1005][1005], word[1005][1005];
int ans[1005][3];  // 记录三个答案,分别为坐标x,y,和方向
int l, c, w;
bool visited[1005];
int dir[8][2] = {-1,0,-1,1,0,1,1,1,1,0,1,-1,0,-1,-1,-1};  //八个方向从北开始按顺势针排序
struct Trie
{
    int n;
    Trie *next[maxn];
};
Trie *root;

void init()
{
    root = (Trie *)malloc(sizeof(Trie));
    for(int i = 0; i < maxn; i++)
    {
        root -> next[i] = NULL;
        root -> n = -1;  // 建立新指针的时候,n都赋为-1,这样所有n只能为-1或编号两种可能
    }
}

void insert(char *word, int id)
{
    Trie *temp = root;
    for(int i = 0; i < strlen(word); i++)
    {
        int pos = word[i] - 'A';
        if(temp -> next[pos] == NULL)
        {
            Trie *cur = (Trie *)malloc(sizeof(Trie));
            for(int j = 0; j < maxn; j++)
            {
                cur -> next[j] = NULL;
                cur -> n = -1;
            }
            temp -> next[pos] = cur;
        }
        temp = temp -> next[pos];
    }
    temp -> n = id;  // 单词结尾处记录单词编号
}
void search(int x,int y,int d)
{
    Trie *temp = root;
    int xx = x, yy = y;
    while(xx >= 0 && xx < l && yy >=0 && yy <c)
    {
        if(temp -> next[map[xx][yy] - 'A'] == NULL)  //如果指针为空就跳出
            break;
        else
            temp = temp -> next[map[xx][yy] - 'A'];  // 否则指针下移
        if(temp -> n != -1)   // 一直找到单词的末尾处
        {
            if(!visited[temp -> n])  // 已访问过就不需要记录答案
            {
                visited[temp -> n] = 1;
                ans[temp -> n][0] = x;
                ans[temp -> n][1] = y;
                ans[temp -> n][2] = d;
            }
        }
        xx += dir[d][0];  // 朝某一方向搜索
        yy += dir[d][1];
    }
}
int main()
{
    scanf("%d%d%d", &l, &c, &w);
    for(int i = 0; i < l; i++)
        scanf("%s", map[i]);
    init();
    memset(visited, 0, sizeof(visited));
    for(int i = 0; i < w; i++)
    {
        scanf("%s", word[i]);
        insert(word[i], i);   // 建树
    }
    for(int i = 0; i < l; i++)  //对整个图进行暴搜
        for(int j = 0; j < c; j++)
            for(int k = 0; k < 8; k++)
                search(i,j,k);
    for(int i = 0; i < w; i++)
        printf("%d %d %c\n", ans[i][0], ans[i][1], ans[i][2] + 'A');
}

Word Puzzles(字典树)

时间: 2024-08-14 17:37:06

Word Puzzles(字典树)的相关文章

poj 1204 Word Puzzles(字典树)

题目链接:http://poj.org/problem?id=1204 思路分析:由于题目数据较弱,使用暴力搜索:对于所有查找的单词建立一棵字典树,在图中的每个坐标,往8个方向搜索查找即可: 需要注意的是查找时不能匹配了一个单词就不在继续往该方向查找,因为在某个坐标的某个方向上可能会匹配多个单词,所以需要一直 查找直到查找到该方向上最后一个坐标: 代码如下: #include <cstdio> #include <cstring> #include <iostream>

[POJ 1204]Word Puzzles(Trie树暴搜)

Description Word puzzles are usually simple and very entertaining for all ages. They are so entertaining that Pizza-Hut company started using table covers with word puzzles printed on them, possibly with the intent to minimise their client's percepti

UVaLive 3942 Remember the Word (dp+字典树)

Remember the Word DescriptionNeal is very curious about combinatorial problems, and now here comes a problem about words. Knowing that Ray has a photographic memory and this may not trouble him, Neal gives it to Jiejie. Since Jiejie can’t remember nu

[POJ 1204]Word Puzzles(Trie树暴搜&amp;amp;AC自己主动机)

Description Word puzzles are usually simple and very entertaining for all ages. They are so entertaining that Pizza-Hut company started using table covers with word puzzles printed on them, possibly with the intent to minimise their client's percepti

LA 3942 - Remember the Word (字典树 + dp)

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1943 题目大意: 给定一个字符串和给定一个单词集合.问从给定单词集合中选取单词,有多少种选取方法刚好拼接成字符串. 例如: abcd 4 a b cd ab 有两种 a-b-cd ab-cd 这两种情况 解题思路: 因为给定的字符串的长度是3*10^5所以暴力就不能解决问题了

LA 3942 Remember the Word(字典树+DP)

题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1943 题意:一个长字符串和多个短字符串,求短字符串有多少种方式组成长字符串. 状态转移方程: dp[i] = sum(d[i + len(x)])  (x是s[i...L]的前缀) 对于每个i,如果直接暴力寻找s[i...L]的前缀,复杂度为O(nm) (n为短字符

LA、Remember the Word (字典树, 简单dp)

传送门 题意: 给你一个初始串 S,strlen(s) <= 3e5  然后给你 n 个单词. n <= 4000,  每个单词的长度不超过 100 : 问你这个初始串,分割成若干个单词的连接的方案:(这些单词必须是给定的n个单词中的任意一个,一个单词可以被使用多次.) 解: 将 n 个单词建个字典树: dp[ i ] 表示,S的 0 ~ i - 1 切割成若干个 单词的方案数: 枚举S, 枚举到 当前位置 i: 然后就在字典树找,以 S 的 i + 1 开始的, 能不能找到一个单词与之匹配:

hdu 1247 Hat’s Words (字典树模板)

//那个单词是有出现的两个单词构成的 # include <cstdio> # include <cstring> # include <algorithm> # include <iostream> # define MAX 26 using namespace std; typedef struct Trie_Node { bool isWord; struct Trie_Node *next[MAX]; } Trie; char s[50000][50

字典树PKU 1204 Word Puzzles

PKU 1204 Word Puzzles 题目的大意为: 要求编程实现查找字谜拼图中的单词,输出查找到的单词的起始位置和方向(ABCDEFGH分别代表北.东北.东.东南.南.西南.西.西北八个方向). 输入: 输入的第一行包含三个正数:行数,0<L<=1000:列数,0<C<= 1000:和字谜单词个数0<W<=1000.接下来的L行,每行输入C个字符.最后W行每行输入一个字谜单词. 输出: 你的程序应该每行输出每个单词(跟输入的顺序相同)第一个字母出现的行和列的下标