[Poj 1204] word puzzles (trie tree Brute Force Search)

Source: Internet
Author: User

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 minimize 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 coshould devise a program to speedup (hopefully !) Solution Finding in such puzzles.

The following figure extends strates 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 ctions 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 shocould 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 10QWSPILAATIRAGRAMYKEIAGTRCLQAXLPOIJLFVBUQTQTKAZXVMRWALEMAPKCWLIEACNKAZXKPOTPIZCEOFGKLSTCBTROPICALBLBCJEWHJEEWSMLPOEKORORALUPQWRNJOAAGJKMUSJAEKRQEIOLOAOQPRTVILCBZQOPUCAJSPPOUTMTSLPSFLPOUYTRFGMMLKIUISXSWWAHCPOIYTGAKLMNAHBVAEIAKHPLBGSMCLOGNGJMLLDTIKENVCSWQAZUAOEALHOPLPGEJKMNUTIIORMNCLOIUFTGSQACAXMOPBEIOQOASDHOPEPNBUYUYOBXBIONIAELOJHSWASMOUTRKHPOIYTJPLNAQWDRIBITGLPOINUYMRTEMPTMLMNBOPAFCOPLHAVAIANALBPFSMARGARITAALEMABARBECUETROPICALSUPREMALOUISIANACHEESEHAMEUROPAHAVAIANACAMPONESA

Sample output

0 15 G2 11 C7 18 A4 8 C16 13 B4 15 E10 3 D5 1 E19 7 C11 11 H

Source

Southwestern Europe 2002

 

It can be said that it is a trie template question. After inserting words into the trie tree, you can use each vertex on the map as the starting point and search for words in eight directions. Wa has 6 posts, and finally AC, note that when the current word has been found to an end (to terminate the node), do not stop searching! You can also use the AC automatic machine and trie Diagram for this question!

# Include <stdio. h> # include <iostream> # include <string. h> # define word 26 # define maxn 1050 using namespace STD; struct trie {trie * child [word]; // son node pointer int ID; // if this node is the end node of a word, id = the word number trie () {memset (child, 0, sizeof (child )); // The son node Initialization is empty id =-1 ;}}; // root = root node trie * root = new trie (); int DX [] = {-1, -,-1}; int dy [] = {, 0,-1,-1,-1}; // search direction, brute-force enumeration: int ans [maxn] [3], visit [maxn], L, C, and W in each vertex; // The Position of the word I = (ANS [I] [1], ANS [I] [2]), direction: ANS [I] [0] string C [maxn], word; // The Void build (string S, int num) matrix that stores all strings) // Insert the string s numbered num into the trie tree {int I; trie * P = root; // At the beginning, P points to the root node for (I = 0; I <S. size (); I ++) {If (p-> child [s [I]-'a'] = NULL) // no ready-made string corresponding to the position of the son node p-> child [s [I]-'a'] = new trie (); P = p-> child [s [I]-'a']; // move the pointer to the corresponding son node under the current node} p-> id = num; // write down the corresponding word number of the End Node} void search (INT Sx, int Sy, int DIR) // search trie tree, word start point (sx, Sy ), the extension direction is dir {in T xx = Sx, YY = sy; // coordinate of the current letter (XX, YY) trie * point = root; // point to the current trie Tree node while (XX> = 0 & XX <L & YY> = 0 & YY <C) // coordinates do not cross the border {If (! Point-> child [C [XX] [YY]-'a']) // The End Node is reached, but the word break is not over yet; elsepoint = point-> child [C [XX] [YY]-'a']; // move the pointer to the bottom of the node if (point-> ID! =-1) // This node is the end node {If (visit [point-> id] = 0) {visit [point-> id] = 1; ans [point-> id] [0] = dir; // record the start coordinate and direction of the word ans [point-> id] [1] = SX; ans [point-> id] [2] = sy;} xx + = DX [dir]; // move the coordinates YY + = Dy [dir];} int main () {int I, j, k; scanf ("% d", & L, & C, & W); for (I = 0; I <L; I ++) {CIN> C [I] ;}for (I = 0; I <W; I ++) {CIN> word; build (word, I); // insert words into the trie tree} for (I = 0; I <L; I ++) // enumeration start point (I, j) for (j = 0; j <C; j ++) for (k = 0; k <8; k ++) // brute-force enumerated word presence direction search (I, j, k); for (I = 0; I <W; I ++) printf ("% d % C \ n", ANS [I] [1], ans [I] [2], ANS [I] [0] + 'A'); Return 0 ;}


 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.