110302 Where's Waldorf (Where s Waldorf)


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

using namespace std;

#define MAX_CHAR_IN_LINE 50

static bool EastFind(char input[MAX_CHAR_IN_LINE][MAX_CHAR_IN_LINE], int rows, int cols, char* word, int wordLen, int curRow, int curCol)
{
	if ((curCol + wordLen) > cols)
		return false;

	for (int i = 0; i < wordLen; ++i)
	{
		if (word[i] != input[curRow][curCol + i])
			return false;
	}

	return true;
}

static bool WestFind(char input[MAX_CHAR_IN_LINE][MAX_CHAR_IN_LINE], int rows, int cols, char* word, int wordLen, int curRow, int curCol)
{
	if (curCol < (wordLen - 1))
		return false;

	for (int i = 0; i < wordLen; ++i)
	{
		if (word[i] != input[curRow][curCol - i])
			return false;
	}

	return true;
}

static bool SouthFind(char input[MAX_CHAR_IN_LINE][MAX_CHAR_IN_LINE], int rows, int cols, char* word, int wordLen, int curRow, int curCol)
{
	if ((curRow + wordLen) > rows)
		return false;

	for (int i = 0; i < wordLen; ++i)
	{
		if (word[i] != input[curRow + i][curCol])
			return false;
	}

	return true;
}

static bool NorthFind(char input[MAX_CHAR_IN_LINE][MAX_CHAR_IN_LINE], int rows, int cols, char* word, int wordLen, int curRow, int curCol)
{
	if (curRow < (wordLen - 1))
		return false;

	for (int i = 0; i < wordLen; ++i)
	{
		if (word[i] != input[curRow - i][curCol])
			return false;
	}

	return true;
}

static bool SouthEastFind(char input[MAX_CHAR_IN_LINE][MAX_CHAR_IN_LINE], int rows, int cols, char* word, int wordLen, int curRow, int curCol)
{
	if ((curCol + wordLen) > cols)
		return false;
	if ((curRow + wordLen) > rows)
		return false;

	for (int i = 0; i < wordLen; ++i)
	{
		if (word[i] != input[curRow + i][curCol + i])
			return false;
	}

	return true;
}

static bool NorthEastFind(char input[MAX_CHAR_IN_LINE][MAX_CHAR_IN_LINE], int rows, int cols, char* word, int wordLen, int curRow, int curCol)
{
	if ((curCol + wordLen) > cols)
		return false;
	if (curRow < (wordLen - 1))
		return false;

	for (int i = 0; i < wordLen; ++i)
	{
		if (word[i] != input[curRow - i][curCol + i])
			return false;
	}

	return true;
}

static bool SouthWestFind(char input[MAX_CHAR_IN_LINE][MAX_CHAR_IN_LINE], int rows, int cols, char* word, int wordLen, int curRow, int curCol)
{
	if (curCol < (wordLen - 1))
		return false;
	if ((curRow + wordLen) > rows)
		return false;

	for (int i = 0; i < wordLen; ++i)
	{
		if (word[i] != input[curRow + i][curCol - i])
			return false;
	}

	return true;
}

static bool NorthWestFind(char input[MAX_CHAR_IN_LINE][MAX_CHAR_IN_LINE], int rows, int cols, char* word, int wordLen, int curRow, int curCol)
{
	if (curCol < (wordLen - 1))
		return false;
	if (curRow < (wordLen - 1))
		return false;

	for (int i = 0; i < wordLen; ++i)
	{
		if (word[i] != input[curRow - i][curCol - i])
			return false;
	}

	return true;
}

typedef bool (*PFIND_FUNC)(char input[MAX_CHAR_IN_LINE][MAX_CHAR_IN_LINE], int rows, int cols, char* word, int wordLen, int curRow, int curCol);
static PFIND_FUNC s_FindFuncArray[] =
{
	EastFind, WestFind, SouthFind, NorthFind, SouthEastFind, NorthEastFind, SouthWestFind, NorthWestFind
};

static bool FindWord(char input[MAX_CHAR_IN_LINE][MAX_CHAR_IN_LINE], int rows, int cols, char* word, int wordLen, int curRow, int curCol)
{
	for (int i = 0; i < (sizeof(s_FindFuncArray) / sizeof(PFIND_FUNC)); ++i)
	{
		if (s_FindFuncArray[i](input, rows, cols, word, wordLen, curRow, curCol))
			return true;
	}
	return false;
}

static void FindWord(char input[MAX_CHAR_IN_LINE][MAX_CHAR_IN_LINE], int rows, int cols, char* word)
{
	int len = strlen(word);
	for (int i = 0; i < len; ++i)
	{
		if (word[i] >= 'a')
			word[i] -= ('a' - 'A');
	}

	for (int i = 0; i < rows; ++i)
	{
		for (int j = 0; j < cols; ++j)
		{
			if (FindWord(input, rows, cols, word, len, i, j))
			{
				cout << i + 1 << ' ' << j + 1 << endl;
				return;
			}
		}
	}
}

static void DoTest(char input[MAX_CHAR_IN_LINE][MAX_CHAR_IN_LINE])
{
	int rows, cols;
	cin >> rows >> cols;

	for (int i = 0; i < rows; ++i)
	{
		for (int j = 0; j < cols; ++j)
		{
			cin >> input[i][j];
			if (input[i][j] >= 'a')
				input[i][j] -= ('a' - 'A');
		}
	}

	static char s_Word[MAX_CHAR_IN_LINE + 1];
	int wordsCnt;
	cin >> wordsCnt;

	for (int i = 0; i < wordsCnt; ++i)
	{
		cin >> s_Word;
		FindWord(input, rows, cols, s_Word);
	}
}

static void TestAll()
{
	static char s_Input[MAX_CHAR_IN_LINE][MAX_CHAR_IN_LINE];
	int cnt;
	cin >> cnt;
	for (int i = 0; i < cnt; ++i)
	{
		DoTest(s_Input);
		if (i < (cnt - 1))
			cout << endl;
	}
}

int main(int argc, char* argv[])
{
	TestAll();
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值