#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int dir[8][2] = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};//代表相对一个位置的8各方向;
char str[55][55];//存储初始给定的字符集;
char word[25][80];//存储将被查询的字符集;
bool search(char *s, int p, int q)
{
int len = strlen(s);
int x, y;
if(s[0] != str[p][q])
return false;//先根据首字母的比较初步确定位置;
for(int i = 0; i < 8; i++)//8个方向依次尝试;
{
x = p; y = q;
bool flag = true;
for(int j = 1; j < len; j++)
{
x += dir[i][0];
y += dir[i][1];
if(str[x][y] != s[j])
{
flag = false;//在某一方向上,只要有一个字符不合适,这个方向就不符合条件,退出此方向上的循环;
break;
}
}
if(flag)
return true;
}
return false;
}
int main()
{
int t;
scanf("%d", &t);//数据组数;
int ans;
ans = t;
while(t--)
{
memset(str, 0, sizeof(str));
memset(word, 0, sizeof(word));
if(t != ans-1)
printf("\n");//输出格式需求;
int n, m;
scanf("%d%d", &m, &n);
for(int i = 0; i < m; i++)
{
scanf("%s", str[i]);
for(int j = 0; j < n; j++)
{
str[i][j] = tolower(str[i][j]);
}
}
int k;
scanf("%d", &k);
for(int i = 0; i < k; i++)
{
scanf("%s", word[i]);
int len = strlen(word[i]);
for(int j = 0; j < len; j++)
{
word[i][j] = tolower(word[i][j]);
}
}
for(int i = 0; i < k; i++)
{
bool flag = false;
for(int p = 0; p < m && !flag; p++)
for(int q = 0; q < n && !flag; q++)
{
if(search(word[i], p, q))
{
flag = true;
printf("%d %d\n", p+1, q+1);
}
}
}
}
}
此题解借鉴学习了网上其他人士的代码,个人认为代码量还比较适当。