UVa 10010 Where ' s Waldorf? (Eight directional string matching & template questions)

Source: Internet
Author: User
Tags time limit

Time limit:3.000 seconds

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=96&page=show_problem &problem=951

Given a m by n grid of letters, ( ), and a list of words, find the location into the grid at which the word can Found. A word matches a straight, uninterrupted line of letters in the grid. A Word can match the letters in the grid regardless of case (i.e. upper and lower case letters are to be treated as the SA Me). The matching can is done in the eight directions either horizontally, vertically or diagonally through the grid.

Input

The input begins with a single positive integer in a line by itself indicating the number of cases following, each of them as described below. This are followed by a blank line, and there are also a blank line between two consecutive.

The input begins with a pair of integers, m followed by n, in decimal notation on a single Lin E. The next m lines contain n letters each; This is the grid of letters in which the words of the list must to be found. The letters in the "grid may" in upper or lower case. Following the grid of letters, another integer K appears on a line by itself (). The next k lines of input contain the list of words to search for, one word per line. These words could contain upper and lower case letters only (no spaces, hyphens or non-alphabetic).

Output

For each test case, the output must follow the description below. The outputs of two consecutive cases is separated by a blank line.

For each word in the word list, a pair of integers representing the location of the corresponding word in the grid must is Output. The integers must is separated by a. The ' the ' the ' the ' in ' grid where the ' the ' the ' given word can be found (1 represents the topmost Line in the grid, and m represents the "bottommost line". The second integer is the column in the grid where the ' the ' given word can be found (1 represents the LEFTM OST column in the grid, and n represents the rightmost column in the grid. If A word can be found more than once in the grid, then the location which are output should correspond to the uppermost OC Curence of the word (i.e. the occurence which places the "the" the word closest to the top of the grid). If two or words are uppermost, the output should correspond to the leftmost. All words can is found at least once in the grid.

Sample Input

1

8
Abcdefghigg
hebkwaldork
ftyawaldorm
ftsimrlqsrc
byoarbedeyv
Klcbqwikomk
STREBGADHRB
YUIQLXCNBJF
4
Waldorf
Bambi
Betty
Dagbert

Sample Output

2 5
2 3
1 2 7 8

Complete code:

/*0.022s*/#include <cstdio> #include <cctype> #include <cstring> int m, n, Curm, cur  
N  
    
Char grid[100][100], word[100];  
    X,y table Direction inline bool Match (int x, int y) {int I, j, K;  for (k = 0, I = Curm, j = Curn I >= 0 && i < m && J >= 0 && J < n && Word[k];  
    i = = x, j + = y, k++) if (Word[k]!= grid[i][j]) return false;  
if (word[k] = = 0) return true;///to show that Word has read return false; ///Topic said must be able to find ~ inline void found () {for (Curm = 0; Curm < m. curm++) for (Curn = 0; Curn &l T N  
                    curn++) for (int x =-1; x <= 1; x + +) for (int y =-1; y <= 1; y++)  
if (x | | y) && match (x, y)) return;  
    int main (void) {int N, K, I, J, Len;  
            while (~SCANF ("%d", &n)) {while (n--) {scanf ("%d%d", &m, &n); For (i = 0; i < m; i++) {GetChar ();  
            for (j = 0; J < N; j +) Grid[i][j] = ToUpper (GetChar ());  
            } scanf ("%d\n", &k);  
                while (k--) {gets (word);  
                Len = strlen (word);  
                for (i = 0; i < len; ++i) Word[i] = ToUpper (word[i));  
                Find ();  
            printf ("%d%d\n", Curm + 1, Curn + 1);  
        } if (n) putchar (' \ n ');  
} return 0; }

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

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.