UVA 10010

#include <iostream>
#include<cstdio>
//#include <string.h>
#include <cstring>

using namespace std;

char str[55][55];

int m,n;
char bin[55];

int dir[8][2] = {-1,-1,-1,0,-1,1,0,-1,0,1,1,0,1,-1,1,1};

bool _strcmp (char *p,char *q)
{
    int len = strlen (p);
    for (int i = 0; i < len; i++)
    if (p[i] != q[i] && (p[i] + 32) != q[i] && p[i] != (q[i] + 32))
    return false;
    return true;
}
bool exbfs(int r,int c)
{
    char aa[100];
    int k = 0;
    int x = r,y = c;
    int len = strlen(bin);
    for (int i = 0; i < 8; i++)
    {
        k = 1;
        x = r,y = c;
        aa[0] = str[x][y];
        while (k < len)
        {
            x += dir[i][0];
            y += dir[i][1];
            if (x >= 0 && x < m && y >= 0 && y < n)
            aa[k++] = str[x][y];
            else break;
        }
        if (k != len) continue;
        aa[k] = '\0';
        if (_strcmp (aa,bin)) return true;
    }
    return false;
}
int main ()
{
    int cas;
    scanf ("%d",&cas);
    printf("\n");
    while (cas--)
    {
        scanf ("%d %d\n",&m,&n);
        for (int i = 0; i < m; i++)
        {
            gets(str[i]);
        }
        int tt ;
        scanf ("%d\n",&tt);
        for (int t = 0; t < tt; t++)
        {
            gets(bin);
            int row = 0,clum = 0;
            bool flay = false;
            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    if (str[i][j] == bin[0] || str[i][j] == (bin[0]+32) || (str[i][j]+32) == bin[0])
                    {
                        row = i,clum = j;
                        if (exbfs(i,j))
                        {
                            flay = true;
                            break;
                        }
                    }
                }
                if (flay) break;
            }
            printf ("%d %d\n",row+1,clum+1);
        }
        if (cas != 0)
        printf ("\n");
    }
    return 0;
}

 

posted @ 2012-04-28 11:52  open your eyes  阅读(245)  评论(0编辑  收藏  举报