10010 - 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

 


Miguel Revilla
2000-08-22
  

ACM UVaOJ 10010 Where Waldorf RE 求解决方法。

04-24

UVaOJ 10010 Where Waldorfrn多次AC不了,不知道为什么会RE了,应该如何修改才好呢?rnrn/**rn * 题目分析:(从八个方向找出匹配的字符串)rn * 1.先输入一个测试用例的数目,然后输入测试数组的行,列可看成是构成了一个二维字符数组,rn * 接下来输入准备输入单词的数目,然后,分别输入各个单词。rn * 2.要求,输入的这些单词能分别从数组中的单词首字母所在行列中找出来,rn *  这个单词在字符数组中可能是从东,南,西,北,东南,西南,东北,西北八个方向的其中一个顺序读取。rn * 3.输入该单词对应二维数组中的首元素的地址。rn *rn * 下面程序直接暴力了,按照题意,从每个方向与输入的单词作对比。rn **/rnrn[code=C/C++]// 10010_WhereWaldorf.cpprn/**rn * rn * Sample Input rn * 1rn * rn * 8 11rn * abcDEFGhiggrn * hEbkWalDorkrn * FtyAwaldORmrn * FtsimrLqsrcrn * byoArBeDeyvrn * Klcbqwikomkrn * strEBGadhrbrn * yUiqlxcnBjfrn * 4rn * Waldorfrn * Bambirn * Bettyrn * Dagbertrn * rn * Sample Output rn * 2 5rn * 2 3rn * 1 2rn * 7 8rn **/rnrn/**rn * 题目分析:(从八个方向找出匹配的字符串)rn * 1.先输入一个测试用例的数目,然后输入测试数组的行,列可看成是构成了一个二维字符数组,rn * 接下来输入准备输入单词的数目,然后,分别输入各个单词。rn * 2.要求,输入的这些单词能分别从数组中的单词首字母所在行列中找出来,rn *  这个单词在字符数组中可能是从东,南,西,北,东南,西南,东北,西北八个方向的其中一个顺序读取。rn * 3.输入该单词对应二维数组中的首元素的地址。rn *rn * 下面程序直接暴力了,按照题意,从每个方向与输入的单词作对比。rn **/rnrn// RE rutime errorrnrn#include rn#include rn#include rn#include rnrn#include rnusing namespace std;rnrnconst int maxRow = 50;rnconst int maxColumn = 50;rnchar testGroup[maxRow + 10][maxColumn + 10];rnrnconst int wordMaxNum = 20;rn// 一个单词的最长长度可以计算矩形的对角线, sqrt(50*sqrt(2))会小于100rnconst int wordMaxLong = 100;rnchar testWordGroup[wordMaxNum + 5][wordMaxLong];rnrnint main()rnrn // freopen("test.in", "r", stdin);rn int row, column;rn scanf("%d%d", &row, &column);rn char cTemp;rn for (int i = 0; i < row; ++i)rn for (int j = 0; j < column; ++j)rn cin >> cTemp;rn testGroup[i][j] = tolower(cTemp); // 全部转换为小写rn rn rnrn int wordNum; // 输入的会小于20rn scanf("%d", &wordNum);rn for (int i = 0; i < wordNum; ++i)rn scanf("%s", &(testWordGroup[i][0]));rn for (int j = 0; j < strlen(*(testWordGroup + i)); ++j)rn testWordGroup[i][j] = tolower(testWordGroup[i][j]);rn rn rnrn // 1. 遍历数组找到相同的首字母rn // 2. 从这个首字母开始,向八个方向散开比较东南西北。。。rnrn int result(const int &, const int&, const int&, const int&);rn for(int wi = 0; wi < wordNum; ++wi) // 单个单词rn for (int tgi = 0; tgi < row; ++tgi) // 遍历数组rn for (int tgj = 0; tgj < column; ++tgj)rn if (testWordGroup[wi][0] == testGroup[tgi][tgj]) // 能找到原点位置rn int wordLong = strlen(*(testWordGroup + wi)); // 计算单个单词的长度rn if (result(wi, tgi, tgj, wordLong))rn tgj = column; // 结束行rn tgi = row; // 结束列rn rn rn rn rnrn rn return 0;rnrnrnint result(const int & wi, const int& tgi, const int& tgj, const int& wL)rnrn int count = 0;rn// 1.westrn for(int k = 0, j = tgj; k < wL && j >= 0; --j, ++k)rn if(testWordGroup[wi][k] == testGroup[tgi][j])rn ++count;rn rn elsern break;rn rn rn // west end and judge if it can be the resultrn if (wL == count)rn printf("%d %d\n", tgi + 1, tgj + 1);rn return 1;rn rn elsern count = 0; // 归零,换方向,再继续计算rn rn// 2.north-westrn for(int k = 0, i = tgi, j = tgj; k < wL && i >= 0 && j >= 0; --i, --j, ++k)rn if(testWordGroup[wi][k] == testGroup[i][j])rn ++count;rn rn elsern break;rn rn rn // north-west end and judge if it can be the resultrn if (wL == count)rn printf("%d %d\n", tgi + 1, tgj + 1);rn return 1;rn rn elsern count = 0; // 归零,换方向,再继续计算rn rn// 3.northrn for(int k = 0, i = tgi; k < wL && i >= 0; --i, ++k)rn if(testWordGroup[wi][k] == testGroup[i][tgj])rn ++count;rn rn elsern break;rn rn rn // north end and judge if it can be the resultrn if (wL == count)rn printf("%d %d\n", tgi + 1, tgj + 1);rn return 1;rn rn elsern count = 0; // 归零,换方向,再继续计算rn rn// 4. north-eastrn for(int k = 0, i = tgi, j = tgj; k < wL && i >= 0 && j < maxRow; --i, ++j, ++k)rn if(testWordGroup[wi][k] == testGroup[i][j])rn ++count;rn rn elsern break;rn rn rn // east end and judge if it can be the resultrn if (wL == count)rn printf("%d %d\n", tgi + 1, tgj + 1);rn return 1;rn rn elsern count = 0; // 归零,换方向,再继续计算rn rn// 5.eastrn for(int k = 0, j = tgj; k < wL && j < maxRow; ++j, ++k)rn if(testWordGroup[wi][k] == testGroup[tgi][j])rn ++count;rn rn elsern break;rn rn rn // east end and judge if it can be the resultrn if (wL == count)rn printf("%d %d\n", tgi + 1, tgj + 1);rn return 1;rn rn elsern count = 0; // 归零,换方向,再继续计算rn rn// 6.south-eastrn for(int k = 0, i = tgi, j = tgj; k < wL && j < maxRow && i < maxColumn; ++j, ++i, ++k)rn if(testWordGroup[wi][k] == testGroup[i][j])rn ++count;rn rn elsern break;rn rn rn // south-east end and judge if it can be the resultrn if (wL == count)rn printf("%d %d\n", tgi + 1, tgj + 1);rn return 1;rn rn elsern count = 0; // 归零,换方向,再继续计算rn rn// 7.southrn for (int k = 0, i = tgi; k < wL && i < maxRow; ++i, ++k)rn if(testWordGroup[wi][k] == testGroup[i][tgj])rn ++count;rn rn elsern break;rn rn rn // south end and judge if it can be the resultrn if (wL == count)rn printf("%d %d\n", tgi + 1, tgj + 1);rn return 1;rn rn elsern count = 0; // 归零,换方向,再继续计算rn rn// 8.south-westrn for (int k = 0, i = tgi, j = tgj; k < wL && i < maxRow && j >= 0; --j, ++i, ++k)rn if(testWordGroup[wi][k] == testGroup[i][j])rn ++count;rn rn elsern break;rn rn rn // south-west end and judge if it can be the resultrn if (wL == count)rn printf("%d %d\n", tgi + 1, tgj + 1);rn return 1;rn rn elsern count = 0; // 归零,换方向,再继续计算rn rn // 8 directions judge endrn return 0;rnrn[/code]

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试

关闭