Where's Waldorf?
Given a
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 lower case letters are to be treated as the same). The matching can be done in any of the eight directions either horizontally, vertically or diagonally through the grid.
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
Miguel Revilla
2000-08-22
这题大意就是在一个字符的矩阵中查找一串字符串,不分大小写,主要方法也就是遍历各个位置,对于每个位置沿着它的八个方向查找目的字符即可。
#include <iostream>
#include <cstring>
#include <cctype>
#include <cstdio>
using namespace std;
const int dx[8] = {0,1,1,1,0,-1,-1,-1};
const int dy[8] = {1,1,0,-1,-1,-1,0,1};
const int N = 60;
char str[N][N];
char tmp[N];
bool judge(char (*str)[N],char *tmp,int x,int y,int pos,int len,int k);
void position(char (*str)[N],char *tmp,const int &row,const int &col) {
int len = strlen(tmp);
int pos = 0;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
int pos = 0;
if (tmp[pos] == str[i][j]) {
pos++;
if (pos == len) {
cout << i+1 << " " << j+1 << endl;
return;
}
for (int k = 0; k < 8; k++) {
if (tmp[pos] == str[i+dx[k]][j+dy[k]]) {
if (judge(str,tmp,i+dx[k],j+dy[k],pos+1,len,k)) {
cout << i+1 << " " << j+1 << endl;
return;
}
}
}
}
}
}
}
bool judge(char (*str)[N],char *tmp,int x,int y,int pos,int len,int k) {
if (pos == len) {
return true;
}
else if (tmp[pos] == str[x+dx[k]][y+dy[k]]) {
pos++;
return judge(str,tmp,x+dx[k],y+dy[k],pos,len,k);
}
else {
return false;
}
}
int main() {
int Case;
cin >> Case;
while (Case--) {
int row;
int col;
cin >> row;
cin >> col;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
cin >> str[i][j];
if (isupper(str[i][j])) {
str[i][j] = tolower(str[i][j]);
}
}
}
int Test;
cin >> Test;
getchar();
for (int i = 0; i < Test; i++) {
gets(tmp);
for (int j = 0; tmp[j] && j < N; j++) {
if (isupper(tmp[j])) {
tmp[j] = tolower(tmp[j]);
}
}
position(str,tmp,row,col);
memset(tmp,0,sizeof(tmp));
}
if (Case)
cout << endl;
}
return 0;
}