File size: 1,257 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
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

string grid[51];

bool inside(int x, int y, int m, int n) {
	return x < m && x >= 0 && y < n && y >= 0;
}

bool search(const string & word, int x, int y, int pos, const int & incrx, const int & incry, const int & m, const int & n) {
	if(tolower(word[pos]) == tolower(grid[x][y])) {
		if (pos < word.length()-1) {
			int found = false;
			if (inside(x+incrx, y+incry, m, n)) found = search(word, x+incrx, y+incry, pos+1, incrx, incry, m, n);
			return found;
		} else {
			return true;
		}
	} else return false;
}

int main() {
	int cases;
	cin >> cases;
	while (cases--) {
		int m, n, k;
		cin >> m >> n;
		for (int i = 0; i < m; i++) cin >> grid[i];
		cin >> k;
		string word;
		for (int i = 0; i < k; i++) {
			cin >> word;
			bool found = false;
			for (int x = 0; x < m && !found; x++) {
				for (int y = 0; y < n && !found; y++) {
					for (int incrx = -1; incrx <= 1 && !found; incrx++) {
						for (int incry = -1; incry <= 1 && !found; incry++) {
							if (incrx == 0 && incry == 0) continue;
							found = search(word, x, y, 0, incrx, incry, m, n);
							if (found) printf("%d %d\n", x+1, y+1);
						}
					}
				}
			}
		}
		if (cases) printf("\n");
	}
	return 0;
}