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 \leqm,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 integerk appears on a line by itself ( $1 \leq k \leq 20$). The nextk 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
/*大概的题意就是,从给定的大的字符数组里去找以下的几个数组,但是是从8个方向寻找(大小写不敏感)
当时给我的灵感利用8个函数去做,遗憾的是,格式太乱,内容太长,判断不太鲜明,以至于容易出错
所以可以归结到一个函数里,但是,如何构建这个函数,还是8个方向吗?难道还是一个方向一个方向的写,然后根据能找到一个就返回的函数?
别逗了,虽然那样比较容易想,但是,还是不利于竞赛,所以,如何找出代码不冗长,但又可以表达出和它相同的意思呢?
这就需要某些技巧,利用数组来做出X,Y 的移动方向,具体的参见我的代码。*/
 
 
#include<stdio.h>
#include<string.h>
char a[100][100];/*定义全局字符数组*/
char b[100];/*定义被查找的数组*/
int n,m;/*由于传递太麻烦,所以,直接全局变量*/
const long xd[]= {-1,-1,0,1,1,1,0,-1},yd[]= {0,1,1,1,0,-1,-1,-1};/*每一步都是想要上下要走的步骤,例如xd[0]、yd[0]表示x向上走,y不动*/
void search(int *t,int *k)
{
    int i,j,x_,y_,z;
    for(i=1; i<=n; i++) /*防a[i][j]走的时候会越界,所以,要都在i=1,和j=1时开始*/
    {
        for(j=1; j<=m; j++)
        {
            if(a[i][j]==b[0])/*查找第一项,当找到是进行以下操作*/
            {
                z=0;/*把方向清0*/
                while(z<8)/*从八个方向开始找*/
                {
                    x_=i,y_=j;/*定位到a[i][j]位置*/
                    int v;
                    for(v=0; b[v]&&a[x_][y_]==b[v]; v++)
                    {
                        x_+=xd[z];y_+=yd[z];/*移位*/
                    }
                    if(b[v]==0)/*当b[]能到最后,结合刚才循环的条件,就说明已经找到了*/
                    {
                        *t=i;
                        *k=j;/*把初始的位置传出去*/
                        return;
                    }
                    z++;/*可用for循环,因为这里的计数变量总是好掉*/
                }
            }
        }
    }
}
void main()
{
    /*freopen("C:\\Users\\john\\Desktop\\1.txt","r",stdin);*/
    int sj,x;
    scanf("%d",&sj);
    for(x=1;x<=sj;x++)/*事件数*/
    {
        if(x>1) printf("\n");/*每个事件后都有一个空格*/
        memset(a,0,sizeof(a));/*把a数组清空,以便于判断*/
	memset(b,0,sizeof(b));/*防止runtime error*/
        int i,j;/*行列数*/
        scanf("%d%d",&n,&m);/*输入矩阵行列*/
        for(i=1;i<=n;i++)
        {
            getchar();/*必须加,否者%c会吞掉字符*/
            for(j=1;j<=m;j++)/*判断时候开头的时候是a[1][1]所以,这里输入的时候也是1,1*/
            {
                scanf("%c",&a[i][j]);
                if('a'<=a[i][j]&&'z'>=a[i][j])/*条件说大小写不敏感,所以全都换成大写*/
                    a[i][j]-=32;
            }
        }
        scanf("%d",&i);
        j=0;
        for(j=1;j<=i;j++)/*被比较的字符串的个数*/
        {
            scanf("%s",&b);
            int k;
            for(k=0;k<strlen(b);k++)
            {
                if('a'<=b[k]&&'z'>=b[k])/*相应的,别比较的字符串也要变*/
                    b[k]-=32;
            }
            int x,y;/*定义x,y去接受结果*/
            search(&x,&y);
            printf("%d %d\n",x,y);
        }


    }
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值