Description
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
Output
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
//
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
//给出一个N*L的字符矩阵,再给出M个字符串,问这M个字符串在这个字
//符矩阵中出现的位置。
//逆向思维
//先将M个字符串构造成字典树,然后再字符矩阵上枚举
const int maxn=1100;
struct trie
{
int num;
int index;
trie* next[26];
};
//静态链表
trie temp[1000010];
int tp;
//.......
trie* root;//trie树根节点
int c,l;//主串行 列
int n;//模式串个数
char mat[maxn][maxn];//主串
char str[maxn];//模式串
int move[8][2]={-1,0,-1,1,0,1,1,1,1,0,1,-1,0,-1,-1,-1};
struct node//结果
{
int x,y,d;//头字母位置 方向
};
node ans[maxn];
void reset(trie* p)
{
p->num=0,p->index=-1;
for(int i=0;i<26;i++) p->next[i]=NULL;
}
void init()
{
tp=0;
root=&temp[tp++];
reset(root);
for(int i=0;i<maxn;i++) ans[i].d=-1;
}
void insert(char* word,int index)
{
trie* p=root;
int i=0;
while(word[i])
{
int x=word[i]-'A';
if(p->next[x]==NULL)
{
p->next[x]=&temp[tp++];
reset(p->next[x]);
}
p=p->next[x];
i++;
}
p->num++,p->index=index;
}
void check(int x,int y,int d)
{
int i=x,j=y;
trie* p=root;
while(i>=0&&i<c&&j>=0&&j<l&&p!=NULL)
{
int id=mat[i][j]-'A';
trie* cnt=p->next[id];
if(cnt!=NULL&&cnt->num&&ans[cnt->index].d==-1)
{
ans[cnt->index].x=x;
ans[cnt->index].y=y;
ans[cnt->index].d=d;
}
i+=move[d][0],j+=move[d][1];
p=cnt;
}
}
void solve()
{
for(int i=0;i<c;i++)
{
for(int j=0;j<l;j++)
{
for(int k=0;k<8;k++)
{
check(i,j,k);
}
}
}
for(int i=1;i<=n;i++) printf("%d %d %c/n",ans[i].x,ans[i].y,ans[i].d+'A');
}
int main()
{
init();
scanf("%d%d%d",&c,&l,&n);
for(int i=0;i<c;i++) scanf("%s",mat[i]);
for(int i=1;i<=n;i++)
{
scanf("%s",str);
insert(str,i);
}
solve();
return 0;
}