Background
Given an m by n grid of letters and a list of words, find the location in the grid at which the word can be found.
A word matches a straight, uninterrupted line of letters in the grid. A word can match the letters in the grid regardless of case (i.e., upper- and lowercase letters are to be treated as the same). The matching can be done in any of the eight horizontal, vertical, or diagonal directions through the grid.
Input
The input begins with a single positive integer on a line by itself indicating the number of cases, followed by a blank line. There is also a blank line between each two consecutive cases.
Each case begins with a pair of integers m followed by n on a single line, where 1 ≤ m, n ≤ 50 in decimal notation. The next m lines contain n letters each, representing the grid of letters where the words must be found. The letters in the grid may be in upper- or lowercase. Following the grid of letters, another integer k appears on a line by itself (1 ≤ k ≤ 20). The next k lines of input contain the list of words to search for, one word per line. These words may contain upper- and lowercase letters only – no spaces, hyphens, or other non-alphabetic characters.
Output
For each word in each test case, output a pair of integers representing its location in the corresponding grid. 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 output the location of the uppermost occurrence of the word (i.e., the occurrence which places the first letter of the word closest to the top of the grid). If two or more words are uppermost, output the leftmost of these occurrences. All words can be found at least once in the grid.
The output of two consecutive cases must be separated by a blank line.
Source
http://online-judge.uva.es/p/v100/10010.html 测试输入![]() | 期待的输出![]() | 时间限制![]() | 内存限制![]() | 额外进程![]() | |
---|---|---|---|---|---|
测试用例 1 | 以文本方式显示
| 以文本方式显示
| 1秒 | 64M | 0 |
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 100
char letters[N][N]; //字母矩阵
char word[N]; //待查找单词
int m,n,starti,startj; //m是矩阵行数,n是矩阵列数,starti、startj是单词入口点坐标
int Find(int x, int y) //根据传入的坐标增量值循环查找匹配
{
int d = 1;
for (int i = starti + x,j = startj + y; i < m && i >= 0 && j < n && j >= 0 && d < strlen(word); i += x, j += y )
{
char c = letters[i][j] < 97 ? letters[i][j] + 32 : letters[i][j];
if (c != word[d] && c != word[d] + 32)
break;
d++;
}
return d == strlen(word) ? 1 : 0;
}
int SearchWord() //根据入口点的位置遍历八个方位,寻找合适的匹配
{
int a[3] = {1, 0, -1},i,j; //这八个方位可以用一个数组表示 a[3] * a[3]
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)//依次搜索这8个位置
{ //starti和startj是入口点的下标
if (starti+a[i] < m && starti+a[i] >= 0 && startj+a[j] < n && startj+a[j] >= 0)
{
if (a[i] == 0 && a[j] == 0)
continue;
if (Find(a[i], a[j]))
return 1;
}
}
return 0;
}
void SearchEntry() //寻找入口点,即第一个字母的位置
{
int i,j;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
{
char c = letters[i][j] < 97 ? letters[i][j] + 32 : letters[i][j];
if (c == word[0] || c == word[0] + 32)
{
starti = i, startj = j;
if (SearchWord())
{
printf("%d %d\n", i+1, j+1);
return;
}
}
}
}
int main()
{
freopen("in.txt","r",stdin);
int k, i, h;
scanf("%d", &k);
while (k--)
{
getchar();
scanf("%d %d", &m, &n);
for (i = 0; i < m; i++)
scanf("%s",letters[i]);
scanf("%d", &h);
while (h--)
{
scanf("%s", word);
SearchEntry();
}
if (k) //测试系统的要求最后一行不能要回车,不同测试例结果之间要有空行
printf("\n");
}
return 0;
}