Question 10010-where's Waldorf?

Source: Internet
Author: User

Where's Waldorf?


Given a m by N Grid
Of letters ,(),
And a list of words, find the location in the grid at which the word can be 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 same). The matching can be done in any of the eight directions either horizontally, vertically or diagonally through the grid.

Input

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

The input begins with a pair of integers,MFollowedN, In
Decimal notation on a single line. The nextMLines containNLetters each; this is the grid of letters in which the words of the list must be found. The letters in the grid may be in upper or lower case. Following the grid of letters, another
IntegerKAppears on a line by itself (). The nextKLines of input contain the list of words to search
For, one word per line. These words may contain in upper and lower case letters only (no spaces, hyphens or other non-alphabetic characters ).

Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be 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 be output. the integers must be separated by a single space. the first integer is the line
In the grid where the first letter of the given word can be found (1 represents the topmost line in the grid, andMRepresents the bottommost Line). The second integer is the column in the grid where the first letter of the given word can be found
(1 represents the leftmost column in the grid, andNRepresents the rightmost column in the grid ). if a word can be found more than once in the grid, then the location which is output shoshould correspond to the uppermost occurence of the word (I. e.
The occurence which places the first letter of the word closest to the top of the grid ). if two or more words are uppermost, the output shocould correspond to the leftmost of these occurences. all words can be found at least once in the grid.

Sample Input
18 11abcDEFGhigghEbkWalDorkFtyAwaldORmFtsimrLqsrcbyoArBeDeyvKlcbqwikomkstrEBGadhrbyUiqlxcnBjf4WaldorfBambiBettyDagbert
Sample output
2 52 31 27 8

Miguel Revilla
2000-08-22

[General idea ]:

Input:

A grid consisting of letters, n columns in m rows. Find the position of a word in the grid. A word matches a letter in the grid that is not broken. It can be matched in any direction, and a total of eight directions can be matched. Case Insensitive.

There are K strings to be matched.

Output:

Each group of outputs has a blank line.

M n: M indicates the top row of the match.

N indicates the bottom row of the match.

If there are multiple results, only matching strings are output. Requirement: the first letter of the matching string must be the highest and leftmost. There must be at least one result.

[Code ]:

/********************************** Date: * Author: sjf0115 * question: Question 10010-where's Waldorf? * Source: http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 12 & page = show_problem & problem = 951 * result: AC * Source: VA * conclusion: * *********************************/# include <stdio. h> # include <string. h> char matrix [51] [51]; char STR [21], temp [21]; int startr, startc; // m rows, n columns, int match (INT m, int N, Int & startr, Int & startc) {int I, J, K, flag; startr = 51, startc = 51; int Len = strlen (STR ); for (I = 0; I <m; I ++) {for (j = 0; j <n; j ++) {flag = 1; // left-Rig Htif (J + Len <= N) {flag = 0; For (k = 0; k <Len; k ++) {If (STR [k]! = Matrix [I] [J + k]) {flag = 1; break ;}} if (flag = 0) {If (startr> I + 1) {startr = I + 1; startc = J + 1;} else if (startr = I + 1 & startc> J + 1) {startr = I + 1; startc = J + 1 ;}}// right-leftif (J-len + 1 >=0) {flag = 0; For (k = 0; k <Len; k ++) {If (STR [k]! = Matrix [I] [J-K]) {flag = 1; break ;}} if (flag = 0) {If (startr> I + 1) {startr = I + 1; startc = J + 1;} else if (startr = I + 1 & startc> J + 1) {startr = I + 1; startc = J + 1 ;}}// up-downif (I + Len <= m) {flag = 0; For (k = 0; k <Len; k ++) {If (STR [k]! = Matrix [I + k] [J]) {flag = 1; break ;}} if (flag = 0) {If (startr> I + 1) {startr = I + 1; startc = J + 1;} else if (startr = I + 1 & startc> J + 1) {startr = I + 1; startc = J + 1 ;}}// down-upif (I-len + 1 >=0) {flag = 0; For (k = 0; k <Len; k ++) {If (STR [k]! = Matrix [I-K] [J]) {flag = 1; break ;}} if (flag = 0) {If (startr> I + 1) {startr = I + 1; startc = J + 1;} else if (startr = I + 1 & startc> J + 1) {startr = I + 1; startc = J + 1 ;}}// right-upif (J + Len <= N & I-len + 1> = 0) {flag = 0; for (k = 0; k <Len; k ++) {If (STR [k]! = Matrix [I-K] [J + k]) {flag = 1; break ;}}if (flag = 0) {If (startr> I + 1) {startr = I + 1; startc = J + 1;} else if (startr = I + 1 & startc> J + 1) {startr = I + 1; startc = J + 1 ;}}// right-downif (J + Len <= N & I + Len <= m) {flag = 0; for (k = 0; k <Len; k ++) {If (STR [k]! = Matrix [I + k] [J + k]) {flag = 1; break ;}}if (flag = 0) {If (startr> I + 1) {startr = I + 1; startc = J + 1;} else if (startr = I + 1 & startc> J + 1) {startr = I + 1; startc = J + 1 ;}}// left-upif (J-len + 1 >=0 & I-len + 1 >=0) {flag = 0; for (k = 0; k <Len; k ++) {If (STR [k]! = Matrix [I-K] [J-K]) {flag = 1; break ;}}if (flag = 0) {If (startr> I + 1) {startr = I + 1; startc = J + 1;} else if (startr = I + 1 & startc> J + 1) {startr = I + 1; startc = J + 1 ;}}// left-downif (J-len + 1 >=0 & I + Len <= m) {flag = 0; for (k = 0; k <Len; k ++) {If (STR [k]! = Matrix [I-K] [J + k]) {flag = 1; break ;}}if (flag = 0) {If (startr> I + 1) {startr = I + 1; startc = J + 1;} else if (startr = I + 1 & startc> J + 1) {startr = I + 1; startc = J + 1 ;}}// for j} // For ireturn 0;} int main () {int I, j, Case, K, M, N; // freopen ("C: \ Users \ xiaosi \ Desktop \ acm.txt", "r", stdin); While (scanf ("% d ", & case )! = EOF) {While (case --) {scanf ("% d", & M, & N); // input character matrix for (I = 0; I <m; I ++) {scanf ("% s", temp); For (j = 0; j <n; j ++) {matrix [I] [J] = temp [J]; // convert it to lowercase if (Matrix [I] [J]> = 'A' & matrix [I] [J] <= 'Z ') {matrix [I] [J] = matrix [I] [J]-'A' + 'A' ;}} scanf ("% d", & K ); // string to be matched for (I = 0; I <K; I ++) {scanf ("% s", STR); int Len = strlen (STR ); // convert to lowercase for (j = 0; j <Len; j ++) {If (STR [J]> = 'A' & STR [J] <= 'Z ') {STR [J] = STR [J]-'A' + 'a'; }}// printf ("% s", STR); match (m, n, startr, startc); printf ("% d \ n", startr, startc);} // empty rows between each group of tests if (CASE) {printf ("\ n") ;}} 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.