博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVA10010 Where's Waldorf?
阅读量:4334 次
发布时间:2019-06-07

本文共 3410 字,大约阅读时间需要 11 分钟。

题目:

  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 

18 11abcDEFGhigghEbkWalDorkFtyAwaldORmFtsimrLqsrcbyoArBeDeyvKlcbqwikomkstrEBGadhrbyUiqlxcnBjf4WaldorfBambiBettyDagbert

Sample Output 

2 52 31 27 8

 

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

做这题的第一感觉就是让我想起了新秀杯现场赛的那道五子棋,当时编的代码及其繁琐。看了高人的代码之后学习了把搜索的八个方向存在两个数组里面,然后遍历整个字符串数组。这的确是一个很好的方法,减少了很多繁琐的步骤。其次就是fgets会存下末尾的换行符,用strlen的时候总是大1,这个把我折腾了好久,最后才查出这个错误。然后WA的原因就是如果有相同的位置,要最前面的那个。也就是说你搜索到了隐藏的字符串,就要立即return了。

在大牛看来应该是很水的题吧,terry还需努力!

代码:

1 #include
2 #include
3 #include
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

 

转载于:https://www.cnblogs.com/terryX/archive/2012/12/04/2801735.html

你可能感兴趣的文章
VMware黑屏解决方法
查看>>
JS中各种跳转解析
查看>>
JAVA 基础 / 第八课:面向对象 / JAVA类的方法与实例方法
查看>>
Ecust OJ
查看>>
P3384 【模板】树链剖分
查看>>
Thrift源码分析(二)-- 协议和编解码
查看>>
考勤系统之计算工作小时数
查看>>
4.1 分解条件式
查看>>
Equivalent Strings
查看>>
flume handler
查看>>
收藏其他博客园主写的代码,学习加自用。先表示感谢!!!
查看>>
H5 表单标签
查看>>
su 与 su - 区别
查看>>
C语言编程-9_4 字符统计
查看>>
在webconfig中写好连接后,在程序中如何调用?
查看>>
限制用户不能删除SharePoint列表中的条目(项目)
查看>>
feign调用spring clound eureka 注册中心服务
查看>>
ZT:Linux上安装JDK,最准确
查看>>
LimeJS指南3
查看>>
关于C++ const成员的一些细节
查看>>