10010 - Where's Waldorf?
Time limit: 3.000 seconds
Where's Waldorf?
Given a m by n grid of letters, ( ), and a list of words, find thelocation in the grid at which the word can be found. A word matchesa straight, uninterrupted line of letters in the grid. A word canmatch the letters in the grid regardless of case (i.e. upper andlower case letters are to be treated as the same). The matching canbe done in any of the eight directions either horizontally,vertically or diagonally through the grid.
Input
The input begins with a single positive integer on a line by itselfindicating the number of the cases following, each of them asdescribed below. This line is followed by a blank line, and thereis also a blank line between two consecutive inputs.
The input begins with a pair of integers, m followed byn,1<=m,n<=50, in decimal notation ona single line. The next m lines contain n letters each; this is thegrid of letters in which the words of the list must be found. Theletters in the grid may be in upper or lower case. Following thegrid of letters, another integer k appears on a line by itself (1<=k<=20). The next k lines of inputcontain the list of words to search for, one word per line. Thesewords may contain upper and lower case letters only (no spaces,hyphens or other non-alphabeticcharacters).
Output
For each test case, the output must follow the description below.The outputs of two consecutive cases will be separated by a blankline.
For each word in the word list, a pair of integers representing thelocation of the corresponding word in the grid must be output. Theintegers must be separated by a single space. The first integer isthe line in the grid where the first letter of the given word canbe found (1 represents the topmost line in the grid, and mrepresents the bottommost line). The second integer is the columnin the grid where the first letter of the given word can be found(1 represents the leftmost column in the grid, and n represents therightmost column in the grid). If a word can be found more thanonce in the grid, then the location which is output shouldcorrespond to the uppermost occurence of the word (i.e. theoccurence which places the first letter of the word closest to thetop of the grid). If two or more words are uppermost, the outputshould correspond to the leftmost of these occurences. All wordscan 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
从八个方向上找相同串 找到后输出坐标
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<map> #include<algorithm> using namespace std; char s[59][60],s1[100],s2[100]; int n,m; struct node { int x,y; } p; node Find(int len) { int flag=0; for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { if(s[i][j]==s1[0]) { int x=i,y=j,num=0; while(x>0&&s[x-1][y]==s1[num+1]) { num++; x--; } if(num==len-1) { p.x=i; p.y=j; return p; } x=i,y=j,num=0; while(x<n-1&&s[x+1][y]==s1[num+1]) { num++; x++; } if(num==len-1) { p.x=i; p.y=j; return p; } x=i,y=j,num=0; while(y>0&&s[x][y-1]==s1[num+1]) { num++; y--; } if(num==len-1) { p.x=i; p.y=j; return p; } x=i,y=j,num=0; while(y<m-1&&s[x][y+1]==s1[num+1]) { num++; y++; } if(num==len-1) { p.x=i; p.y=j; return p; } x=i,y=j,num=0; while(x>0&&y>0&&s[x-1][y-1]==s1[num+1]) { num++; y--; x--; } if(num==len-1) { p.x=i; p.y=j; return p; } x=i,y=j,num=0; while(x>0&&y<m-1&&s[x-1][y+1]==s1[num+1]) { num++; y++; x--; } if(num==len-1) { p.x=i; p.y=j; return p; } x=i,y=j-1,num=0; while(x<n-1&&y>0&&s[x+1][y-1]==s1[num+1]) { num++; y--; x++; } if(num==len-1) { p.x=i; p.y=j; return p; } x=i+1,y=j+1,num=1; while(x<n-1&&y<m-1&&s[x+1][y+1]==s1[num+1]) { num++; y++; x++; } if(num==len-1) { p.x=i; p.y=j; return p; } } } } } int main() { int T; while(~scanf("%d",&T)) { //int kk; while(T--) { scanf("%d%d",&n,&m); for(int i=0; i<n; i++) { scanf("%s",s[i]); for(int j=0; j<m; j++) //不i if(s[i][j]>='A'&&s[i][j]<='Z') //忘记了 s[i][j]=s[i][j]-'A'+'a'; //cout<<s[i]; } int k=0; scanf("%d",&k); for(int i=0; i<k; i++) { scanf("%s",s1); int len=strlen(s1); for(int j=0; j<len; j++) { if(s1[j]>='A'&&s1[j]<='Z') s1[j]=s1[j]-'A'+'a'; } p=Find(len); printf("%d %d\n",p.x+1,p.y+1); } if(T!=0) //两组之间有空行 cout<<endl; } } }