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 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 ≤ m, n ≤ 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 ≤ k ≤ 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

问题链接UVA10010 Where's Waldorf?

问题简述: 

  给定矩阵是m*n的,其中有字母;给定k个单词,从矩阵的某个位置开始寻找,可以有8种方向,寻找过程中不改变方向。输出各个单词的起始位置。

问题分析

  预先定义数组drow[]和dcol[],其中放入8个方向。采用暴力法向8个方向搜索一下就可以了。

程序说明

  需要注意数组下标与矩阵下标的转换。

题记:逻辑清晰代码简洁是程序员的一种境界。

参考链接:(略)

AC的C++语言程序如下:

/* UVA10010 Where's Waldorf? */

#include <bits/stdc++.h>

using namespace std;

// 方向分别是:右,下,左,上,右下, 左上,右上,左下
int drow[] = {0, 1, 0, -1,   1, -1, -1, 1};
int dcol[] = {1, 0, -1, 0,   1, -1, 1, -1};
const int D = sizeof(drow) / sizeof(int);

const int N = 50;
char g[N][N], w[N + 1];

void find(int m, int n)
{
    int len = strlen(w);

    for(int i = 0; i < m; i++)
        for(int j = 0; j < n; j++) {
            if(g[i][j] == w[0]) {
                for(int k = 0; k < D; k++) {
                    int nextrow = i, nextcol = j, l = 0;
                    while(g[nextrow][nextcol] == w[l]) {
                        if(++l == len) {
                            cout << i + 1 << " " << j + 1 << endl;
                            return;
                        }
                        nextrow += drow[k];
                        nextcol += dcol[k];
                        if(nextrow < 0 || nextrow >= m || nextcol < 0 || nextcol >= n)
                            break;
                    }
                }
            }
        }
}

int main()
{
    int t, m, n, k;
    cin >> t;
    while(t--) {
        cin >> m >> n;
        for(int i = 0; i < m; i++)
            for(int j = 0; j < n; j++) {
                cin >> g[i][j];
                g[i][j] = toupper(g[i][j]);
            }

        cin >> k;
        for(int i = 0; i < k; i++) {
            cin >> w;
            for(int j = 0; w[j]; j++)
                w[j] = toupper(w[j]);
            find(m, n);
        }

        if(t)
            cout << endl;
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/tigerisland45/article/details/81484937