UVa 10010 Where's Waldorf?
来源:互联网 发布:java常用设计模式详解 编辑:程序博客网 时间:2024/06/01 09:20
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
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 100char str[N][N], a[N];int t[8][2] = {{1,0}, {-1,0}, {0,1}, {0,-1}, {-1,1}, {1,-1}, {1,1}, {-1,-1}};int k, m, n, p, q, len;int is_find(int x, int y) {int row = x, col = y;if (str[x][y] == a[0]) {for (k = 0; k < 8; k++) {int cnt = 0;while (1) {if (x < 0 || y < 0 || x >= m || y >= n || str[x][y] != a[cnt])break;if (cnt == len - 1)return 1;x += t[k][0]; y += t[k][1];cnt++;}x = row; y = col;}return 0;}elsereturn 0;}int main() {scanf("%d", &q);while (q--) {scanf("%d %d", &m, &n);for (int i = 0; i < m; i++)scanf("%s", str[i]);for (int i = 0; i < m; i++) //转化成大写 for (int j = 0; j < n; j++)str[i][j] = toupper(str[i][j]);scanf("%d", &p);while (p--) {scanf("%s", a);len = strlen(a);for (int i = 0; i < len; i++) //转化成大写a[i] = toupper(a[i]);int flag;for (int i = 0; i < m; i++) {for (int j = 0; j < n; j++) {flag = 0;if (is_find(i, j)) {flag = 1;printf("%d %d\n", i + 1, j + 1);break;}}if (flag)break;}}if (q)printf("\n");}return 0;}
#include <stdio.h>#include <string.h>#include <ctype.h>#define In(up, x, low) ((x > up) && (x < low))#define N 100char str[N][N], a[N];int t[8][2] = {{1,0}, {-1,0}, {0,1}, {0,-1}, {-1,1}, {1,-1}, {1,1}, {-1,-1}};int k, m, n, p, q, len;int is_find(int x, int y) {if (str[x][y] == a[0]) {for (k = 0; k < 8; k++) {int cnt = 1;if (In((m - 1), (x + (len - 1) * t[k][0]), 0) || In((n - 1), (y + (len - 1) * t[k][1]), 0)) break;while (cnt < len) {if (str[x + cnt * t[k][0]][y + cnt * t[k][1]] == a[cnt]) cnt++;else break;}if (cnt == len)return 1;}if (k == 8)return 0;}elsereturn 0;}int main() {scanf("%d", &q);while (q--) {scanf("%d %d", &m, &n);for (int i = 0; i < m; i++)scanf("%s", str[i]);for (int i = 0; i < m; i++) //转化成大写 for (int j = 0; j < n; j++)str[i][j] = toupper(str[i][j]);scanf("%d", &p);while (p--) {scanf("%s", a);len = strlen(a);for (int i = 0; i < len; i++) //转化成大写a[i] = toupper(a[i]);int flag;for (int i = 0; i < m; i++) {for (int j = 0; j < n; j++) {flag = 0;if (is_find(i, j)) {flag = 1;printf("%d %d\n", i + 1, j + 1);break;}}if (flag)break;}}if (q)printf("\n");}return 0;}
可以先看要检查的串有没有超出范围,在比对一下是不是在某个方向上相等。或者直接看每个字符的坐标去比较有没有超出范围以及有没有相等。
- UVa 10010 - Where's Waldorf?
- uva 10010 Where's Waldorf?
- UVa 10010 - Where's Waldorf?
- UVa 10010 - Where's Waldorf?
- uva-10010 - Where's Waldorf?
- UVA 10010 - Where's Waldorf?
- UVa 10010 - Where's Waldorf?
- UVa 10010 - Where's Waldorf?
- UVA 10010 Where's Waldorf?
- UVa 10010: Where's Waldorf?
- UVA 10010 Where's Waldorf?
- UVa - 10010 - Where's Waldorf?
- UVA 10010 Where's Waldorf?
- uva 10010 - Where's Waldorf?
- uva 10010 Where's Waldorf?
- uva - 10010 - Where's Waldorf?
- UVa 10010 - Where's Waldorf?
- UVA - 10010 - Where's Waldorf
- hdu1686 oulipo 【KMP】
- PicoBlaze程序仿真调试及在线快速调试说明
- Zigbee技术开发一 设置NV_RESTORE
- dede如何对描述字符description限制字数
- ReactiveMongo 学习笔记
- UVa 10010 Where's Waldorf?
- 归并排序算法
- qq与邮箱的最大区别是什么
- [kuangbin带你飞]专题六 最小生成树
- 关于finally的几个问题
- Oracle海量数据导入MongoDB 采用JAVA反射-Penghao
- iOS UITableViewCell重用问题
- Spark安装
- 做了三年开发 到现在才完美解决了弹键盘的问题