Word Puzzles
Time Limit: 5000MS | Memory Limit: 65536K | |||
Total Submissions: 8894 | Accepted: 3342 | 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).
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
这题够无语的,写出来了,然后有的样例没过,又检查了好久,妹妹啊!原来是可能有两个字符串同一棵子树,所以得继续搜下去。然后有一个是判断val[u]为不为真的时候然后才判断它们的坐标是否已经出界,就是在这WA得想放弃了……靠……想了想,觉得可能在出界之前val[u]可能为真就出错了,所以换了下顺序就过了……fuck!!!
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <list>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define PI acos(-1.0)
#define mem(a,b) memset(a,b,sizeof(a))
#define sca(a) scanf("%d",&a)
#define pri(a) printf("%d\n",a)
#define lson i<<1,l,mid
#define rson i<<1|1,mid+1,r
#define MM 200205
#define MN 1005
#define INF 10000007
using namespace std;
char str[MN][MN],ss[MN];
int val[MM],sz=1,vis[27],out[MN][3],x,y,n,m;
int dis[8][2]={-1,0,-1,1,0,1,1,1,1,0,1,-1,0,-1,-1,-1};
struct Trie
{
int next[27];
} trie[MM];
int idx(char c) { return c-'A'; }
void insert(char *s,int k)
{
int u=0,i,c,l=strlen(s);
for(i=0; i<l; i++)
{
c=idx(s[i]);
if(!trie[u].next[c]) trie[u].next[c]=sz++;
u=trie[u].next[c];
}
val[u]=k;
}
void dfs(int u,int i,int j,int q)
{
if(!u||i<0||i>=n||j<0||j>=m) return; //i<0之后的条件刚开始放在if(val[u])条件之后的,然后WA了6发,差点想暂时放弃了
if(val[u])
{
out[val[u]][0]=x,out[val[u]][1]=y,out[val[u]][2]=q;
val[u]=0;
}
int l=str[i+dis[q][0]][j+dis[q][1]]-'A'; //下一个搜索的是当前结点+下一个字母的结点,和insert原理相同
dfs(trie[u].next[l],i+dis[q][0],j+dis[q][1],q);
}
int main()
{
int p,i,j,k;
scanf("%d%d%d",&n,&m,&p);
for(i=0; i<n; i++)
scanf("%s",str[i]);
for(i=1; i<=p; i++)
scanf("%s",ss),insert(ss,i),vis[ss[0]-'A']=1;
for(i=0; i<n; i++)
for(j=0; j<m; j++)
if(vis[str[i][j]-'A'])
for(k=0;k<8;k++)
{
x=i,y=j;
dfs(trie[0].next[str[i][j]-'A'],i,j,k);
}
for(i=1;i<=p;i++)
printf("%d %d %c\n",out[i][0],out[i][1],out[i][2]+'A');
return 0;
}