題目鏈接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=951
Where's Waldorf? |
Input
The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.The input begins with a pair of integers, m followed by n, in decimal notation on a single line. The next m lines contain n letters each; this is the grid of letters in which the words of the list must be found. The letters in the grid may be in upper or lower case. Following the grid of letters, another integer k appears on a line by itself ( ). The next k lines of input contain the list of words to search for, one word per line. These words may contain upper and lower case letters only (no spaces, hyphens or other non-alphabetic characters).
Output
For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.For each word in the word list, a pair of integers representing the location of the corresponding word in the grid must be output. The integers must be separated by a single space. The first integer is the line in the grid where the first letter of the given word can be found (1 represents the topmost line in the grid, and m represents the bottommost line). The second integer is the column in the grid where the first letter of the given word can be found (1 represents the leftmost column in the grid, and n represents the rightmost column in the grid). If a word can be found more than once in the grid, then the location which is output should correspond to the uppermost occurence of the word (i.e. the occurence which places the first letter of the word closest to the top of the grid). If two or more words are uppermost, the output should correspond to the leftmost of these occurences. All words can be found at least once in the grid.
Sample Input
1 8 11 abcDEFGhigg hEbkWalDork FtyAwaldORm FtsimrLqsrc byoArBeDeyv Klcbqwikomk strEBGadhrb yUiqlxcnBjf 4 Waldorf Bambi Betty Dagbert
Sample Output
2 5 2 3 1 2 7 8
/*
*使用8方向搜索的模版
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
using namespace std;
const int MAX = 55;
char grid[MAX][MAX];
int m, n, mcount, ncount, len;
string words;
bool match(int x, int y)
{
int i = mcount, j = ncount, k = 0;//初始化
while(1)
{
if(i < 0|| i == m || j < 0 || j == n || k == len)
return false;
else if(grid[i][j] != words[k])
return false;
i = i + x, j = j + y, k = k + 1;
if(k == len) return true;
}
}
void direction_search()
{
for(mcount = 0; mcount != m; mcount++)
for(ncount = 0; ncount != n; ncount++)
for(int x = -1; x <= 1; ++x)
for(int y = -1; y <= 1; ++y)
if((x || y) && match(x, y)) return;
// x == 0 && y == 0,表示沒有向什麼方向搜索
}
int main()
{
#ifdef LOCAL
freopen("f:\\input.txt", "r", stdin);
//freopen("f:\\output.txt", "w", stdout);
#endif
int num; //測試組數
cin >> num;
while(num--)
{
char c;
int k;
cin >> m >> n;
for(int i = 0; i != m; i++)
{
for(int j = 0; j != n; j++)
{
cin >> c;
c = tolower(c);
grid[i][j] = c;
}
}
cin >> k;
getchar();
while(k--)
{
getline(cin, words);
len = words.size();
for(int i = 0; i < len; ++i)
words[i] = tolower(words[i]);
direction_search();
cout << mcount + 1 << " " << ncount + 1<< endl;
}
if(num) cout << endl;
}
return 0;
}