Description
Input
The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.The input begins with a pair of integers, m followed by n, in decimal notation on a single line. The next m lines contain n letters each; this is the grid of letters in which the words of the list must be found. The letters in the grid may be in upper or lower case. Following the grid of letters, another integer k appears on a line by itself ( ). The next k lines of input contain the list of words to search for, one word per line. These words may contain upper and lower case letters only (no spaces, hyphens or other non-alphabetic characters).
Output
For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.For each word in the word list, a pair of integers representing the location of the corresponding word in the grid must be output. The integers must be separated by a single space. The first integer is the line in the grid where the first letter of the given word can be found (1 represents the topmost line in the grid, and m represents the bottommost line). The second integer is the column in the grid where the first letter of the given word can be found (1 represents the leftmost column in the grid, and n represents the rightmost column in the grid). If a word can be found more than once in the grid, then the location which is output should correspond to the uppermost occurence of the word (i.e. the occurence which places the first letter of the word closest to the top of the grid). If two or more words are uppermost, the output should correspond to the leftmost of these occurences. All words can be found at least once in the grid.
Sample Input
1 8 11 abcDEFGhigg hEbkWalDork FtyAwaldORm FtsimrLqsrc byoArBeDeyv Klcbqwikomk strEBGadhrb yUiqlxcnBjf 4 Waldorf Bambi Betty Dagbert
Sample Output
2 5 2 3 1 2 7 8
刚刚AC啊,通宵一晚上才做出来。
这个题我的算法很纯粹,也可以说很白痴,就是一个一个字母的比较,这样实现起来相当的麻烦而且很难调试,还好我写出来之后没有出很大问题,只是在每组之间有个空行WA了一次,其实这样的代码出错率还是很高的。
对于更好的算法,有家伙就是建立八个数组分别储存八个方向,然后拿一个字符串与八个字符串比较,这样的思路实现起来不是很麻烦,非常适合函数调用,这样代码也精炼。
我这片长达360行的代码,真是够笨重的。
#include <stdio.h>
#include <string.h>
void atoA(char *p);
void fuz(int hang,int lie);
int hang(char *a,char *b,int h);
int lie(char *a,char *b,int h,int s);
int xie(char a[70][60],char *b,int h,int l);
char a[70][60];
char b[60];
int phang = 70,plie = 60;
int main()
{
int n = 0,i = 0;
int h = 0,l = 0,k = 0,h_tmp = 0;
int d = 0,j = 0;
int st = 0;
freopen ("1.txt","r",stdin);
scanf ("%d",&n);
for (i = 0; i < n; i++)
{
scanf ("%d%d%*c",&h,&l);
memset (a,'\0',sizeof(char) * 70 * 60);
for (k = 0; k < h; k++)
{
gets (a[k]);
atoA (a[k]);
}
for (j = 0; j < 60; j++)
{
a[h][j] = '\0';
}
scanf ("%d",&d);
for (j = 0; j < d; j++)
{
scanf ("%s",b);
atoA (b);
phang = 70;
plie = 60;
st = 0;
k = 0;
h_tmp = h;
while (h_tmp--)
{
if (st != 0)
break;
st = hang(a[k],b,k + 1);
st = lie(a[k],b,k + 1,h);
k++;
}
xie(a,b,h,l);
printf ("%d %d\n",phang,plie);
}
if (i < n - 1)
printf ("\n");
}
return 0;
}
void atoA(char *p)
{
while (*p != '\0')
{
if (*p >= 'a' && *p <= 'z')
*p -= 32;
p++;
}
}
void fuz(int hang,int lie)
{
if (phang > hang)
{
phang = hang;
plie = lie;
}else if (phang == hang)
{
if (plie > lie)
{
plie = lie;
}
}
}
int hang(char *a,char *b,int h)
{
char *p1 = a,*p2 = b;
char *tmp = NULL;
int num = 0;
while (*p1 != '\0')
{
num++;
p2 = b;
if (*p1 == *p2)
{
tmp = p1;
while (*p2 != '\0' && *tmp != '\0')
{
if (*tmp != *p2)
{
p2 = b;
break;
}
p2++;
tmp++;
}
if (p2 != b && *p2 == '\0')
{
fuz (h,num);
return 1;
}
}
p1++;
}
p1--;
num++;
while (p1 != (a - 1))
{
num--;
p2 = b;
if (*p1 == *p2)
{
tmp = p1;
while (*p2 != '\0' && tmp != (a - 1))
{
if (*tmp != *p2)
{
p2 = b;
break;
}
p2++;
tmp--;
}
if (p2 != b && *p2 == '\0')
{
fuz (h,num);
return 1;
}
}
p1--;
}
return 0;
}
int lie(char *ap,char *b,int h,int s)
{
char *p1 = ap,*p2 = b;
char *tmp = NULL;
int num = 0;
while (*p1 != '\0')
{
num++;
p2 = b;
if (*p1 == *p2)
{
tmp = p1;
while (*p2 != '\0' && *tmp != '\0')
{
if (*tmp != *p2)
{
p2 = b;
break;
}
p2++;
tmp += sizeof(char) * 60;
}
if (p2 != b && *p2 == '\0')
{
fuz (h,num);
return 1;
}
}
p1++;
}
p1 = ap;
p2 = b;
num = 0;
while (*p1 != '\0')
{
num++;
p2 = b;
if (*p1 == *p2)
{
tmp = p1;
while (*p2 != '\0' && tmp >= a[0])
{
if (*tmp != *p2)
{
p2 = b;
break;
}
p2++;
tmp -= sizeof(char) * 60;
}
if (p2 != b && *p2 == '\0')
{
fuz (h,num);
return 1;
}
}
p1++;
}
return 0;
}
int xie(char a[70][60],char *b,int h,int l)
{
int hang = 0,lie = 0;
char *p1 = NULL,*tmp = NULL;
char *p2 = b;
int ht = 0,lt = 0;
while (hang < h)
{
lie = 0;
while (a[hang][lie] != '\0')
{
p2 = b;
if (a[hang][lie] == *p2)
{
ht = hang;
lt = lie;
while (*p2 != '\0' && a[ht][lt] != '\0' && &a[ht][lt] >= &a[0][0])
{
if (a[ht][lt] != *p2)
{
p2 = b;
break;
}
p2++;
ht++;
lt++;
}
if (*p2 == '\0' && &a[ht][lt] >= &a[0][0])
{
fuz (hang + 1,lie + 1);
return 1;
}
}
lie++;
}
hang++;
}
hang = 0;
while (hang < h)
{
lie = 0;
while (a[hang][lie] != '\0')
{
p2 = b;
if (a[hang][lie] == *p2)
{
ht = hang;
lt = lie;
while (*p2 != '\0' && a[ht][lt] != '\0' && &a[ht][lt] >= &a[0][0])
{
if (a[ht][lt] != *p2)
{
p2 = b;
break;
}
p2++;
ht++;
lt--;
}
if (*p2 == '\0' && &a[ht][lt] >= &a[0][0])
{
fuz (hang + 1,lie + 1);
return 1;
}
}
lie++;
}
hang++;
}
hang = 0;
while (hang < h)
{
lie = 0;
while (a[hang][lie] != '\0')
{
p2 = b;
if (a[hang][lie] == *p2)
{
ht = hang;
lt = lie;
while (*p2 != '\0' && a[ht][lt] != '\0' && &a[ht][lt] >= &a[0][0])
{
if (a[ht][lt] != *p2)
{
p2 = b;
break;
}
p2++;
ht--;
lt++;
}
if (*p2 == '\0')
{
fuz (hang + 1,lie + 1);
return 1;
}
}
lie++;
}
hang++;
}
hang = 0;
while (hang < h)
{
lie = 0;
while (a[hang][lie] != '\0')
{
p2 = b;
if (a[hang][lie] == *p2)
{
ht = hang;
lt = lie;
while (*p2 != '\0' && a[ht][lt] != '\0' && &a[ht][lt] >= &a[0][0])
{
if (a[ht][lt] != *p2)
{
p2 = b;
break;
}
p2++;
ht--;
lt--;
}
if (*p2 == '\0')
{
fuz (hang + 1,lie + 1);
return 1;
}
}
lie++;
}
hang++;
}
}