uva 10010 - Where's Waldorf?

November 9, 2012
Uva

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. 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 nletters 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). 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.   这道题目没做出来,看的网上代码想明白了,自己写了一遍,还是有bug,后来才调试出来,唉,各种错误各种bug.不幸的人各有各的不幸……


#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cctype>

using namespace std;
int x[] = {0, -1, -1, -1, 0, 1, 1, 1};
int y[] = {-1, -1, 0, 1, 1, 1, 0, -1};
char str[51][51];

int main(void)
{
    int m, n, i, j, l, r, t, len;
    char s[50];
#ifndef ONLINE_JUDGE
    freopen("in", "r", stdin);
#endif
    int T;
    cin >> T;
    while (T--)
    {
    cin >> m >> n;
    for (i = 0; i < m; i++)    { cin >> str[i]; len = strlen(str[i]); for (j = 0; j < len; j++) str[i][j] = toupper(str[i][j]); }
    cin >> t;
    while (t--)
    {
        cin >> s; len = strlen(s); int cnt = 0, k = 0, u = 0;
        for (i = 0; i < len; i++)    s[i] = toupper(s[i]);
        for (i = 0; i < m; i++)
        {
            for (j = 0; j < n; j++)
            {
                if (str[i][j] == s[0]) 
                {
                    for (k = 0; k < 8; k++)
                    {
                        for (l = i, r = j, u = 0; l>=0 && r>=0 && l<m && r<n; l+=x[k], r+=y[k], u++)
                        { //这里还有一个陷阱,犹豫输出的是首字母的位置,所以,跟扫描的顺序无关
                            if (str[l][r] != s[u])    break;
                            if (u == len-1)    { cout << i+1 << ' ' << j+1 << endl; } //这里要注意,要输出首字母的位置!
                        }
                        if (u == len)    break;  // 下面三处判断不能写成 u == len - 1,同时在上一个语句中加上break, 原因自己认真想,O(∩_∩)O哈哈~
                    }
                }
                if (u == len)    break;
            }
            if (u == len)    break;
        }
    }
    if (T)    cout << endl;
    }

    return 0;
}

牛人的想法就是犀利啊。。。膜拜一下……

comments powered by Disqus