File size: 1,886 Bytes
c4b0eef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#include <iostream>
#include <cstring>

using namespace std;

int m, n;
const int dir[][2] = {{1,0},{-1,0},{0,-1},{0,1},{1,1},{1,-1},{-1,-1},{-1,1}};
char wordSoup[60][60];
bool found;

void toLower(char word[]){
    for(int i = 0; word[i] > 0; i++){
        if (word[i] >= 'A' && word[i] <= 'Z') word[i] += 32;
    }
}

void foundWord(int i, int j, char wordArray[60]){
    for(int k = 0; k < 8; k++){
        int ni = i;
        int nj = j;
        int waI = 0;
        while(wordSoup[ni][nj] == wordArray[waI]){
            ni += dir[k][0];
            nj += dir[k][1];
            waI++;
            if(wordArray[waI] == '\0'){
                found = true;
                return;
            }
            if (ni < 0 || ni >= m || nj < 0 || nj >= n) break;
        }
    }
}

int main(){
    int cases, words;
    cin >> cases;
    while(cases-- > 0){
        cin >> m >> n;
        memset(wordSoup, ' ', sizeof(wordSoup[0][0])*60*60);
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++) cin >> wordSoup[i][j];
            toLower(wordSoup[i]);
        }
        cin >> words;
        char wordArray[60];
        cin.getline(wordArray, 60);
        for(int k = 0; k < words; k++){
            cin.getline(wordArray,60);
            toLower(wordArray);
            found = false;
            for(int i = 0; i < m; i++){
                for(int j = 0; j < n; j++){
                    if(wordSoup[i][j] == wordArray[0]){
                        foundWord(i, j, wordArray);
                        if(found){ 
                          cout << (i + 1) << " " << (j + 1) << endl;
                          break;
                        }
                    }
                }
                if(found) break;
            }
        }
        if(cases != 0) cout << endl;
    }
    return 0;
}