Fork me on GitHub

Uva 10010 - Where's Waldorf?

 Where's Waldorf? 

Time limit: 3.000 seconds

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 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 \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
 
 
#include<stdio.h>
#include<string.h>



int Traverse(char (*letter)[52], char (*word)[52], int row, int column, int k)
{
    int i, j, t, len, srhi, srhj, srhl, flag;
    char ch;
    for(t=0; t<k; ++t)
    {

        len = strlen(word[t]);   
        ch = word[t][0];    
        for(i=0, flag=1; i<row&&flag; ++i)    
        for(j=0; j<column; ++j) 
        {
            
            if(letter[i][j] == ch)  
            {
        
                if(j+1-len >= 0)    
                {
                    for(srhi=j,srhj=0; srhj<len; --srhi, ++srhj)
                    {
                        if(letter[i][srhi] != word[t][srhj]) {break;}
                    }
                    if(srhj == len)
                    {
                        printf("%d %d\n", i+1, j+1);
                        flag = 0;
                        break;
                    }
                }
                if(column-j-len >= 0)  
                {
                    for(srhi=j,srhj=0; srhj<len; ++srhi, ++srhj)
                    {
                        if(letter[i][srhi] != word[t][srhj]) { break;}
                    }
                    if(srhj == len)
                    {
                        printf("%d %d\n", i+1, j+1);
                        flag = 0;
                        break;
                    }    
                }
            
                if(i+1-len >= 0)       
                {
                    for(srhi=i,srhj=0; srhj<len; --srhi, ++srhj)
                    {
                        if(letter[srhi][j] != word[t][srhj]) {break;}
                    }
                    if(srhj == len)
                    {
                        printf("%d %d\n", i+1, j+1);
                        flag = 0;
                        break;
                    }
                }
                if(row-i-len >= 0)  
                {
                    for(srhi=i,srhj=0; srhj<len; ++srhi, ++srhj)
                    {
                        if(letter[srhi][j] != word[t][srhj]) { break;}
                    }
                    if(srhj == len)
                    {
                        printf("%d %d\n", i+1, j+1);
                        flag = 0;
                        break;
                    }
                }
        
                if(column-j-len >= 0 && row-i-len >= 0) 
                {
                    for(srhi=i,srhj=j,srhl=0; srhl<len; ++srhl, ++srhj, ++srhi)
                    {
                        if(letter[srhi][srhj] != word[t][srhl]) { break;}
                    }
                    if(srhl == len)
                    {
                        printf("%d %d\n", i+1, j+1);
                        flag = 0;
                        break;
                    }
                    
                }
                if(column-j-len >= 0 && i+1-len >= 0)    
                {
                    for(srhi=i,srhj=j,srhl=0; srhl<len; ++srhl, ++srhj, --srhi)
                    {
                        if(letter[srhi][srhj] != word[t][srhl]) {break;}
                    }
                    if(srhl == len)
                    {
                        printf("%d %d\n", i+1, j+1);
                        flag = 0;
                        break;
                    }
                }
                if(j+1-len >= 0 && row-i-len >= 0)       
                {
                    for(srhi=i,srhj=j,srhl=0; srhl<len; ++srhl, --srhj, ++srhi)
                    {
                        if(letter[srhi][srhj] != word[t][srhl]) {break;}
                    }
                    if(srhl == len)
                    {
                        printf("%d %d\n", i+1, j+1);
                        flag = 0;
                        break;
                    }
                }
                if(j+1-len >= 0 && i+1-len >= 0)        
                {
                    for(srhi=i,srhj=j,srhl=0; srhl<len; ++srhl, --srhj, --srhi)
                    {
                        if(letter[srhi][srhj] != word[t][srhl]) {break;}
                    }
                    if(srhl == len)
                    {
                        printf("%d %d\n", i+1, j+1);
                        flag = 0;
                        break;
                    }
                }
                
                 
            }
        }
    }
    return 0;
}

int main()
{
    char letter[52][52], word[52][52];
    int t, n, k, i, j, row ,column, len = 0;
    scanf("%d", &n);
    for(t=1; t<=n; ++t)
    {
        getchar();
        for(i=0; i<52; ++i) memset(letter[i], 0, sizeof(letter[0]));
        scanf("%d%d", &row, &column);
        for(i=0; i<row; ++i)
        {
            scanf("%s", letter[i]);
            getchar();
            for(j=0; j<column; ++j)
            if('A'<=letter[i][j] && letter[i][j]<='Z') letter[i][j] = letter[i][j] + 32;   
        }
        
        for(i=0; i<52; ++i) memset(word[i], 0, sizeof(letter[0]));
        scanf("%d", &k);
        for(i=0; i<k; ++i)
        {
            scanf("%s", word[i]);
            getchar();
            for(j=0, len=strlen(word[i]); j<len; ++j)
            if('A'<=word[i][j] && word[i][j]<='Z') word[i][j] = word[i][j] + 32;  
        }
    Traverse(letter, word, row, column, k);
    if(t != n) printf("\n");
    }
    return 0;
}

 

解题报告:

          

1Compilation error:Uva并不允许代码里面出现//等注释

1WA:未将输出中间值的printf函数删除

2WA:case之间忘了加blank line

题目很简单,查找存在的单词的首字母,其中查找单词的方式是找到首字母,在行上向左右两边匹配查找,在列上上下查找,左右斜向查找共八个方向查找,匹配成功输出坐标。

其中卡住我的是Traverse函数中的形参表示,传递数组中的数组,这个问题在《C的缺陷和陷阱》这本书中有提到;还有就是尽量不要复制代码,这会让你浪费更多的时间

 

posted @ 2012-11-12 19:21  Gifur  阅读(3372)  评论(0编辑  收藏  举报
TOP