Where's Waldorf?

来源:互联网 发布:社交网络 电影 百度云 编辑:程序博客网 时间:2024/06/09 20:25
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 

18 11abcDEFGhigghEbkWalDorkFtyAwaldORmFtsimrLqsrcbyoArBeDeyvKlcbqwikomkstrEBGadhrbyUiqlxcnBjf4WaldorfBambiBettyDagbert

Sample Output 

2 52 31 27 8


苦战六小时 终于把这道B题拿下了 开始的时候 各种没输出  后来经过调试发现是 输入的时候有点问题 没有吧所有的数据都输入进去 看来以后需要好好注意输入数据的处理了

我身边的同学做这道题 都是用指针和八个函数 构思 代码看起来非常的清晰 但是本屌丝表示完全不会用指针和函数 所以干脆这样写了


#define TEST
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
using namespace std;
const int maxn=1000;
int main()
{
    freopen("e:\\input.txt","r",stdin);
    char str[maxn][50];
    char ch[20][50];
    int a,n,m;
    int k,i,j;
    int x,l;
    int p,q,len,ok,lenn;
    scanf("%d",&x);
    while(x>0)
    {
        x--;
        memset(str,0,sizeof(str));
        memset(ch,0,sizeof(ch));
        scanf("%d %d",&m,&n);
        for(i=0; i<m; i++)
        {
            scanf("%s",str[i]);
        }
        scanf("%d",&a);
        for(k=0; k<a; k++)
        {
            scanf("%s",ch[k]);
        }
        for(i = 0; i < a; i++)
        {
            ok = 0;
            for(int j = 0; j < m && !ok; j++)
                for(int k = 0; k < n && !ok; k++)
                    if(toupper(str[j][k]) == toupper(ch[i][0]))//因为不需要区分大小写问题所以干脆全转换成大写字母比较
                    {
                        len = strlen(ch[i]);

//接下来按照八个方向顺时针开始比较如果正确 就输出首字母的下标

                        for(p = j-1, q = k-1, lenn = 1; p >= 0 && q >= 0; --p, --q)
                        {
                            if(toupper(str[p][q]) == toupper(ch[i][lenn])) lenn++;
                            else break;
                            if(lenn == len)
                            {
                                printf("%d %d\n", j+1, k+1);
                                ok = 1;
                            }
                        }//下一条开始每条都需要判断OK是否为1,避免重复运算和输出
                        for(p = j+1, q = k+1, lenn = 1; p <= m && q <= n && !ok; ++p, ++q)
                        {
                            if(toupper(str[p][q]) == toupper(ch[i][lenn])) lenn++;
                            else break;
                            if(lenn == len)
                            {
                                printf("%d %d\n", j+1, k+1);
                                ok = 1;
                            }
                        }
                        for(p = j+1, q = k-1, lenn = 1; p <= m && q >= 0 && !ok; ++p, --q)
                        {
                            if(toupper(str[p][q]) == toupper(ch[i][lenn])) lenn++;
                            else break;
                            if(lenn == len)
                            {
                                printf("%d %d\n", j+1, k+1);
                                ok = 1;
                            }
                        }
                        for(p = j-1, q = k+1, lenn = 1; p >= 0 && q <= n && !ok; --p, ++q)
                        {
                            if(toupper(str[p][q]) == toupper(ch[i][lenn])) lenn++;
                            else break;
                            if(lenn == len)
                            {
                                printf("%d %d\n", j+1, k+1);
                                ok = 1;
                            }
                        }
                        for(p = j-1, q = k, lenn = 1; p >= 0 && !ok; --p)
                        {
                            if(toupper(str[p][q]) == toupper(ch[i][lenn])) lenn++;
                            else break;
                            if(lenn == len)
                            {
                                printf("%d %d\n", j+1, k+1);
                                ok = 1;
                            }
                        }
                        for(p = j+1, q = k, lenn = 1; p <= m && !ok; ++p)
                        {
                            if(toupper(str[p][q]) == toupper(ch[i][lenn])) lenn++;
                            else break;
                            if(lenn == len)
                            {
                                printf("%d %d\n", j+1, k+1);
                                ok = 1;
                            }
                        }
                        for(p = j, q = k-1, lenn = 1; q >= 0 && !ok; --q)
                        {
                            if(toupper(str[p][q]) == toupper(ch[i][lenn])) lenn++;
                            else break;
                            if(lenn == len)
                            {
                                printf("%d %d\n", j+1, k+1);
                                ok = 1;
                            }
                        }
                        for(p = j, q = k+1, lenn = 1; q <= n && !ok; ++q)
                        {
                            if(toupper(str[p][q]) == toupper(ch[i][lenn])) lenn++;
                            else break;
                            if(lenn == len)
                            {
                                printf("%d %d\n", j+1, k+1);
                                ok = 1;
                            }
                        }
                    }
        }
        if(x>0)
            printf("\n");//空行 万恶的空行 就因为这个我WA了一次
    }
    return 0;
}


0 0