您的位置:首页 > 其它

ACM-uva10010

2013-10-20 19:31 363 查看
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: