Description

Where's Waldorf?
Given a
m by
n grid of letters, (
Where's Waldorf? |

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, m followed by n, in decimal notation on a single line. The next m lines contain nletters 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 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 may contain 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, and m represents 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, 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 is output should 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 should correspond to the leftmost of these occurences. All words can 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
Miguel Revilla
2000-08-22
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
using namespace std;
char map[501][501];
int n,m,K;
char a[1005];
int jx[] = {0,0,1,-1,1,-1,1,-1};
int jy[] = {1,-1,0,0,1,-1,-1,1};
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=0; i<n; i++)
{
scanf("%s",map[i]);
for(int j=0; j<m; j++)
{
if(map[i][j]>='A' && map[i][j]<='Z')
{
map[i][j] = map[i][j]+32;
}
}
}
scanf("%d",&K);
while(K--)
{
scanf("%s",a);
for(int i=0; a[i]!='\0'; i++)
{
if(a[i]>='A' && a[i]<='Z')
{
a[i] = a[i] + 32;
}
}
int len = strlen(a);
int p = 0;
int ff = 0;
int pi = 0;
int pj = 0;
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
if(a[0] == map[i][j])
{
pi = i;
pj = j;
//printf("pi = %d pj = %d\n",pi+1,pj+1);
int ii = i;
int jj = j;
for(int k=0; k<8; k++)
{
ii = pi + jx[k];
jj = pj + jy[k];
p = 1;
if(p == len)
{
ff = 1;
break;
}
if(ii<0 || jj<0 || ii>=n || jj>=m)
{
continue;
}
while(p<len)
{
if(a[p] == map[ii][jj])
{
//printf("a[%d] = %c map[%d][%d] = %c\n",p,a[p],ii,jj,map[ii][jj]);
ii = ii + jx[k];
jj = jj + jy[k];
p++;
if(p == len)
{
ff = 1;
}
if(ii<0 || jj<0 || ii>=n || jj>=m)
{
break;
}
}
else
{
break;
}
}
if(p == len)
{
ff = 1;
break;
}
}
}
if(ff == 1)
{
break;
}
}
if(ff == 1)
{
break;
}
}
printf("%d %d\n",pi+1,pj+1);
}
if(T!=0)
{
printf("\n");
}
}
return 0;
}