您的位置:首页 > 其它

uva_10010_Where's Waldorf?

2012-10-26 22:10 549 查看
这里必须注意的是输出格式,uva里面好像是没有格式错误的,多一个少一个空格或者回车都返回wanganswer.
这里题意为搜索8个方位,暴力的时间复杂度还算可以。
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
using namespace std;

#define DIR             8
#define MAX_WORD        151
#define MAX_KEYWORD     121
#define INF             0x3f3f3f3f

int row, col;
int dir[][2] = {
{1, 0}, {-1, 0}, {0, 1}, {0, -1},
{1, 1}, {-1, -1}, {1, -1}, {-1, 1}
};
int ans_x[MAX_KEYWORD], ans_y[MAX_KEYWORD];
char map[MAX_WORD][MAX_WORD], key_word[MAX_KEYWORD][MAX_WORD], tmp[DIR][MAX_WORD];

void conversion(char *src)
{
for(int i = 0; i < strlen(src); i ++) {
if( isupper(src[i]) ) {
src[i] += 32;
}
}
}

void get_direction(const int &d, int len, const int &x, const int &y)
{
int tx(x), ty(y), t_pos(0);
tmp[d][t_pos ++] = map[x][y]; len --;
while( true ) {
tx += dir[d][0]; ty += dir[d][1];
if( tx < 0 || ty < 0 || tx >= row || ty >= col || len <= 0 ) {
tmp[d][t_pos] = '\0'; return;
}
tmp[d][t_pos ++] = map[tx][ty]; len --;
}
}

void get_position(const int &k_cnt)
{
memset(ans_x, 0x3f, sizeof(ans_x)); memset(ans_y, 0x3f, sizeof(ans_y));
for(int i = 0; i < row; i ++) {
for(int j = 0; j < col; j ++) {
for(int k = 0; k < k_cnt; k ++) {
for(int d = 0; d < DIR && (INF == ans_x[k]); d ++) {
get_direction(d, strlen(key_word[k]), i, j);
if( !strcmp(tmp[d], key_word[k]) ) {
ans_x[k] = i; ans_y[k] = j; break;
}
}
}
}
}
}

int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("test.in", "r", stdin);
#endif
int cas, k_cnt, ans_row, ans_col;
scanf("%d", &cas);
for(int cnt = 0 ; cnt < cas; cnt ++ )  {
if( cnt ) {
printf("\n");
}
scanf("%d %d", &row, &col);
for(int i = 0; i < row; i ++) {
scanf("%s", map[i]); conversion(map[i]);
}
scanf("%d", &k_cnt);
for(int i = 0; i < k_cnt; i ++) {
scanf("%s", key_word[i]); conversion(key_word[i]);
}
get_position(k_cnt);
for(int i = 0; i < k_cnt; i ++) {
printf("%d %d\n", ans_x[i]+1, ans_y[i]+1);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: