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
// 10010 - Where's Waldorf.cpp : 定义控制台应用程序的入口点。 // #include <iostream> #include<string> #include <cctype> #include <cstring> using namespace std; void out(char (*src)[52],int row,int col) { for(int i=0;i<row;i++){ for(int j=0;j<col;j++){ cout<<src[i][j]; } cout<<endl; } } void getin(char (*src)[52],int row,int col) { int r=0; string s; while(r<row){ cin>>s; for(int i=0;i<col;++i){ src[r][i]=toupper(s[i]); } ++r; } } bool L_R(char (*src)[52],int row,int col,int r,int start,char str[50]) {//from left to right to compare int len=strlen(str); if(col-start < len) return false; for(int i=0,j=start;i<len;++i,++j){ if(src[r][j]!=str[i]) return false; } return true; } bool R_L(char (*src)[52],int row,int col,int r,int start,char str[50]) {//right to left to compare int len=strlen(str); if(start+1 < len) return false; for(int i=0,j=start;i<len;++i,--j){ if(src[r][j]!=str[i]) return false; } return true; } bool U_D(char (*src)[52],int row,int col,int start,int c,char str[50]) {//from up to down to compare int len=strlen(str); if(row-start < len) return false; for(int i=0,j=start;i<len;++i,++j){ if(src[j][c]!=str[i]) return false; } return true; } bool D_U(char (*src)[52],int row,int col,int start,int c,char str[50]) {//from down to up to compare int len=strlen(str); if(start+1 < len) return false; for(int i=0,j=start;i<len;++i,--j){ if(src[j][c]!=str[i]) return false; } return true; } bool LU_RD(char (*src)[52],int row,int col,int row_start,int col_start,char str[50]) {//from left_up to right_down to compare int len=strlen(str); if((row-row_start < len ) || (col-col_start <len) ) return false; for(int i=0,r=row_start,c=col_start;i<len;++i,++r,++c){ if(src[r][c]!=str[i]) return false; } return true; } bool RD_LU(char (*src)[52],int row,int col,int row_start,int col_start,char str[50]) {//from right_down to left_up to compare int len=strlen(str); if((row_start+1 < len ) || (col_start+1 <len) ) return false; for(int i=0,r=row_start,c=col_start;i<len;++i,--r,--c){ if(src[r][c]!=str[i]) return false; } return true; } bool LD_RU(char (*src)[52],int row,int col,int row_start,int col_start,char str[50]) {//from left_down to right_up to compare int len=strlen(str); if((row_start+1 < len) || (col-col_start <len )) return false; for(int i=0,r=row_start,c=col_start;i<len;++i,--r,++c){ if(src[r][c]!=str[i]) return false; } return true; } bool RU_LD(char (*src)[52],int row,int col,int row_start,int col_start,char str[50]) {//from right_up to left_down to compare int len=strlen(str); if((row-row_start < len) || (col_start+1 <len) ) return false; for(int i=0,r=row_start,c=col_start;i<len;++i,++r,--c){ if(src[r][c]!=str[i]) return false; } return true; } int main() { int n; cin>>n; while(n>0){ char src[52][52]; int row,col; cin>>row>>col; getin(src,row,col); // out(src,row,col); int m; cin>>m; while(m--){ char str[500]; cin>>str; for(int i=0;i<strlen(str);++i){ str[i]=toupper(str[i]); } // cout<<"str="<<str<<" row="<<row<<" col="<<col<<endl; bool ok=false; int i,j; for(i=0;i<row;++i){ for(j=0;j<col;++j){ if(L_R(src,row,col,i,j,str)){ ok=true; break; }else if(R_L(src,row,col,i,j,str)){ ok=true; break; }else if(U_D(src,row,col,i,j,str)){ ok=true; break; }else if(D_U(src,row,col,i,j,str)){ ok=true; break; }else if(LU_RD(src,row,col,i,j,str)){ ok=true; break; }else if(RD_LU(src,row,col,i,j,str)){ ok=true; break; }else if(LD_RU(src,row,col,i,j,str)){ ok=true; break; }else if(RU_LD(src,row,col,i,j,str)){ ok=true; break; } } if(ok) break; } cout<<i+1<<" "<<j+1<<endl; } n-=1; if(n) cout<<endl; } return 0; } /* 2 8 11 abcDEFGhigg hEbkWalDork FtyAwaldORm FtsimrLqsrc byoArBeDeyv Klcbqwikomk strEBGadhrb yUiqlxcnBjf 4 Waldorf Bambi Betty Dagbert 8 11 abcDEFGhigg hEbkWalDork FtyAwaldORm FtsimrLqsrc byoArBeDeyv Klcbqwikomk strEBGadhrb yUiqlxcnBjf 4 Waldorf Bambi Betty Dagbert */