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, 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
  
  
  
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include<stdio.h>
#include<string.h>
char s1[100][100];
char s2[100];
int xunzhao(char st1[][100], char st2[], int x, int y, int z, int b, int c);
int main()
{
    int t, m, n, i, j, x, k, i1, i2, flag, len, h;
    scanf("%d", &t);
    for(i = 0; i < t; i++)
    {
        scanf("%d%d ", &m, &n);
        for(j = 0; j < m; j++)
        {
            scanf("%s", s1[j]);
            for(k = 0; k < n; k++)
            {
                if(s1[j][k] >= 'A' && s1[j][k] <= 'Z')
                    s1[j][k] = s1[j][k] + 32;
            }
        }
        scanf("%d ", &x);
        for(j = 0; j < x; j++)
        {
            scanf("%s", s2);
            len = strlen(s2);
            for(k = 0; k < len; k++)
            {
                if(s2[k] >= 'A' && s2[k] <= 'Z')
                    s2[k] = s2[k] + 32;
            }
            for(i1 = 0; i1 < m; i1++)
            {
                h = 0;
                for(i2 = 0; i2 < n; i2++)
                {
                    if(s1[i1][i2] == s2[0])
                    {
                        flag = xunzhao(s1,s2,i1,i2,len, m, n);
                        if(flag)
                        {
                            printf("%d %d\n", i1+1, i2+1);
                            h = 1;
                            break;
                        }
                    }
                }
                if(h)
                    break;
            }
        }
        if(i != t - 1)
                printf("\n");
    }
    return 0;
}
int xunzhao( char st1[][100], char st2[], int x, int y, int z, int b, int c)
{
    int k0, a;
    if(c-y>=z)
    {
        a = 1;
        for(k0 = 1; k0 < z; k0++)
        {
            if(s1[x][y+k0] != st2[k0])/*右*/
            {
                a = 0;
                break;
            }
        }
        if(a)
            return 1;
    }
    if(c-y>=z&&b-x>=z)
    {
        a = 1;
        for(k0 = 1; k0 < z; k0++)
        {
            if(st1[x+k0][y+k0] != st2[k0])/*右下*/
            {
                a = 0;
                break;
            }
        }
        if(a)
            return 1;
    }
    if(b-x>=z)
    {
        a = 1;
        for(k0 = 1; k0 < z; k0++)
        {
            if(st1[x+k0][y] != st2[k0])/*下*/
            {
                a = 0;
                break;
            }
        }
        if(a)
            return 1;
    }
    if(b-x>=z&&y+1>=z)
    {
        a = 1;
        for(k0 = 1; k0 < z; k0++)
        {
            if(st1[x+k0][y-k0]!=st2[k0])/*左下*/
            {
                a = 0;
                break;
            }
        }
        if(a)
            return 1;
    }
    if(y+1>=z)
    {
        a = 1;
        for(k0 = 1; k0 < z; k0++)
        {
            if(st1[x][y-k0]!=st2[k0])/*左*/
            {
                a = 0;
                break;
            }
        }
        if(a)
            return 1;
    }
    if(x+1>=z&&y+1>=z)
    {
        a = 1;
        for(k0 = 1; k0 < z; k0++)
        {
            if(st1[x-k0][y-k0]!=st2[k0])/*左上*/
            {
                a = 0;
                break;
            }
        }
        if(a)
            return 1;
    }
    if(x+1>=z)
    {
        a = 1;
        for(k0 = 1; k0 < z; k0++)
        {
            if(st1[x-k0][y]!=st2[k0])/*上*/
            {
                a = 0;
                break;
            }
        }
        if(a)
            return 1;
    }
    if(x+1>=z&&c-y>=z)
    {
        a = 1;
        for(k0 = 1; k0 < z; k0++)
        {
            if(st1[x-k0][y+k0]!=st2[k0])/*右上*/
            {
                a = 0;
                break;
            }
        }
        if(a)
            return 1;
    }
    return 0;
}


你可能感兴趣的:(编程,C语言)