现在的位置: 首页 > 综合 > 正文

【字典树】 poj1204 Word Puzzles

2018年01月14日 ⁄ 综合 ⁄ 共 3208字 ⁄ 字号 评论关闭
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

题意:给一个字母地图和一些字符串,问那些字符串在那个地图第一次出现的位置,输出字符串第一个字母的位置和字符串的方向。

题解:用字符串建字典树,枚举地图每个点的8个方向。

#include<cstdio>
#include<cstring>
using namespace std;
struct node
{
    node *next[26];
    int id;
    node()
    {
        memset(next,0,sizeof(next));
        id=0;
    }
}*head;
int dir[][2]={-1,0,-1,1,0,1,1,1,1,0,1,-1,0,-1,-1,-1};//方向
char mat[1005][1005];//地图
char s[1005];
int out[1005][3];
int cnt,x,y;
int n,m,w;
void build(char *s,node *head,int id)//建树
{
    int len=strlen(s),k;
    for(int i=0;i<len;++i)
    {
        k=s[i]-'A';
        if(head->next[k]==NULL)
           head->next[k]=new node();
        head=head->next[k];
    }
    head->id=id;
}
void dfs(node *p,int a,int b,int c)//深搜
{
    if(p==NULL) return;
    if(p->id>0)
    {
        out[p->id][0]=x;out[p->id][1]=y;out[p->id][2]=c;
        p->id=0;
        //这里不能return
    }
    if(a<0||n<=a||b<0||m<=b) return;
    dfs(p->next[mat[a][b]-'A'],a+dir[c][0],b+dir[c][1],c);
}
int main()
{
    head=new node();
    node *p;
    scanf("%d%d%d",&n,&m,&w);
    //输入地图
    for(int i=0;i<n;++i)
        scanf("%s",mat[i]);
    for(int i=1;i<=w;++i)
    {
        scanf("%s",s);
        build(s,head,i);
    }
    //枚举地图上每个点
    for(int i=0;i<n;++i)
        for(int j=0;j<m;++j)
            for(int k=0;k<8;++k)
            {
                x=i;y=j;
                p=head;
                dfs(p,i,j,k);
            }
    //输出
    for(int i=1;i<=w;++i)
        printf("%d %d %c\n",out[i][0],out[i][1],out[i][2]+'A');
    return 0;
}

抱歉!评论已关闭.