HDU 1875 word puzzle

Source: Internet
Author: User

Link:

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1857

Question:

Word Puzzle

Time Limit: 3000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 427 accepted submission (s): 76


Problem descriptiondid you heard of a little game named "Word Puzzle "? If you didn't, what a pity!
In the game, you will be given a rectangular grid of letters, in which several words are hidden. each word may begin anywhere in the puzzle, and may be oriented in any straight line horizontally, vertically, or diagonally. however, the words must all go down,
Right, or down-Right. A dictionary is also given to you, indicating the words to be found in the grid.

You task is to find the locations of each word within the grid.

 


InputThere is only one test case.

The first line is two integers R and C separated by a whitespace. R (20≤r ≤ 500) is the number of rows of the grid. C (20 ≤ C ≤ 500) is the number of columns of the grid.

The following R lines, each line will contains exactly C characters without anything else. Each character is in the range 'a'-'Z '.

A blank line will be followed after the grid.

The following lines, each line contains a unique word in the dictionary. each word will contain between 1 and 20 characters (also in the range 'a'-'Z '). the dictionary consists of at most 10000 words.

-1 means the end of dictionary.

 


Outputfor each word, output the "Row col" (quotes for clarity) pair, where row is the 0-based row in which the first letter of the word is found, and col is the 0-based column in which the first letter of the word is found. if the same word can be found more
Once, the location in the lowest-indexed row shoshould be returned. if there is still a tie, return the location with the lowest-indexed column. if a word cannot be found in the grid, return "-1-1" for the word.


Sample Input

3 5HENRYGAVINMAGICHENRYHGMHAGMAVIN-1
 


Sample output

0 00 00 0-1 -1
 


Authorxrtgavin @ tju


Sourcehdu 2007 programming contest-FinalQuestion:To a matrix of letters, a word can be formed in a matrix that is horizontal, vertical, and oblique (top left ---> bottom right. Then, ask if some words exist in the matrix. If it exists, output its starting position (the starting position is above, the left is better ).

Analysis and Summary:I thought about it for two days. Is it easy for me.
1. At first glance, I felt that I had a question, and I directly enumerated the start point of the matrix. Then I constructed the trie tree for all possible words in the matrix, and saved the start point in the trie. Then I submitted the results happily.Memory
Limit exceeded
2. Keep thinking and thinking... wireless tangle
3. finally, the light bulb on my mind lit up and found that there were only one million words to ask. In fact, we can build a trie tree with the words in the matrix, and then enumerate the words in the matrix, to check whether the enumerated word is in trie, save the position. After enumeration is complete, the information about the position where the word is saved is displayed in trie.
4. Then I will look at the shining light bulb on the ACM official logo image. I seem to have some insights... isn't ACM pursuing the moment when the light bulb is on? If ACM is used, the light bulb will be made when it is not lit, and there will be less fun.


Code:

# Include <iostream> # include <cstdio> # include <cstring> using namespace STD; const int kind = 26; const int maxn = 1000000; int cnt_node; int R, C; char map [505] [505]; char word [10005] [22]; struct node {bool isword; int R, C; node * Next [kind]; void Init () {r = c =-1; isword = false; memset (next, 0, sizeof (next) ;}} heap [maxn]; inline node * new_node () {heap [cnt_node]. init (); Return & heap [cnt_node ++];} // create the trie tree void insert (node * root, char * Str) based on the word to be asked) {for (char * P = STR; * P; ++ p) {int CH = * P-'A'; If (root-> next [CH] = NULL) root-> next [CH] = new_node (); root = root-> next [CH];} root-> isword = true ;} // check whether the enumerated words in the matrix are searched by void search (node * root, char * STR, int row, int col) {for (char * P = STR; * P; ++ p) {int CH = * P-'A'; If (root-> next [CH] = NULL) return; root = root-> next [CH]; If (root-> isword & root-> r =-1 & root-> C =-1) {root-> r = row, root-> C = Col ;}} if (root-> isword & root-> r =-1 & root-> C =-1) {root-> r = row, root-> C = Col ;}/// output the position of the queried word in the matrix void output (node * root, char * Str) {for (char * P = STR; * P; ++ p) {int CH = * P-'A'; If (root-> next [CH] = NULL) return; root = root-> next [CH];} If (root-> isword) printf ("% d \ n", root-> r, root-> C);} int main () {// trie init cnt_node = 0; node * root = new_node (); scanf ("% d % * C ", & R, & C); For (INT I = 0; I <r; ++ I) {gets (Map [I]);} gets (word [0]); // remove spaces int Pos = 0; while (gets (word [POS]) {If (word [POS] [0] = '-') break; insert (root, word [POS ++]);} Char STR [30]; for (INT I = 0; I <r; ++ I) {for (Int J = 0; j <C; ++ J) {int end_r, end_c = J; // vertical if (I + 20 <R) end_r = I + 20; else end_r = r; memset (STR, 0, sizeof (STR); For (int K = I, P = 0; k <end_r; ++ K) STR [p ++] = map [k] [J]; search (root, STR, I, j ); // If (J + 20 <c) end_c = J + 20; else end_c = C; memset (STR, 0, sizeof (STR )); for (int K = J, P = 0; k <end_c; ++ K) STR [p ++] = map [I] [k]; search (root, STR, i, j); // skew int r = I, C = J, P = 0; memset (STR, 0, sizeof (STR )); while (r <end_r & C <end_c) {STR [p ++] = map [r] [c]; ++ R, ++ C ;}search (root, STR, I, j) ;}}for (INT I = 0; I <Pos; ++ I) {output (root, word [I]) ;}return 0 ;}

 -- The significance of life is to give it a meaningful person.

Original Http://blog.csdn.net/shuangde800,
D_double (reprinted please mark)


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.