您的位置:首页 > 其它

UVa-10010-Where's Waldorf?

2014-08-22 08:49 387 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: