日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

UVA10010 Where's Waldorf?

發(fā)布時間:2025/7/25 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 UVA10010 Where's Waldorf? 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題目:

? 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 Dagbert

Sample Output?

2 5 2 3 1 2 7 8

?

今天是讓這個惡心的瓦爾多夫折騰慘了,沒想到最簡單的String部分的題就如此的惡心。意思大概是和找單詞的游戲差不多。給一個字母表格,在里面找出下面輸入的單詞。我當初還想直接跳到數(shù)據(jù)結構做題,現(xiàn)在想起來實在是too young too simple。這道題用時估計超過2個小時了,WA了三次,中間查了題解。不過身為菜鳥的terry也學到了不少東西,在這里也做一下記錄。

做這題的第一感覺就是讓我想起了新秀杯現(xiàn)場賽的那道五子棋,當時編的代碼及其繁瑣。看了高人的代碼之后學習了把搜索的八個方向存在兩個數(shù)組里面,然后遍歷整個字符串數(shù)組。這的確是一個很好的方法,減少了很多繁瑣的步驟。其次就是fgets會存下末尾的換行符,用strlen的時候總是大1,這個把我折騰了好久,最后才查出這個錯誤。然后WA的原因就是如果有相同的位置,要最前面的那個。也就是說你搜索到了隱藏的字符串,就要立即return了。

在大牛看來應該是很水的題吧,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 }

?

轉載于:https://www.cnblogs.com/terryX/archive/2012/12/04/2801735.html

總結

以上是生活随笔為你收集整理的UVA10010 Where's Waldorf?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。