因为数据量小 所以直接枚举所有情况 不会超时
#include<cstdio>
#include<cstring>
#include<cctype>
int dir_x[8]={-1,-1,-1,0,0,1,1,1};//定义方向增量
int dir_y[8]={-1,0,1,-1,1,-1,0,1};
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL
int N,m,n,k,X,Y;
scanf("%d",&N);
while(N--)
{
scanf("%d%d",&m,&n);
getchar();
char str[m+5][n+5];
int i,j;
for(i = 0; i < m ;i++)
gets(str[i]);
for(i = 0; i < m; i++)
for(j = 0; j < n; j++)
if(isalpha(str[i][j])) str[i][j] = toupper(str[i][j]);//小写转换成大写
scanf("%d",&k);
getchar();
char s[55];
for(i = 0; i < k ; i++)
{
scanf("%s",s);
int len_s = strlen(s);
for(j = 0; j < len_s; j++)
if(isalpha(s[j])) s[j] = toupper(s[j]);//小写转换成大写
int ok[8],flag = 0;
for(int x = 0; x < m && !flag; x++)
for(int y = 0; y < n && !flag; y++)//枚举每一个字母
{
if(str[x][y]!=s[0])continue;
for(int q = 0 ; q < 8; q++) ok[q] = 1;
for(int l = 0; l < 8; l++)//匹配每个方向的字母
{
for(j = 1; j < len_s; j++)
if(str[x+dir_x[l]*j][y+dir_y[l]*j]!=s[j])
{ok[l] = 0;break;}//当前方向不满足条件了就结束
}
for(j = 0; j < 8; j++)
if(ok[j]){flag = 1; X = x+1; Y = y+1; break;}//只要有一个ok到最后的值还是一就满足
}
printf("%d %d\n",X,Y);
}
if(N!=0)printf("\n");
}
return 0;
}