ACM-uva10010

#include <iostream>
#include <string>
#include <string.h>

using namespace std;

#define MAXN 50 + 5

int temp[MAXN][MAXN];
int r, c;
int dir[8][2] = 
{
	0, -1, -1, 0, 0, 1, 1, 0, -1, -1, -1, 1, 1, 1, 1, -1
};

int ansr, ansc;

int change(char ch)
{
	if (ch >= 'a' && ch <= 'z')
	{
		return ch - 'a';
	}
	else
	{
		return ch - 'A';
	}
}

bool match(int x, int y, string &str)
{
	int len = str.length();
	int num = 0;
	
	for (int i = 0; i < 8; i++)
	{
		num = 0;
		
		for (int j = 0; j < len; j++)
		{
			int xt = x + j * dir[i][0], yt = y + j * dir[i][1];
			
			if (xt >= 0 && xt < r && yt >= 0 && yt < c)
			{
				if (temp[xt][yt] == change(str[j]))
				{
					num++;
				}
				else
				{
					break;
				}
			}
			else
			{
				break;
			}
		}
		
		if (num == len)
		{
			return true;
		}
	}
	
	return false;
}

void find(string &str)
{
	bool ok = false;
	
	for (int i = 0; i < r; i++)
	{
		for (int j = 0; j < c; j++)
		{
			if (temp[i][j] == change(str[0]))
			{
				if (match(i, j, str))
				{
					ansr = i + 1;
					ansc = j + 1;
					ok = true;
					break;
				}
			}
		}
		if (ok)
		{
			break;
		}
	}
}

void input()
{
	int t, k;
	string str;
	char ch;
	
	cin >> t;
	
	while (t--)
	{
		cin >> r >> c;
		
		for (int i = 0; i < r; i++)
		{
			for (int j = 0; j < c; j++)
			{
				cin >> ch;
				temp[i][j] = change(ch);
			}
		}
		
		cin >> k;
		
		for (int i = 0; i < k; i++)
		{
			cin >> str;
			
			find(str);
			
			cout << ansr << ' ' << ansc << endl;
		}
		
		if (t)
		{
			cout << endl;
		}
	}	
}

int main()
{
	input();
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值