uva 10010 - Where's Waldorf?

#include<iostream>
#include<cctype>
#include<algorithm>
using namespace std;

char a[55][55];
string s[22];
int T,m,n,k;

int mx[] = {1, 1, 1, 0, 0, -1, -1, -1};              //定义8个方向
int my[] = {1, 0, -1, 1, -1, 1, 0, -1};

bool in(int x,int y){
    return x>=1&&x<=m&&y>=1&&y<=n;
}

void getLocation(string s){
    int flag ;

    for(int i = 1; i <= m; i++){
        for(int j = 1; j <= n; j++){
            if(a[i][j] == tolower(s[0])){           //找到与要查找的字符串首字母
                for(int k = 0; k < 8; k++){         //向8个方向遍历
                    flag = 1;
                    int t1 = i,t2 = j;
                    for(int p = 1; p < s.length(); p++){
                        t1 += mx[k];
                        t2 += my[k];
                        if(a[t1][t2] != tolower(s[p]) || !in(t1,t2))   //遇到不想等,或越界,中止
                        {
                            flag = 0;
                            break;
                        }
                    }
                    if(flag){
                        cout << i << " " << j << endl;
                        return;
                    }
                }
            }
        }
    }
}

int main(){
    cin >> T;
    cin.ignore();

    while(T--){
        cin >> m >> n;
        cin.ignore();

        for(int i = 1; i<= m; i++)
            for(int j = 1; j <= n; j++){
                cin >> a[i][j];
                a[i][j] = tolower(a[i][j]);          //将字符变成小写
            }

        cin >> k;
        cin.ignore();

        for(int i = 0; i < k; i++)
            cin >> s[i];
        for(int i = 0; i < k; i++){
            getLocation(s[i]);
        }

        cout << endl;
    }

    return 0;
}

 

 

posted @ 2015-07-16 01:34  杨永华  阅读(158)  评论(0编辑  收藏  举报