POJ 1204 Word Puzzles - dic_tree + DFS

Word Puzzles
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 9487 Accepted: 3535 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
 
此题单纯用DFS会比较麻烦,加上字典树,即将要寻找的单词放入字典树,标记单词的根,再在所给puzzle中找,
由于要输出起点及方向,可以一个个的找,只是很容易超时,所以用这个方法要用scanf输入

 
 
#include
    
    
     
     
#include
     
     
      
      
int head;
char puz[1005][1005];
char w[1005];
int mark[1005][3];
int n,m,k,x1,y1;
int dx[]={-1,-1,0,1,1,1,0,-1};
int dy[]={0,1,1,1,0,-1,-1,-1};

struct node
{
    int next[26];
    int v;
}trie[210000];

void insert_trie(char s1[],int id)
{
    int t = 0;
    int l = strlen(s1);
    for(int i = 0; i < l; i++)
    {
        int d = s1[i] - 'A';
        if(trie[t].next[d] == -1)
            trie[t].next[d] = ++head;
        t = trie[t].next[d];
    }
    trie[t].v = id;
}

void dfs(int t,int x,int y,int dir)
{
    int p = puz[x][y] - 'A';
    if(t == -1)
        return;

    if(trie[t].v != -1)
    {
        int temp = trie[t].v;
        mark[temp][0] = x1;
        mark[temp][1] = y1;
        mark[temp][2] = dir;
        trie[t].v = -1;
    }
    if(x < 0 ||x >= n || y < 0 ||y >= m)
        return;
    dfs(trie[t].next[p],x+dx[dir],y+dy[dir],dir);
}
int main()
{
    int i,j,l;
    while(scanf("%d%d%d",&n,&m,&k)!=EOF)
    {
        head = 0;
        for(i = 0; i < 210000; i++){
                trie[i].v = -1;
            for(j = 0; j < 26; j++){
                trie[i].next[j] = -1;
            }
        }
        for(i = 0; i < n; i++)
            scanf("%s",puz[i]);
        for(i = 0; i < k; i++)
        {
            scanf("%s",w);
            insert_trie(w,i);
        }
        for(i = 0; i < n; i++)
            for(j = 0; j < m; j++)
               for(l = 0; l < 8; l++)
        {
            x1 = i;
            y1 = j;
            dfs(0,i,j,l);
        }
        for(i = 0; i < k; i++)
        {
            printf("%d %d %c\n",mark[i][0],mark[i][1],mark[i][2]+'A');
        }
    }
}

     
     
    
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值