// Where’s Waldorf? (序找单词)
// PC/UVa IDs: 110302/10010, Popularity: B, Success rate: average Level: 2
// Verdict: Accepted
// Submission Date: 2011-05-22
// UVa Run Time: 0.016s
//
// 版权所有(C)2011,邱秋。metaphysis # yeah dot net
//
// 使用数组来表示方向,在比较之前先判定长度,避免不可能的比较。
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
int abs(int a)
{
return a >= 0 ? a : (-1) * a;
}
bool equal(char a, char b)
{
return (a == b) || (abs(a - b) == 32);
}
void find(vector < string > & matrix, int nlines, int nrows, string word)
{
int length = word.length();
int bounds[8][2] = { {-1, 0}, {-1, -1}, {0, -1}, {1, -1}, {1, 0},
{1, 1}, {0, 1}, {-1, 1} };
int test[8];
for (int i = 0; i < nlines; i++)
for (int j = 0; j < nrows; j++)
if (equal(matrix[i][j], word[0]))
{
memset(test, 0, sizeof(test));
// 判定八个方向是否需要尝试匹配,若值为 1 表示需要尝试。
if ((j + 1) >= length)
test[0] = 1;
if ((j + 1) >= length && (i + 1) >= length)
test[1] = 1;
if ((i + 1) >= length)
test[2] = 1;
if ((nrows - j) >= length && (i + 1) >= length)
test[3] = 1;
if ((nrows - j) >= length)
test[4] = 1;
if ((nrows - j) >= length && (nlines - i) >= length)
test[5] = 1;
if ((nlines - i) >= length)
test[6] = 1;
if ((j + 1) >= length && (nlines - i) >= length)
test[7] = 1;
// 尝试匹配。
for (int m = 0; m < 8; m++)
if (test[m])
{
int directx = bounds[m][0];
int directy = bounds[m][1];
int posx = j;
int posy = i;
int start = 0;
bool matched = true;
while (start < length)
if (equal(matrix[posy][posx], word[start]))
{
start++;
posx += directx;
posy += directy;
}
else
{
matched = false;
break;
}
// 判断是否符合。
if (matched)
{
cout << (i + 1) << " " << (j + 1) << endl;
return;
}
}
}
}
int main(int ac, char *av[])
{
vector < string > matrix; // 字母矩阵。
int cases; // 测试数据组数。
int current = 0; // 当前数据组数。
int nlines; // 矩阵行数。
int nrows; // 矩阵列数。
int nwords; // 每组数据待查找的单词个数。
string line;
cin >> cases;
while (current < cases)
{
cin >> nlines >> nrows;
cin.ignore();
// 清空向量数组。
matrix.clear();
for (int i = 0; i < nlines; i++)
{
getline(cin, line);
matrix.push_back(line);
}
cin >> nwords;
cin.ignore();
for (int i = 0; i < nwords; i++)
{
getline(cin, line);
find(matrix, nlines, nrows, line);
}
current++;
if (current < cases)
cout << '\n';
}
return 0;
}
UVa Problem 10010 Where’s Waldorf?(寻找单词)
最新推荐文章于 2021-07-27 23:49:32 发布