您的位置:首页 > 其它

trie学习 poj1204 Word Puzzles

2014-10-08 15:53 344 查看
Word Puzzles

Time Limit: 5000MS Memory Limit: 65536K
               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

Source
Southwestern Europe 2002

题意:给出一个字母阵,然后询问多个字符串,在字母阵中的位置,描述为一个起点和一个方向(8个方向)。

思路:将需要询问的字符串组成一颗trie,在字母阵中从每个点进行八个方向的暴力搜索。

代码:

//#pragma warning(disable:4996)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node{
node *next[26];
int id;
node(){
memset(next,0,sizeof(next));
id = 0;
}
}*head;
char map[1005][1005];
char a[1005];
int r,c;
int xx,yy;
//八个方向是有顺序的
int dirx[8] = {-1,-1,0,1,1,1,0,-1};
int diry[8] = {0,1,1,1,0,-1,-1,-1};
void build(node *head, char *s,int id){// build a trie
int length = strlen(s);
for (int i = 0; i < length; i++)
{
int k = s[i]-'A';
if(head->next[k] == NULL)
head->next[k] = new node();
head = head->next[k];
}
head->id = id;
}
int ans[1005][3];
void dfs(node *p, int x,int y,int d){
if(p == NULL)return ;
if(p->id != 0){//找到了一个单词
ans[p->id][0] = xx;
ans[p->id][1] = yy;
ans[p->id][2] = d;
p->id = 0;//防止之后还会找到这个
}
if(x<0||x>=r||y<0||y>=c)return ;
dfs(p->next[map[x][y]-'A'],x+dirx[d],y+diry[d],d);
}
int main(){
head = new node();
int w;
while(cin>>r>>c>>w){
for (int i = 0; i < r; i++)
scanf("%s",map[i]);
for (int i = 1; i <= w; i++){
scanf("%s",a);
build(head,a,i);
}
node *p;
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
for (int k = 0; k < 8; k++)
{
xx = i,yy = j;
p = head;
dfs(p,i,j,k);
}
for (int i = 1; i <= w; i++)
{
printf("%d %d %c\n",ans[i][0],ans[i][1],ans[i][2]+'A');
}
}
return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  string trie