Where's Waldorf? |
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).
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.
1 8 11 abcDEFGhigg hEbkWalDork FtyAwaldORm FtsimrLqsrc byoArBeDeyv Klcbqwikomk strEBGadhrb yUiqlxcnBjf 4 Waldorf Bambi Betty Dagbert
2 5 2 3 1 2 7 8
题目大意:
该题让在一个m*n的范围内找一个数组;从左上方开始横着遍历,然后下一行继续遍历。
查找八个方向是否有匹配的字符串出现。如果匹配则的左上为优先
因为方向没弄清楚,这题WA了十几次,最后终于爬出了这个坑。
#include <iostream> #include <stdio.h> #include <ctype.h> #include <string.h> using namespace std; int pos_m,pos_n; const int N = 51; char letter[N][N]; char word[N][N]; int m,n; void solve(int x) { char temp[N]; int len = strlen(word[x]); for(int i = 0; i < m; i++) { for(int j = 0; j < n; j++) { if(word[x][0] == letter[i][j]) { //ru memset(temp,0,sizeof(temp)); for(int k = 0; k < len; k++) { temp[k] = letter[i + k][j - k]; } if( !strcmp(temp,word[x]) ) { pos_m = i; pos_n = j; return; } //rd memset(temp,0,sizeof(temp)); for(int k = 0; k < len; k++) { temp[k] = letter[i + k][j + k]; } if( !strcmp(temp,word[x]) ) { pos_m = i; pos_n = j; return; } //lu memset(temp,0,sizeof(temp)); for(int k = 0; k < len; k++) { temp[k] = letter[i - k][j - k]; } if( !strcmp(temp,word[x]) ) { pos_m = i; pos_n = j; return; } //ld memset(temp,0,sizeof(temp)); for(int k = 0; k < len; k++) { temp[k] = letter[i - k][j + k]; } if( !strcmp(temp,word[x]) ) { pos_m = i; pos_n = j; return; } //u memset(temp,0,sizeof(temp)); for(int k = 0; k < len; k++) { temp[k] = letter[i][j - k]; } if( !strcmp(temp,word[x]) ) { pos_m = i; pos_n = j; return; } //d memset(temp,0,sizeof(temp)); for(int k = 0; k < len; k++) { temp[k] = letter[i][j + k]; } if( !strcmp(temp,word[x]) ) { pos_m = i; pos_n = j; return; } //l memset(temp,0,sizeof(temp)); for(int k = 0; k < len; k++) { temp[k] = letter[i - k][j]; } if( !strcmp(temp,word[x]) ) { pos_m = i; pos_n = j; return; } //r memset(temp,0,sizeof(temp)); for(int k = 0; k < len; k++) { temp[k] = letter[i + k][j]; } if( !strcmp(temp,word[x]) ) { pos_m = i; pos_n = j; return; } } } } pos_m = pos_n = 0; return; } int main() { int t; int len; scanf("%d",&t); while(t--) { scanf("%d%d",&m,&n); for(int i = 0; i < m; i++) { scanf("%s",letter[i]); len = strlen(letter[i]); for(int j = 0; j < len; j++) { letter[i][j] = tolower(letter[i][j]); } } int k; scanf("%d",&k); for(int i = 0;i < k; i++) { scanf("%s",word[i]); len = strlen(word[i]); for(int j=0;j < len; j++) { word[i][j] = tolower(word[i][j]); } } for(int i=0; i < k; i++) { solve(i); printf("%d %d\n",pos_m+1,pos_n+1); } if(t != 0) printf("\n"); } return 0; }