poj 1204 Word Puzzles(字典树)

Word Puzzles
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 8092 Accepted: 3068 Special Judge

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

题意:给出一个字符矩阵,要在这个矩阵中找到一些单词,要输出单词首字母在矩阵中的坐标,以及单词在矩阵中排列的方向。
 
思路:字典树。以要找的单词建树。在单词的末尾字符标志count为1。然后枚举整个矩阵的每个字符作为首字母,往8个方向搜索,搜到count为1即为找到一个单词。
 
AC代码:
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <iostream>

using namespace std;
int dir[8][2]= {{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}};   //保存8个方向
char s[1005][1005];           //保存字符矩阵
int x[1005],y[1005],d[1005];  //保存坐标和方向
int L,C,W;
int allocp=0,sum=0;
typedef struct TireNode
{
    struct TireNode *next[26];
    int count;         //成功查找到单词的标志
    int num;           //单词的编号
} TireNode;
TireNode Memory[1000000];    //快速获得空间分配
TireNode *root;              //字典树的树根
TireNode *CreatTireNode()    //创建新节点
{
    TireNode *p;
    p=&Memory[allocp++];
    p->count=0;
    p->num=0;
    for(int i=0; i<26; i++)
        p->next[i]=NULL;
    return p;
}
void InsertTire(char *str,int ii)     //插入到字典树中
{
    int i=0,k;
    TireNode *p=root;
    while(str[i])
    {
        k=str[i++]-'A';
        if(p->next[k]==NULL)
            p->next[k]=CreatTireNode();
        p=p->next[k];
    }
    p->count=1;            //末尾的count标记为1
    p->num=ii;
}
bool ok(int i,int j)   //判断坐标是否合法
{
    if(i>=0&&j>=0&&i<L&&j<C)
        return true;
    return false;
}
void query(int i,int j,int k)
{
    int tmp,nx,ny;
    TireNode *q=root;
    nx=i;
    ny=j;
    while(ok(nx,ny))          //如果坐标不越界,那么继续往这个方向搜下去
    {
        tmp=s[nx][ny]-'A';
        if(q->next[tmp]==NULL)   //字典树中无这个字符
            return;
        q=q->next[tmp];
        nx+=dir[k][0];
        ny+=dir[k][1];
        if(q->count)           //成功找到了一个单词
        {
            sum++;
            x[q->num]=i;       //保存坐标和方向
            y[q->num]=j;
            d[q->num]=k;
            q->count=0;         //因为可能存在某单词是另一个单词的前缀,所以在找到某个单词时要清除单词结尾的标志
                                //避免找下一个单词时未到结尾就标志找到
        }
    }
}
void search()
{
    sum=0;
    for(int i=0; i<L; i++)        //枚举每个字符作为起点
        for(int j=0; j<C; j++)
            for(int k=0; k<8; k++)         //往8个方向搜
            {
                query(i,j,k);
                if(sum==W)
                    return;
            }
}
int main()
{
    char str[2005];
    root=CreatTireNode();
    scanf("%d%d%d",&L,&C,&W);
    for(int i=0; i<L; i++)
        scanf("%s",s[i]);
    for(int i=1; i<=W; i++)
    {
        scanf("%s",str);
        InsertTire(str,i);
    }
    search();
    for(int i=1; i<=W; i++)
        printf("%d %d %c\n",x[i],y[i],'A'+d[i]);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值