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 shocould 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

This question has been WA several times. At the beginning, I did not pay attention to case-insensitive data, but I could get the correct answer (my data is in lower case ). Later, I found out that after modification. The second time, we did not find that the subscript of this question started from 1. After modification, we still changed WA. After reading other code, we realized that we had to be free for the second test. Pay attention to the format later...The code for this question is messy.
#include <stdio.h>#include <string.h>#define MAXN100char grid[MAXN][MAXN];char word[MAXN][MAXN];int foo(int x, int y, char word[], int m, int n){int i, j, k;int len;int flag;len = strlen(word);if (len + y < n) {/* left-right */flag = 1;for (i=0; i<len; ++i) if (word[i] != grid[x][y+i]) {flag = 0;break;} if (flag)return 1;}if (y - len >= 0) {/* right-left */flag = 1;for (i=0; i<len; ++i)if (word[i] != grid[x][y-i]) {flag = 0;break;}if (flag)return 1;}if (x - len >= 0) {/* down-up */flag = 1;for (i=0; i<len; ++i)if (word[i] != grid[x-i][y]) {flag = 0;break;}if (flag)return 1;}if (x + len < m) {/* up-down */flag = 1;for (i=0; i<len; ++i)if (word[i] != grid[x+i][y]) {flag = 0;break;}if (flag)return 1;}if (y-len+1>=0 && x-len+1>=0) {/* left-up */flag = 1;for (i=0; i<len; ++i)if (word[i] != grid[x-i][y-i]) {flag = 0;break;}if (flag)return 1;}if (y+len-1<n && x-len+1>=0) {/* right-up */flag = 1;for (i=0; i<len; ++i)if (word[i] != grid[x-i][y+i]) {flag = 0;break;}if (flag)return 1;}if (x+len-1<m && y-len+1>=0) {/* left-down */flag = 1;for (i=0; i<len; ++i)if (word[i] != grid[x+i][y-i]) {flag = 0;break;}if (flag)return 1;}if (x+len-1<m && y+len-1<n) {/* right-down */flag = 1;for (i=0; i<len; ++i)if (word[i] != grid[x+i][y+i]) {flag = 0;break;}if (flag)return 1;}return 0;}int main(void){int count, nword;int m, n;int i, j, k;int flag;scanf("%d", &count);while (count--) {scanf("%d%d", &m, &n);for (i=0; i<m; ++i)scanf("%s", grid[i]);scanf("%d", &nword);for (i=0; i<nword; ++i)scanf("%s", word[i]);for (i=0; i<m; ++i)for (j=0; j<n; ++j)if (isupper(grid[i][j]))grid[i][j] += 32;for (i=0; i<nword; ++i)for (j=0; j<strlen(word[i]); ++j)if (isupper(word[i][j]))word[i][j] += 32;for (k=0; k<nword; ++k) {flag = 0;for (i=0; i<m; ++i) {for (j=0; j<n; ++j) {if (grid[i][j] == word[k][0]) flag = foo(i, j, word[k], m, n);if (flag) {printf("%d %d\n", i+1, j+1);goto loop;} elsecontinue;}}loop:;}if (count)putchar('\n');}}

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.