UVA10010 Where's Waldorf?

题目:

  Where's Waldorf? 

Given a m by n grid of letters, ( $1 \leq m,n \leq 20$), 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, $1 \leq
m,n \leq 50$ 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 ( $1 \leq k \leq 20$). 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

 

今天是让这个恶心的瓦尔多夫折腾惨了,没想到最简单的String部分的题就如此的恶心。意思大概是和找单词的游戏差不多。给一个字母表格,在里面找出下面输入的单词。我当初还想直接跳到数据结构做题,现在想起来实在是too young too simple。这道题用时估计超过2个小时了,WA了三次,中间查了题解。不过身为菜鸟的terry也学到了不少东西,在这里也做一下记录。

做这题的第一感觉就是让我想起了新秀杯现场赛的那道五子棋,当时编的代码及其繁琐。看了高人的代码之后学习了把搜索的八个方向存在两个数组里面,然后遍历整个字符串数组。这的确是一个很好的方法,减少了很多繁琐的步骤。其次就是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 }

 

posted @ 2012-12-04 17:39  上白泽慧音  阅读(339)  评论(0编辑  收藏  举报