UVA 10010
2015-07-20 20:19
525 查看
Given a m by n grid of letters, ( ), and a list of words, find thelocation in the grid at which the word can be found. A word matchesa straight, uninterrupted line of letters in the grid. A word canmatch the letters in the grid regardless of case (i.e. upper
andlower case letters are to be treated as the same). The matching canbe 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 itselfindicating the number of the cases following, each of them asdescribed below. This line is followed by a blank line, and thereis also a blank line between two consecutive inputs.
The input begins with a pair of integers, m followed byn,1<=m,n<=50, in decimal notation ona single line. The next m lines contain n letters each; this is thegrid of letters in which the words of the list must be found. Theletters in the grid may be in upper
or lower case. Following thegrid of letters, another integer k appears on a line by itself (1<=k<=20). The next k lines of inputcontain the list of words to search for, one word per line. Thesewords may contain upper and lower case letters only (no spaces,hyphens
or other non-alphabeticcharacters).
Output
For each test case, the output must follow the description below.The outputs of two consecutive cases will be separated by a blankline.
For each word in the word list, a pair of integers representing thelocation of the corresponding word in the grid must be output. Theintegers must be separated by a single space. The first integer isthe line in the grid where the first letter of the given word
canbe found (1 represents the topmost line in the grid, and mrepresents the bottommost line). The second integer is the columnin the grid where the first letter of the given word can be found(1 represents the leftmost column in the grid, and n represents therightmost
column in the grid). If a word can be found more thanonce in the grid, then the location which is output shouldcorrespond to the uppermost occurence of the word (i.e. theoccurence which places the first letter of the word closest to thetop of the grid). If
two or more words are uppermost, the outputshould correspond to the leftmost of these occurences. All wordscan 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
代码
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define N 60
int search(char list[]
, char word[], int row, int col, int m, int n, int len)
{
int i, j, k;
for (i = row, k = 0; i >= 0 && k < len && list[i][col] == word[k]; --i, ++k) ;
if (k == len) return 1;
for (i = row, k = 0; i < m && k < len && list[i][col] == word[k]; ++i, ++k) ;
if (k == len) return 1;
for (j = col, k = 0; j >= 0 && k < len && list[row][j] == word[k]; --j, ++k) ;
if (k == len) return 1;
for (j = col, k = 0; j < n && k < len && list[row][j] == word[k]; ++j, ++k) ;
if (k == len) return 1;
for (i = row, j = col, k = 0; i >= 0 && j >= 0 && k < len && list[i][j]
== word[k]; i--, j--, ++k) ;
if (k == len) return 1;
if (k == len) return 1;
for (i = row, j = col, k = 0; i >= 0 && j < n && k < len && list[i][j] == word[k]; i--, j++, ++k) ;
if (k == len) return 1;
for (i = row, j = col, k = 0; i < m && j >= 0 && k < len && list[i][j] == word[k]; i++, j--, ++k) ;
if (k == len) return 1;
for (i = row, j = col, k = 0; i < m && j < n && k < len && list[i][j] == word[k]; i++, j++, ++k) ;
return k == len;
}
int main()
{
char list
, words
;
int i, j, k, z, m, n, cases;;
scanf("%d", &cases);
while (cases--) {
int found
= {0};
scanf("%d%d", &m, &n);
for (i = 0; i < m; ++i)
scanf("%s", list[i]);
for (i = 0; i < m; ++i)
for (j = 0; j < n; ++j)
list[i][j] = toupper(list[i][j]);
scanf("%d", &k);
for (i = 0; i < k; ++i)
scanf("%s", words[i]);
for (i = 0; i < k; ++i)
for (j = 0; j < strlen(words[i]); ++j)
words[i][j] = toupper(words[i][j]);
for (z = 0; z < k; ++z)
for (i = 0; i < m; ++i)
for (j = 0; j < n; ++j)
if (!found[z] && search(list, words[z], i, j, m, n, strlen(words[z]))) {
printf("%d %d\n", i+1, j+1);
found[z] = 1;
}
if (cases)
printf("\n");
}
return 0;
}
andlower case letters are to be treated as the same). The matching canbe 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 itselfindicating the number of the cases following, each of them asdescribed below. This line is followed by a blank line, and thereis also a blank line between two consecutive inputs.
The input begins with a pair of integers, m followed byn,1<=m,n<=50, in decimal notation ona single line. The next m lines contain n letters each; this is thegrid of letters in which the words of the list must be found. Theletters in the grid may be in upper
or lower case. Following thegrid of letters, another integer k appears on a line by itself (1<=k<=20). The next k lines of inputcontain the list of words to search for, one word per line. Thesewords may contain upper and lower case letters only (no spaces,hyphens
or other non-alphabeticcharacters).
Output
For each test case, the output must follow the description below.The outputs of two consecutive cases will be separated by a blankline.
For each word in the word list, a pair of integers representing thelocation of the corresponding word in the grid must be output. Theintegers must be separated by a single space. The first integer isthe line in the grid where the first letter of the given word
canbe found (1 represents the topmost line in the grid, and mrepresents the bottommost line). The second integer is the columnin the grid where the first letter of the given word can be found(1 represents the leftmost column in the grid, and n represents therightmost
column in the grid). If a word can be found more thanonce in the grid, then the location which is output shouldcorrespond to the uppermost occurence of the word (i.e. theoccurence which places the first letter of the word closest to thetop of the grid). If
two or more words are uppermost, the outputshould correspond to the leftmost of these occurences. All wordscan 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
代码
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define N 60
int search(char list[]
, char word[], int row, int col, int m, int n, int len)
{
int i, j, k;
for (i = row, k = 0; i >= 0 && k < len && list[i][col] == word[k]; --i, ++k) ;
if (k == len) return 1;
for (i = row, k = 0; i < m && k < len && list[i][col] == word[k]; ++i, ++k) ;
if (k == len) return 1;
for (j = col, k = 0; j >= 0 && k < len && list[row][j] == word[k]; --j, ++k) ;
if (k == len) return 1;
for (j = col, k = 0; j < n && k < len && list[row][j] == word[k]; ++j, ++k) ;
if (k == len) return 1;
for (i = row, j = col, k = 0; i >= 0 && j >= 0 && k < len && list[i][j]
== word[k]; i--, j--, ++k) ;
if (k == len) return 1;
if (k == len) return 1;
for (i = row, j = col, k = 0; i >= 0 && j < n && k < len && list[i][j] == word[k]; i--, j++, ++k) ;
if (k == len) return 1;
for (i = row, j = col, k = 0; i < m && j >= 0 && k < len && list[i][j] == word[k]; i++, j--, ++k) ;
if (k == len) return 1;
for (i = row, j = col, k = 0; i < m && j < n && k < len && list[i][j] == word[k]; i++, j++, ++k) ;
return k == len;
}
int main()
{
char list
, words
;
int i, j, k, z, m, n, cases;;
scanf("%d", &cases);
while (cases--) {
int found
= {0};
scanf("%d%d", &m, &n);
for (i = 0; i < m; ++i)
scanf("%s", list[i]);
for (i = 0; i < m; ++i)
for (j = 0; j < n; ++j)
list[i][j] = toupper(list[i][j]);
scanf("%d", &k);
for (i = 0; i < k; ++i)
scanf("%s", words[i]);
for (i = 0; i < k; ++i)
for (j = 0; j < strlen(words[i]); ++j)
words[i][j] = toupper(words[i][j]);
for (z = 0; z < k; ++z)
for (i = 0; i < m; ++i)
for (j = 0; j < n; ++j)
if (!found[z] && search(list, words[z], i, j, m, n, strlen(words[z]))) {
printf("%d %d\n", i+1, j+1);
found[z] = 1;
}
if (cases)
printf("\n");
}
return 0;
}
相关文章推荐
- 优酷菜单
- Find Minimum in Rotated Sorted Array II
- http://www.zhihuishi.com/source/1.html
- 你问我为什么要写日志?
- Android自定义属性理解和应用
- 690 - Pipeline Scheduling
- topcoder decode
- Snail—OC学习之可变字符串NSMutableString
- uva 10361(字符串)
- 组合数
- 获得当前打开的窗口标题
- JavaScript页面的简单运算
- Android清单文件详解(六) ---- <activity>节点的属性
- Codeforces 432E Square Tiling(结构体+贪婪)
- UnicodeEncodeError: 'gbk' codec can't encode character '\xbb' in position 0: illegal multibyte sequence
- 可伸缩Web架构与分布式系统
- (七十七)地理编码与反地理编码
- 大数阶乘
- java jvm内存
- iOS开发笔记---SDWebImage分析