#include <iostream>
#include <string>
#include <string.h>
using namespace std;
#define MAXN 50 + 5
int temp[MAXN][MAXN];
int r, c;
int dir[8][2] =
{
0, -1, -1, 0, 0, 1, 1, 0, -1, -1, -1, 1, 1, 1, 1, -1
};
int ansr, ansc;
int change(char ch)
{
if (ch >= 'a' && ch <= 'z')
{
return ch - 'a';
}
else
{
return ch - 'A';
}
}
bool match(int x, int y, string &str)
{
int len = str.length();
int num = 0;
for (int i = 0; i < 8; i++)
{
num = 0;
for (int j = 0; j < len; j++)
{
int xt = x + j * dir[i][0], yt = y + j * dir[i][1];
if (xt >= 0 && xt < r && yt >= 0 && yt < c)
{
if (temp[xt][yt] == change(str[j]))
{
num++;
}
else
{
break;
}
}
else
{
break;
}
}
if (num == len)
{
return true;
}
}
return false;
}
void find(string &str)
{
bool ok = false;
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
if (temp[i][j] == change(str[0]))
{
if (match(i, j, str))
{
ansr = i + 1;
ansc = j + 1;
ok = true;
break;
}
}
}
if (ok)
{
break;
}
}
}
void input()
{
int t, k;
string str;
char ch;
cin >> t;
while (t--)
{
cin >> r >> c;
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
cin >> ch;
temp[i][j] = change(ch);
}
}
cin >> k;
for (int i = 0; i < k; i++)
{
cin >> str;
find(str);
cout << ansr << ' ' << ansc << endl;
}
if (t)
{
cout << endl;
}
}
}
int main()
{
input();
return 0;
}
ACM-uva10010
最新推荐文章于 2017-08-31 21:16:00 发布