#include<iostream>
#include<cctype>
#include<algorithm>
using namespace std;
char a[55][55];
string s[22];
int T,m,n,k;
int mx[] = {1, 1, 1, 0, 0, -1, -1, -1};
int my[] = {1, 0, -1, 1, -1, 1, 0, -1};
bool in(int x,int y){
    return x>=1&&x<=m&&y>=1&&y<=n;
}
void getLocation(string s){
    int flag ;
    for(int i = 1; i <= m; i++){
        for(int j = 1; j <= n; j++){
            if(a[i][j] == tolower(s[0])){
                for(int k = 0; k < 8; k++){
                    flag = 1;
                    int t1 = i,t2 = j;
                    for(int p = 1; p < s.length(); p++){
                        t1 += mx[k];
                        t2 += my[k];
                        if(a[t1][t2] != tolower(s[p]) || !in(t1,t2))
                        {
                            flag = 0;
                            break;
                        }
                    }
                    if(flag){
                        cout << i << " " << j << endl;
                        return;
                    }
                }
            }
        }
    }
}
int main(){
    cin >> T;
    while(T--){
        cin >> m >> n;
        cin.ignore();
        for(int i = 1; i<= m; i++)
            for(int j = 1; j <= n; j++){
                cin >> a[i][j];
                a[i][j] = tolower(a[i][j]);
            }
        cin >> k;
        cin.ignore();
        for(int i = 0; i < k; i++)
            cin >> s[i];
        for(int i = 0; i < k; i++){
            getLocation(s[i]);
        }
        if(T>0)
            cout << endl;
    }
    return 0;
}
原文:http://www.cnblogs.com/yong-hua/p/4649966.html