UVa-10010-Where's Waldorf?

AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 1. Elementary Problem Solving :: String


// 10010 - Where's Waldorf?
#include <iostream>
#include <cstring>
#include <cctype>
//include <cstdio>
using namespace std;

int X, Y, m, n, len;
char word[100];
char data[100][100];
int r[] = {-1, 1, 0, 0, -1, -1, 1, 1};	// 模拟八个方向
int c[] = {0, 0, -1, 1, -1, 1, -1, 1};

void input_grid()
{
	for(int i=0; i<m; i++)
		for(int j=0; j<n; j++)
		{
			cin >> data[i][j];
			data[i][j] = tolower(data[i][j]);
		}
}

int check(int p, int q)					// 判断是否越界
{
	return (p>=0 && q>=0 && p<m && q<n);
}

void find_word()
{
	int i, j, d, k, p, q;
	for(i=0; i<m; i++)					// 遍历每个起始位置
	{
		for(j=0; j<n; j++)
		{
			if(data[i][j] != word[0])	// 除去首字母不相同的
				continue;
			for(d=0; d<8; d++)			// 遍历每个方向
			{
				p = i;					// 记录坐标
				q = j;
				for(k=1; k<len; k++)
				{
					p += r[d];			// 向某个方向延伸
					q += c[d];
					if(!check(p, q) || data[p][q]!=word[k])
						break;			// 检查是否越界及逐个字符匹配
				}
				if(k == len)			// 符合条件立即输出
				{
					X = i;
					Y = j;
					return;
				}
			}
		}
	}
}

int main(void)
{
	//freopen("in", "r", stdin);
	//freopen("out", "w", stdout);
	int N, T;
	cin >> N;
	while(N--)
	{
		cin >> m >> n;
		input_grid();
		cin >> T;
		while(T--)
		{
			cin >> word;
			len = strlen(word);
			for(int i=0; i<len; i++)
				word[i] = tolower(word[i]);
			find_word();
			cout << X+1 << ' ' << Y+1 << endl;
		}
		if(N != 0) cout << endl;
	}
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值