zoukankan      html  css  js  c++  java
  • uva 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 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 ( $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
     

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

     1 #include <iostream>
     2 #include <cstdlib>
     3 #include <cstdio>
     4 #include <cstring>
     5 #include <cctype>
     6 
     7 using namespace std;
     8 int x[] = {0, -1, -1, -1, 0, 1, 1, 1};
     9 int y[] = {-1, -1, 0, 1, 1, 1, 0, -1};
    10 char str[51][51];
    11 
    12 int main(void)
    13 {
    14     int m, n, i, j, l, r, t, len;
    15     char s[50];
    16 #ifndef ONLINE_JUDGE
    17     freopen("in", "r", stdin);
    18 #endif
    19     int T;
    20     cin >> T;
    21     while (T--)
    22     {
    23     cin >> m >> n;
    24     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]); }
    25     cin >> t;
    26     while (t--)
    27     {
    28         cin >> s; len = strlen(s); int cnt = 0, k = 0, u = 0;
    29         for (i = 0; i < len; i++)    s[i] = toupper(s[i]);
    30         for (i = 0; i < m; i++)
    31         {
    32             for (j = 0; j < n; j++)
    33             {
    34                 if (str[i][j] == s[0]) 
    35                 {
    36                     for (k = 0; k < 8; k++)
    37                     {
    38                         for (l = i, r = j, u = 0; l>=0 && r>=0 && l<m && r<n; l+=x[k], r+=y[k], u++)
    39                         { //这里还有一个陷阱,犹豫输出的是首字母的位置,所以,跟扫描的顺序无关
    40                             if (str[l][r] != s[u])    break;
    41                             if (u == len-1)    { cout << i+1 << ' ' << j+1 << endl; } //这里要注意,要输出首字母的位置!
    42                         }
    43                         if (u == len)    break;  // 下面三处判断不能写成 u == len - 1,同时在上一个语句中加上break, 原因自己认真想,O(∩_∩)O哈哈~
    44                     }
    45                 }
    46                 if (u == len)    break;
    47             }
    48             if (u == len)    break;
    49         }
    50     }
    51     if (T)    cout << endl;
    52     }
    53 
    54     return 0;
    55 }

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

  • 相关阅读:
    Day2 while 循环,格式化输出,运算符,字符串编码
    Day 1 变量,基础数据类型与条件语句
    关于字符的一些看法
    正则的全局问题
    模块——js功能(倒计时,幻灯)
    垂直居中
    不确定宽度元素居中
    css3媒体查询
    less的预处理
    手机幻灯
  • 原文地址:https://www.cnblogs.com/liuxueyang/p/2761925.html
Copyright © 2011-2022 走看看