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的缺陷和陷阱》這本書中有提到;還有就是盡量不要復制代碼,這會讓你浪費更多的時間

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM