uva 10010 Where's Waldorf?



Where's Waldorf? 

Given a m by n grid of letters, ( $1 \leq m,n \leq 20$), and a listof 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. upperand lower case letters are to be treated as the same). The matchingcan be done in any of the eight directions either horizontally, verticallyor 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 linescontain n letters each; this is the grid of letters in which thewords of the list must be found. The letters in the grid may be inupper or lower case. Following the grid of letters, another integerk appears on a line by itself ($1 \leq k \leq 20$). The next klines of input contain the list of words to search for, one word perline. These words may contain upper and lower case letters only (nospaces, 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 thelocation of the corresponding word in the grid must be output. Theintegers must be separated by a single space. The first integer is theline 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 thebottommost line). The second integer is the column in the grid wherethe first letter of the given word can be found (1 represents theleftmost column in the grid, and n represents the rightmost column inthe grid). If a word can be found more than once in the grid, then thelocation which is output should correspond to the uppermost occurenceof the word (i.e. the occurence which places the first letter of theword closest to the top of the grid). If two or more words areuppermost, the output should correspond to the leftmost of theseoccurences. 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
大意:
     感谢 DevilMayCry_Li的分享
     感谢 DevilMayCry_Li的分享
     感谢 DevilMayCry_Li的分享

此题是关于字符串查找的问题,和以往字符串的查找不同,此题必须从八个方向来查找,依次是从当前位置向左上方、上方、右上方、左边、右边、左下方、下方、右下方,由于本题的要求,这个查找顺序不能改变。

要点:

1大写和小写字母被认为是一样的,例A=a

2、输出时,每组测试数据之间没有空行;在每组测试数据中,每种情况(case)之间才有空行。  

3、对于单词表(word list)中的每一个单词,必须输出它在grid中的位置,这个位置用坐标行、列表示,行和列之间用一个空格间隔。

4、如果一个单词在grid中能够多次找到,那么输出位置时只能输出一个,且选择最上面、最左边的一个。这就要求必须采用上面所说的查找顺序。输入数据保证单词表中的所有单词在grid中均可找到。

代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
char target[55][55],word[55];
int m,n;
int is_leftup(int x,int y,int len)
{
    int i,j,k;
    for(i=x,j=y,k=0; i>=0&&j>=0&&k<len; i--,j--,k++)
    {
        char a=toupper(target[i][j]);
        char b=toupper(word[k]);
        if(a!=b)
            break;
    }
    if(k==len)
    {
        printf("%d %d\n",x+1,y+1);
        return 1;
    }
    return 0;
}
int is_up(int x,int y,int len)
{
    int i,j,k;
    for(i=x,j=y,k=0; i>=0&&k<len; i--,k++)
    {
        char a=toupper(target[i][j]);
        char b=toupper(word[k]);
        if(a!=b)
            break;
    }
    if(k==len)
    {
        printf("%d %d\n",x+1,y+1);
        return 1;
    }
    return 0;
}
int is_rightup(int x,int y,int len)
{
    int i,j,k;
    for(i=x,j=y,k=0; i<m&&j<n&&k<len; i++,j++,k++)
    {
        char a=toupper(target[i][j]);
        char b=toupper(word[k]);
        if(a!=b)
            break;
    }
    if(k==len)
    {
        printf("%d %d\n",x+1,y+1);
        return 1;
    }
    return 0;
}
int is_left(int x,int y,int len)
{
    int i,j,k;
    for(i=x,j=y,k=0; j>=0&&k<len; j--,k++)
    {
        char a=toupper(target[i][j]);
        char b=toupper(word[k]);
        if(a!=b)
            break;
    }
    if(k==len)
    {
        printf("%d %d\n",x+1,y+1);
        return 1;
    }
    return 0;
}
int is_right(int x,int y,int len)
{
    int i,j,k;
    for(i=x,j=y,k=0; j<n&&k<len; j++,k++)
    {
        char a=toupper(target[i][j]);
        char b=toupper(word[k]);
        if(a!=b)
            break;
    }
    if(k==len)
    {
        printf("%d %d\n",x+1,y+1);
        return 1;
    }
    return 0;
}
int is_leftdown(int x,int y,int len)
{
    int i,j,k;
    for(i=x,j=y,k=0; i>=0&&j>=0&&k<len; i--,j--,k++)
    {
        char a=toupper(target[i][j]);
        char b=toupper(word[k]);
        if(a!=b)
            break;
    }
    if(k==len)
    {
        printf("%d %d\n",x+1,y+1);
        return 1;
    }
    return 0;
}
int is_down(int x,int y,int len)
{
    int i,j,k;
    for(i=x,j=y,k=0; i<m&&k<len; i++,k++)
    {
        char a=toupper(target[i][j]);
        char b=toupper(word[k]);
        if(a!=b)
            break;
    }
    if(k==len)
    {
        printf("%d %d\n",x+1,y+1);
        return 1;
    }
    return 0;
}
int is_rightdown(int x,int y,int len)
{
    int i,j,k;
    for(i=x,j=y,k=0; i<m&&j<n&&k<len; i++,j++,k++)
    {
        char a=toupper(target[i][j]);
        char b=toupper(word[k]);
        if(a!=b)
            break;
    }
    if(k==len)
    {
        printf("%d %d\n",x+1,y+1);
        return 1;
    }
    return 0;
}
int main()
{
    int sum;
    int p;
    int i,j,len;
    int flag=0;
    while(~scanf("%d",&sum))
    {
        while(sum--)
        {
            scanf("%d%d",&m,&n);
            for(i=0; i<m; i++)
                scanf("%s",target[i]);
            scanf("%d",&p);
            while(p--)
            {
                scanf("%s",word);
                len=strlen(word);
                for(i=0; i<m; i++)
                {
                    flag=0;
                    for(j=0; j<n; j++)
                    {
                        if(is_leftup(i,j,len))
                        {
                            flag=1;
                            break;
                        }
                        else if(is_up(i,j,len))
                        {
                            flag=1;
                            break;
                        }
                        else if(is_rightup(i,j,len))
                        {
                            flag=1;
                            break;
                        }
                        else if(is_left(i,j,len))
                        {
                            flag=1;
                            break;
                        }
                        else if(is_right(i,j,len))
                        {
                            flag=1;
                            break;
                        }
                        else if(is_leftdown(i,j,len))
                        {
                            flag=1;
                            break;
                        }
                        else if(is_down(i,j,len))
                        {
                            flag=1;
                            break;
                        }
                        else if(is_rightdown(i,j,len))
                        {
                            flag=1;
                            break;
                        }
                    }
                    if(flag)
                        break;
                }
            }
            if(sum>0)
                printf("\n");
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值