3. Where's Waldorf

Given an m by n grid of letters 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 lowercase letters are to be treated as the same). The matching can be done in any of the eight horizontal, vertical, or diagonal directions through the grid.

Input

The input begins with a single positive integer on a line by itself indicating the number of cases, followed by a blank line. There is also a blank line between each two consecutive cases.

Each case begins with a pair of integers m followed by n on a single line, where 1 ≤ m, n ≤ 50 in decimal notation. The next m lines contain n letters each, representing the grid of letters where the words must be found. The letters in the grid may be in upper- or lowercase. Following the grid of letters, another integer k appears on a line by itself (1 ≤ k ≤ 20). The next k lines of input contain the list of words to search for, one word per line. These words may contain upper- and lowercase letters only – no spaces, hyphens, or other non-alphabetic characters.

Output

For each word in each test case, output a pair of integers representing its location in the corresponding grid. 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 output the location of the uppermost occurrence of the word (i.e., the occurrence which places the first letter of the word closest to the top of the grid). If two or more words are uppermost, output the leftmost of these occurrences. All words can be found at least once in the grid.

The output of two consecutive cases must be separated by a blank line.


测试输入关于“测试输入”的帮助期待的输出关于“期待的输出”的帮助时间限制关于“时间限制”的帮助内存限制关于“内存限制”的帮助额外进程关于“{$a} 个额外进程”的帮助
测试用例 1以文本方式显示
  1. 1↵
  2. 8 11↵
  3. abcDEFGhigg↵
  4. hEbkWalDork↵
  5. FtyAwaldORm↵
  6. FtsimrLqsrc↵
  7. byoArBeDeyv↵
  8. Klcbqwikomk↵
  9. strEBGadhrb↵
  10. yUiqlxcnBjf↵
  11. 4↵
  12. Waldorf↵
  13. Bambi↵
  14. Betty↵
  15. Dagbert↵
以文本方式显示
  1. 2 5↵
  2. 2 3↵
  3. 1 2↵
  4. 7 8↵
1秒1024KB0



#include<cstdio>//AC
#include<cstring>

char Map[55][55], str[55];
int trend[8][2] = { { -1,1 },{ 0,1 },{ 1,1 },{ 1,0},{ 1,-1 },{ 0,-1 },{ -1,-1 },{ -1,0 } };

int judge(char str[], int x, int y, int len, int mm, int nn)
{
	int xx = x, yy = y,n;
	for (int i = 0; i<8; i++)
	{
		n = 1;//忘记这个,好久没发现,痛啊·········
		while (Map[x + trend[i][0]][y + trend[i][1]] == str[n] && x + trend[i][0] >= 1 && x + trend[i][0] <= mm && y + trend[i][1] >= 1 && y + trend[i][1] <= nn)
		{
			x += trend[i][0]; y += trend[i][1];
			n++;
			
		}
		if (n == len)//这个判断条件如果放在第17行则WA
		{
			printf("%d %d\n", xx, yy);
			return 1;
		}
		x = xx; y = yy;
	}
	return 0;
}
int main()
{
	int t, k, ans, m, n, mm, nn;
	while (~scanf("%d", &t))
	{
		while (t--)
		{
			scanf("%d %d", &m, &n);
			mm = m; nn = n;
			getchar();//防止Map[i][j]='*'/'line feed'  吸收回车
			for (int i = 1; i <= m; i++)
			{
				for (int j = 1; j <= n; j++)
				{
					scanf("%c", &Map[i][j]);
					if (Map[i][j] <= 90)
						Map[i][j] += 32;
				}
				getchar(); //防止Map[i][j] = '*' / 'line feed' 吸收回车
			}
				
			scanf("%d", &k);
			while (k--)
			{
				ans = 0;
				memset(str, '\0', sizeof(str));
				scanf("%s", str);
				int len = strlen(str);
				for (int i = 0; i < len; i++)
				{
					if (str[i] <= 90)
						str[i] += 32;
				}
				for (int i = 1; i <= m; i++)
				{
					for (int j = 1; j <= n; j++)
					{
						if (Map[i][j] == str[0])
							ans = judge(str, i, j, len, mm, nn);
						if (ans)
							break;
					}
					if (ans)
						break;
				}
			}
			if (t >= 1)
				printf("\n");
		}
	}

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值