UVA 401

#include<cstdio>
#include<cstring>
#include<cctype>
#include<iostream>
using namespace std;
int main()
{
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    int c, m, j,n, k;
    char grid[60][60], word[60];
    int md[8] = {-1, 0, 1, 1, 1, 0, -1, -1}, nd[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
    scanf("%d", &c);
    for (int i = 1; i <= c; i++)
    {
        scanf("%d %d", &m, &n);
        scanf("%c", grid);
        for (j = 0; j < m; j++)
        {
            for (int k = 0; k <= n; k++)
            {
                scanf("%c", &grid[j][k]);
                if (islower(grid[j][k]))
                    grid[j][k] = toupper(grid[j][k]);
            }
        }
        scanf("%d", &k);
        for (j = 1; j <= k; j++)
        {
            scanf("%s", word);
            int l = strlen(word);
            for (int k = 0; k < l; k++)
            {
                if (islower(word[k]))
                    word[k] = toupper(word[k]);
            }
            bool ok = 0;
            for (int p = 0; p < m && !ok; p++)
            {
                for (int q = 0; q < n && !ok; q++)
                {
                    for (int r = 0; r < 8 && !ok; r++)
                    {
                        for (int s = 0; s < l; s++)
                        {
                            if (word[s] == grid[p + s*md[r]][q + s*nd[r]])
                            {
                                if (s == l - 1)
                                {
                                    ok = 1;
                                    printf("%d %d\n", p+1, q+1);
                                    break;
                                }
                                else
                                    continue;
                            }
                            else
                                break;
                        }
                    }
                }
            }
        }
        if (i < c)
            printf("\n");
    }
    return 0;
}

 

posted @ 2012-04-29 09:29  open your eyes  阅读(170)  评论(0编辑  收藏  举报