题目:

  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部分的题就如此的恶心。意思大概是和找单词的游戏差不多。给一个字母表格,在里面找出下面输入的单词。我当初还想直接跳到数据结构做题,现在想起来实在是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 }

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

UVA10010 Where's Waldorf?相关推荐

  1. UVA10010 Where's Waldorf?【水题】

    Given a m by n grid of letters, (1 ≤ m, n ≤ 50), and a list of words, find the location in the grid ...

  2. AOAPC I: Beginning Algorithm Contests 题解

    AOAPC I: Beginning Algorithm Contests 题解 AOAPC I: Beginning Algorithm Contests (Rujia Liu) - Virtual ...

  3. ICPC程序设计题解书籍系列之八:(美)斯基纳等:《挑战编程-程序设计竞赛训练手册》

    S书<挑战编程--程序设计竞赛训练手册>题目一览 1 Getting Started UVA100 POJ1207 HDU1032 The 3n + 1 problem[水题] - 海岛B ...

  4. (六十三)第四章复习题

    注:以下需要头文件的,一般都注明了头文件,但可能省略掉了int main(){}或者using namespace std;这样的代码,只写了关键代码. 1.如何声明下列数据? a.actor是由30 ...

  5. Part Ⅱ At the Restaurant 在饭店??

    Part Ⅱ At the Restaurant 在饭店 Lesson15 Restaurant Reservation 预订座位 Key Sentences(重点句子) I'd like to ma ...

  6. 有关Linux下的一些配置

    安装ubuntu的底层.server版本,全定制化简洁. 安装openbox: apt-get install openbox 面板使用tint2: apt-get install tint2 图形需 ...

  7. π-Algorithmist分类题目(3)

    原题网站:Algorithmist,http://www.algorithmist.com/index.php/Main_Page π-Algorithmist分类题目(3) Probability ...

  8. π-Algorithmist分类题目(2)

    原题网站:Algorithmist,http://www.algorithmist.com/index.php/Main_Page π-Algorithmist分类题目(2) Set Theory U ...

  9. 常见的USB VID

    常见的USB VID:(遇到再更新) 03f0 Hewlett-Packard 040a Kodak Co. 04b8 Seiko Epson Corp. 0eef D-WAV Scientific ...

最新文章

  1. java 去掉 时期中的图片,去除图片浅色背景(Java 实现)
  2. 009_TreeSet对实现了Comparable接口的对象排序
  3. 5.中文问题(自身,操作系统级别,应用软件的本身),mysql数据库备份
  4. 2006年上海交通大学计算机研究生机试真题
  5. 从基础到高级讲解Kafka
  6. Javascript如何显示完整的大数加法结果而不是科学计数法形式
  7. Ansible-playbook 拾遗
  8. unsigned int数相减时候的trick
  9. 修改win10注册表来控制cpu的最大运行频率,实现电脑降温
  10. IBM要推POWER9,来了解一下POWER处理器的前世今生
  11. 技术人的未来(三)——红海与蓝海
  12. weblogic12c补丁安装
  13. APP服务器与Web服务器的区别是什么?
  14. tensorflow打印模型图_tensorflow实现打印pb模型的所有节点
  15. LVGL_V8.2 时钟动画 (持续更新中)
  16. 神秘的程序员头像包 第二发
  17. 微信支付-企业付款到零钱问题集锦
  18. window下使用C++ Bonjour配置服务
  19. ASCII,UTF,GBK 是什么
  20. 运动时谁还不来个姨妈:一个结合大姨妈的运动打卡小程序

热门文章

  1. 关于android开发添加菜单XML文件之后无法在R.java中生成ID的问题
  2. C# 采用系统委托的方式处理线程内操作窗体控件(转载)
  3. [转]我们需要IQ吗?--敬以此文献给和我一样迷茫,浮躁的人,共勉!
  4. mysql 拷贝数据库 表存在却打不开_mysql数据库文件复制后表打不开
  5. 审车按月还是日期_新手都该知道的审车流程!
  6. leetcode算法题--骑士拨号器
  7. pacman安装php的位置,PacMan 01——地图的搭建
  8. python 安装nameerror_python NameError:name’file’未定义
  9. 软件详细设计说明书_互联网知识大全:软件开发中和各种开发软件文档的常见的英文缩写,还不快快收藏!...
  10. html打地鼠游戏设计报告,有趣的Axure案例:打地鼠游戏的设计