Input
The input begins with a single positive integer on a line by itselfindicating the number of the cases following, each of them asdescribed below. This line is followed by a blank line, and thereis also a blank line between two consecutive inputs.
The input begins with a pair of integers, m followed byn,1<=m,n<=50, in decimal notation ona single line. The next m lines contain n letters each; this is thegrid of letters in which the words of the list must be found. Theletters in the grid may be in upper or lower case. Following thegrid of letters, another integer k appears on a line by itself (1<=k<=20). The next k lines of inputcontain the list of words to search for, one word per line. Thesewords may contain upper and lower case letters only (no spaces,hyphens or other non-alphabeticcharacters).
Output
For each test case, the output must follow the description below.The outputs of two consecutive cases will be separated by a blankline.
For each word in the word list, a pair of integers representing thelocation of the corresponding word in the grid must be output. Theintegers must be separated by a single space. The first integer isthe line in the grid where the first letter of the given word canbe found (1 represents the topmost line in the grid, and mrepresents the bottommost line). The second integer is the columnin the grid where the first letter of the given word can be found(1 represents the leftmost column in the grid, and n represents therightmost column in the grid). If a word can be found more thanonce in the grid, then the location which is output shouldcorrespond to the uppermost occurence of the word (i.e. theoccurence which places the first letter of the word closest to thetop of the grid). If two or more words are uppermost, the outputshould correspond to the leftmost of these occurences. All wordscan be found at least once in the grid.
Sample Input
1
8 11
abcDEFGhigg
hEbkWalDork
FtyAwaldORm
FtsimrLqsrc
byoArBeDeyv
Klcbqwikomk
strEBGadhrb
yUiqlxcnBjf
4
Waldorf
Bambi
Betty
Dagbert
Sample Output
2 5
2 3
1 2
7 8
代码
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define N 60
int search(char list[][N], char word[], int row, int col, int m, int n, int len)
{
int i, j, k;
for (i = row, k = 0; i >= 0 && k < len && list[i][col] == word[k]; --i, ++k) ;
if (k == len) return 1;
for (i = row, k = 0; i < m && k < len && list[i][col] == word[k]; ++i, ++k) ;
if (k == len) return 1;
for (j = col, k = 0; j >= 0 && k < len && list[row][j] == word[k]; --j, ++k) ;
if (k == len) return 1;
for (j = col, k = 0; j < n && k < len && list[row][j] == word[k]; ++j, ++k) ;
if (k == len) return 1;
for (i = row, j = col, k = 0; i >= 0 && j >= 0 && k < len && list[i][j]
== word[k]; i--, j--, ++k) ;
if (k == len) return 1;
if (k == len) return 1;
for (i = row, j = col, k = 0; i >= 0 && j < n && k < len && list[i][j] == word[k]; i--, j++, ++k) ;
if (k == len) return 1;
for (i = row, j = col, k = 0; i < m && j >= 0 && k < len && list[i][j] == word[k]; i++, j--, ++k) ;
if (k == len) return 1;
for (i = row, j = col, k = 0; i < m && j < n && k < len && list[i][j] == word[k]; i++, j++, ++k) ;
return k == len;
}
int main()
{
char list[N][N], words[N][N];
int i, j, k, z, m, n, cases;;
scanf("%d", &cases);
while (cases--) {
int found[N] = {0};
scanf("%d%d", &m, &n);
for (i = 0; i < m; ++i)
scanf("%s", list[i]);
for (i = 0; i < m; ++i)
for (j = 0; j < n; ++j)
list[i][j] = toupper(list[i][j]);
scanf("%d", &k);
for (i = 0; i < k; ++i)
scanf("%s", words[i]);
for (i = 0; i < k; ++i)
for (j = 0; j < strlen(words[i]); ++j)
words[i][j] = toupper(words[i][j]);
for (z = 0; z < k; ++z)
for (i = 0; i < m; ++i)
for (j = 0; j < n; ++j)
if (!found[z] && search(list, words[z], i, j, m, n, strlen(words[z]))) {
printf("%d %d\n", i+1, j+1);
found[z] = 1;
}
if (cases)
printf("\n");
}
return 0;
}