2009年2月20日 星期五

Q10010: Where's Waldorf?

// Run time: 0.000
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define N 50
char str[N][N+1];
char arr[N+1];
void check(int length, char ch, int row, int len);
bool test(int row, int col, int length, int row, int len);

int main()
{
int n;
int row, len;
int length; // check string's length
int num;
int i, j;

scanf("%d", &n);
while (n--)
{
scanf("%d%d\n", &row, &len);

// input
for ( i = 0; i < row; i++)
gets(str[i]);

// toupper
for ( i = 0; i < row; i++)
for ( j = 0; j < len; j++)
str[i][j] = toupper(str[i][j]);

scanf("%d\n", &num);

// input for check string
while (num--)
{
gets(arr);
length = strlen(arr);

// toupper case
for ( i = 0; i < length; i++)
arr[i] = toupper(arr[i]);

check(length, arr[0], row, len);
}
if (n)
printf("\n");
}

return 0;
}

void check(int length, char ch, int row, int len)
{
int i, j;
for ( i = 0; i < row; i++)
for ( j = 0; j < len; j++)
if (str[i][j] == ch)
if (test(i, j, length, row, len))
return;
}

bool test(int r, int c, int length, int row, int len)
{
int i, j, k;

// 往左看
if (c-length >= -1)
{
for ( i = c-1, j = 1; j < length; i--, j++)
if (str[r][i] != arr[j])
break;
if (j == length)
{
printf("%d %d\n", r+1, c+1);
return true;
}
}

// 往右看
if (len-c >= length)
{
for ( i = c+1, j = 1; j < length; i++, j++)
if (str[r][i] != arr[j])
break;
if (j == length)
{
printf("%d %d\n", r+1, c+1);
return true;
}
}

// 往上看
if (r-length >= -1)
{
for ( i = r-1, j = 1; j < length; i--, j++)
if (str[i][c] != arr[j])
break;
if (j == length)
{
printf("%d %d\n", r+1, c+1);
return true;
}
}

// 往下看
if (r+length <= row)
{
for ( i = r+1, j = 1; j < length; i++, j++)
if (str[i][c] != arr[j])
break;
if (j == length)
{
printf("%d %d\n", r+1, c+1);
return true;
}
}

// 往左上
if (c-length >= -1 && r-length >= -1)
{
for ( i = r-1, j = c-1, k = 1; k < length; i--, j--, k++)
if (str[i][j] != arr[k])
break;
if (k == length)
{
printf("%d %d\n", r+1, c+1);
return true;
}
}

// 往右下
if (len-c >= length && r+length <= row)
{
for ( i = r+1, j = c+1, k = 1; k < length; i++, j++, k++)
if (str[i][j] != arr[k])
break;
if (k == length)
{
printf("%d %d\n", r+1, c+1);
return true;
}
}

// 往左下
if (c-length >= -1 && r+length <= row)
{
for ( i = r+1, j = c-1, k = 1; k < length; i++, j--, k++)
if (str[i][j] != arr[k])
break;
if (k == length)
{
printf("%d %d\n", r+1, c+1);
return true;
}
}

// 往右上
if (len-c >= length && r-length >= -1)
{
for ( i = r-1, j = c+1, k = 1; k < length; i--, j++, k++)
if (str[i][j] != arr[k])
break;
if (k == length)
{
printf("%d %d\n", r+1, c+1);
return true;
}
}

return false;
}

沒有留言: