UVA10010 Where's Waldorf?
題目:
| ? 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.
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?
18 11 abcDEFGhigg hEbkWalDork FtyAwaldORm FtsimrLqsrc byoArBeDeyv Klcbqwikomk strEBGadhrb yUiqlxcnBjf 4 Waldorf Bambi Betty DagbertSample Output?
2 5 2 3 1 2 7 8?
今天是讓這個(gè)惡心的瓦爾多夫折騰慘了,沒(méi)想到最簡(jiǎn)單的String部分的題就如此的惡心。意思大概是和找單詞的游戲差不多。給一個(gè)字母表格,在里面找出下面輸入的單詞。我當(dāng)初還想直接跳到數(shù)據(jù)結(jié)構(gòu)做題,現(xiàn)在想起來(lái)實(shí)在是too young too simple。這道題用時(shí)估計(jì)超過(guò)2個(gè)小時(shí)了,WA了三次,中間查了題解。不過(guò)身為菜鳥(niǎo)的terry也學(xué)到了不少東西,在這里也做一下記錄。
做這題的第一感覺(jué)就是讓我想起了新秀杯現(xiàn)場(chǎng)賽的那道五子棋,當(dāng)時(shí)編的代碼及其繁瑣。看了高人的代碼之后學(xué)習(xí)了把搜索的八個(gè)方向存在兩個(gè)數(shù)組里面,然后遍歷整個(gè)字符串?dāng)?shù)組。這的確是一個(gè)很好的方法,減少了很多繁瑣的步驟。其次就是fgets會(huì)存下末尾的換行符,用strlen的時(shí)候總是大1,這個(gè)把我折騰了好久,最后才查出這個(gè)錯(cuò)誤。然后WA的原因就是如果有相同的位置,要最前面的那個(gè)。也就是說(shuō)你搜索到了隱藏的字符串,就要立即return了。
在大牛看來(lái)應(yīng)該是很水的題吧,terry還需努力!
代碼:
1 #include<stdio.h> 2 #include<string.h> 3 #include<ctype.h> 4 int x[9],y[9],m,n,p; 5 char s[50][50]; 6 char k[50]; 7 void fill() 8 { 9 x[1]= 1;y[1]= 0; 10 x[2]= 1;y[2]=-1; 11 x[3]= 0;y[3]=-1; 12 x[4]=-1;y[4]=-1; 13 x[5]=-1;y[5]= 0; 14 x[6]=-1;y[6]= 1; 15 x[7]= 0;y[7]= 1; 16 x[8]= 1;y[8]= 1; 17 } 18 void change() 19 { 20 int i,j; 21 for(i=0;i<m;i++) 22 { 23 for(j=0;j<n;j++) 24 { 25 s[i][j]=tolower(s[i][j]); 26 } 27 } 28 } 29 void input() 30 { 31 int i; 32 scanf("%d%d",&m,&n); 33 getchar(); 34 for(i=0;i<m;i++) 35 { 36 fgets(s[i],sizeof(s[i]),stdin); 37 } 38 } 39 int ok(int i,int j) 40 { 41 int l,q; 42 for(l=1;l<=8;l++) 43 { 44 int flag=1; 45 for(q=0;k[q]!='\0'&&k[q]!='\n';q++) 46 { 47 int xx=j+x[l]*q,yy=i+y[l]*q; 48 if(s[yy][xx]!=k[q]||xx==-1||xx==n||yy==-1||yy==m) 49 { 50 flag=0; 51 break; 52 } 53 } 54 if(flag==1) return 1; 55 } 56 return 0; 57 } 58 void find() 59 { 60 int i,j; 61 for(i=0;i<m;i++) 62 { 63 for(j=0;j<n;j++) 64 { 65 if(ok(i,j)) 66 { 67 printf("%d %d\n",i+1,j+1); 68 return; 69 } 70 } 71 } 72 } 73 void doit() 74 { 75 scanf("%d",&p);getchar(); 76 int i,j; 77 for(i=0;i<p;i++) 78 { 79 fgets(k,sizeof(k),stdin); 80 for(j=0;j<strlen(k);j++) 81 k[j]=tolower(k[j]); 82 find(); 83 } 84 } 85 int main() 86 { 87 fill(); 88 int t; 89 scanf("%d",&t); 90 while(t--) 91 { 92 input(); 93 change(); 94 doit(); 95 if(t!=0) printf("\n"); 96 } 97 return 0; 98 }?
轉(zhuǎn)載于:https://www.cnblogs.com/terryX/archive/2012/12/04/2801735.html
總結(jié)
以上是生活随笔為你收集整理的UVA10010 Where's Waldorf?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 转 OFBiz 菜单组件(menu-wi
- 下一篇: 12.04 scrum report