(字典树+暴搜) poj 1204

Word Puzzles

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 9948   Accepted: 3726   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

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
int n,m,p;
char s[1010][1010],tt[1010];
int mp[1010][1010],next[1000010][27],cnt,endd[1000010],ansx[1010],ansy[1010];
char ansd[1010];
const int dx[8]={-1,-1,0,1,1,1,0,-1};
const int dy[8]={0,1,1,1,0,-1,-1,-1};
void insertt(int x)
{
    int temp=0,alp;
    scanf("%s",tt);
    for(int i=0;tt[i];i++)
    {
        alp=tt[i]-‘A‘;
        if(!next[temp][alp]) next[temp][alp]=++cnt;
        temp=next[temp][alp];
    }
    endd[temp]=x;
}
void findd(int sx,int sy,int dic)
{
    int temp=0,alp,x=sx,y=sy,e;
    while(next[temp][alp=mp[x][y]])
    {
        temp=next[temp][alp];
        e=endd[temp];
        if(e) ansx[e]=sx,ansy[e]=sy,ansd[e]=dic;
        x=x+dx[dic];
        y=y+dy[dic];
        if(x<0||x>=n||y<0||y>=m)
            return ;
    }
}
int main()
{
    while(scanf("%d%d%d",&n,&m,&p)!=EOF)
    {
        cnt=0;
        for(int i=0;i<n;i++)
            scanf("%s",s[i]);
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                mp[i][j]=s[i][j]-‘A‘;
            }
        }
        for(int i=1;i<=p;i++)
            insertt(i);
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                for(int k=0;k<8;k++)
                    findd(i,j,k);
            }
        }
        for(int i=1;i<=p;i++)
            printf("%d %d %c\n",ansx[i],ansy[i],ansd[i]+‘A‘);
    }
    return 0;
}

  

时间: 2024-10-12 20:50:58

(字典树+暴搜) poj 1204的相关文章

[POJ 1204]Word Puzzles(Trie树暴搜)

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 percepti

[POJ 1204]Word Puzzles(Trie树暴搜&amp;amp;AC自己主动机)

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 percepti

字典树模板题 POJ 2503

1 #include <cstdio> 2 #include <cstring> 3 4 char en[11],fr[11]; 5 int st; 6 struct Tire{ 7 int next[26]; 8 char eng[11]; 9 }node[200005]; 10 void insert(char *s,int cur) 11 { 12 if(*s){ 13 if(!node[cur].next[*s-'a']) 14 node[cur].next[*s-'a']

并查集+欧拉回路+字典树 Colored Sticks POJ 2513

输入多组数据,每组数据两种颜色,表示一根木头两端的颜色,现在要将这些木头相连,要求相连部分颜色相同,问能否全部连通 提示 1)一个要判断所有的木头是否在一个集合中,即是否能相连 2)判断一种颜色出现的数量 3)一棵树如果只有0或2个点出现次数为奇数,则树可以一笔画成 #include <stdio.h> #include <string.h> #define maxn 500005 int tot; int f[maxn]; int num[maxn]; struct trie {

poj 1204 Word Puzzles(字典树)

题目链接:http://poj.org/problem?id=1204 思路分析:由于题目数据较弱,使用暴力搜索:对于所有查找的单词建立一棵字典树,在图中的每个坐标,往8个方向搜索查找即可: 需要注意的是查找时不能匹配了一个单词就不在继续往该方向查找,因为在某个坐标的某个方向上可能会匹配多个单词,所以需要一直 查找直到查找到该方向上最后一个坐标: 代码如下: #include <cstdio> #include <cstring> #include <iostream>

POJ 1204 Word Puzzles(字典树+搜索)

题意:在一个字符矩阵中找每个给定字符串的匹配起始位置和匹配方向(A到H表示八个方向): 思路:将给定字符串插入字典树中,遍历字符矩阵,在每个字符处向八个方向用字典树找. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef struct node { int num; node *next[26]; }node; node *head; char str[1

POJ 2503 字典树

这题记得以前是我们周赛的题,然后用的是map,也暴过了. 因为这两天要给大一的讲字典树,所以练练几道的代码,以防给大一搞晕了-- #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<map> #include<queue> #include<set> #include<cmath> #include

poj 3764 The xor-longest Path(字典树)

题目链接:poj 3764 The xor-longest Path 题目大意:给定一棵树,每条边上有一个权值,找出一条路径,使得路径上权值的亦或和最大. 解题思路:dfs一遍,预处理出每个节点到根节点路径的亦或和rec,那么任意路径均可以表示rec[a] ^ rec[b],所以问题 就转换成在一些数中选出两个数亦或和最大,那么就建立字典树查询即可. #include <cstdio> #include <cstring> #include <algorithm> us

poj 2513 并查集,Trie(字典树), 欧拉路径

- Colored Sticks POJ - 2513 You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sticks in a straight line such that the colors of the endpoints that touch are of the same color?