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 integerk appears on a line by itself ( ). The nextk 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
这道题的意思就是 先输入一个字母的网络,M行N列。 然后输入K个单词。查找每个单词在字母网络中位置。要八个方向查找这个单词。
关于这题,我用了八个判定函数,分别判断八个方向上能否查找到这个单词,如果可以,则返回真。
首先将单词的首字母放到字母网络中查找,然后找到位置后,把位置传递给那八个函数,如果找到了就停止,否则找下一个首字母出现的位置。
这题WA了很几次,有些多细节没处理好,两组样例之间是要有空格的,最后一组不需要。而且代码有点太长了。
代码:
#include<iostream>
#include<string>
#include<stdio.h>
#include<cstring>
using namespace std;
char num[100][100];
char sear[100][100];
int n,m,k;
int leftup(int i, int j,int l){
int w;
i=i-1;
j=j-1;
for(w=1 ; w < strlen(sear[l]) ; w++,i--,j--)
{
if(i<0||j<0)
return false;
if(num[i][j]!=sear[l][w]&&num[i][j]!=sear[l][w]+32&&num[i][j]!=sear[l][w]-32)
return 0;
}
return 1;
}
int up(int i , int j , int l){
int w;
i=i-1;
for(w=1 ; w < strlen(sear[l]) ; w++,i--)
{
if(i<0||j)
return 0;
if(num[i][j]!=sear[l][w]&&num[i][j]!=sear[l][w]+32&&num[i][j]!=sear[l][w]-32)
return 0;
}
return 1;
}
int rightup(int i , int j, int l){
int w;
i=i-1;
j=j+1;
for(w=1 ; w < strlen(sear[l]) ; w++,i--,j++)
{
if(i<0||j>m-1)
return 0;
if(num[i][j]!=sear[l][w]&&num[i][j]!=sear[l][w]+32&&num[i][j]!=sear[l][w]-32)
return 0;
}
return 1;
}
int left(int i,int j ,int l){
int w;
j=j-1;
for(w=1 ; w < strlen(sear[l]) ; w++,j++)
{
if(j<0)
return 0;
if(num[i][j]!=sear[l][w]&&num[i][j]!=sear[l][w]+32&&num[i][j]!=sear[l][w]-32)
return 0;
}
return 1;
}
int right(int i,int j,int l){
int w;
j=j+1;
for(w=1 ; w < strlen(sear[l]) ; w++,j++)
{
if(j>m-1)
return 0;
if(num[i][j]!=sear[l][w]&&num[i][j]!=sear[l][w]+32&&num[i][j]!=sear[l][w]-32)
return 0;
}
return 1;
}
int leftdown(int i, int j, int l){
int w;
i=i+1;
j=j-1;
for(w=1 ; w < strlen(sear[l]) ; w++,j--,i++)
{
if(i>m-1||j<0)
return 0;
if(num[i][j]!=sear[l][w]&&num[i][j]!=sear[l][w]+32&&num[i][j]!=sear[l][w]-32)
return 0;
}
return 1;
}
int down(int i, int j ,int l){
int w;
i=i+1;
for(w=1 ; w < strlen(sear[l]) ; w++,i++)
{
if(i>m-1)
return 0;
if(num[i][j]!=sear[l][w]&&num[i][j]!=sear[l][w]+32&&num[i][j]!=sear[l][w]-32)
return 0;
}
return 1;
}
int rightdown(int i ,int j ,int l){
int w;
i=i+1;
j=j+1;
for(w=1 ; w < strlen(sear[l]) ; w++,i++,j++)
{
if(i>m-1)
return 0;
if(num[i][j]!=sear[l][w]&&num[i][j]!=sear[l][w]+32&&num[i][j]!=sear[l][w]-32)
return 0;
}
return 1;
}
int main(){
int i,j;
cin >> n >> m;
getchar();
for(i = 0; i < n ; i++)
{
cin >> num[i];
}
cin>>k;
for(i = 0 ; i < k ; i++)
{
cin>>sear[i];
}
int l,flag=0,p;
int r[8];
for(l=0;l<k;l++)
{ cout<<l<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(num[i][j]==sear[l][0]||num[i][j]==sear[l][0]+32||num[i][j]==sear[l][0]-32)
{ cout<<i<<" "<<j<<endl;
r[0] = leftup(i,j,l);
r[1] = up(i,j,l);
r[2] = rightup(i,j,l);
r[3] = left(i,j,l);
r[4] = right(i,j,l);
r[5] = leftdown(i,j,l);
r[6] = down(i,j,l);
r[7] = rightdown(i,j,l);
if(r[0]==1||r[1]==1||r[2]==1||r[3]==1||r[4]==1||r[5]==1||r[6]==1||r[7]==1)
flag=1;
}
if(flag==1)
break;
}
if(flag==1)
{
cout<<i<<" "<<j<<endl;
i=0;j=0;
flag=0;
break;
}
}
}
return 0;
}