头疼的题,代码不简洁,不知道有没好的方法,求交流!

题目链接:http://acm.sdibt.edu.cn:8080/judge/contest/view.action?cid=397#problem/B

我的代码:

#include <stdio.h>
#include <string.h>
char word1[60][60],word2[100];
int T,m,n,wnumber;
int right(int i,int j)//用了八个函数验证各个方向
{
int h,k;
if(n-j<strlen(word2))
return 0;
else
{
for(k=j,h=0;h<strlen(word2);k++,h++)
{ 
if(word1[i][k]==word2[h]||word1[i][k]+32==word2[h]||
word1[i][k]-32==word2[h])
   ;
else
return 0;
}
 return 1;
}
}

int left(int i,int j)
{
int h,k;
if(j+1<strlen(word2))
	return 0;
else
{
for(k=j,h=0;h<strlen(word2);k--,h++)
	{ 
if(word1[i][k]==word2[h]||word1[i][k]+32==word2[h]||
word1[i][k]-32==word2[h])
	;
else
	return 0;
}
return 1;
}
}
int up(int i,int j)
{
int h,k;
if(i+1<strlen(word2))
	return 0;
else
{
for(k=i,h=0;h<strlen(word2);k--,h++)
{
if(word1[k][j]==word2[h]||word1[k][j]+32==word2[h]
||word1[k][j]-32==word2[h])
;
else
return 0;
}
return 1;
}
}
int down(int i,int j)
{
int h,k;
if(m-i<strlen(word2))
	return 0;
else
{
for(k=i,h=0;h<strlen(word2);k++,h++)
{
	if(word1[k][j]==word2[h]||word1[k][j]+32==word2[h]
||word1[k][j]-32==word2[h])
;
else
return 0;
}
return 1;
}
}
int upleft(int i,int j)
{
int h,k,t;
if(i+1>=strlen(word2)&&j+1>=strlen(word2))
{
for(k=i,t=j,h=0;h<strlen(word2);k--,t--,h++)
{
if(word1[k][t]==word2[h]||word1[k][t]+32==word2[h]
||word1[k][t]-32==word2[h])
	;
else
	return 0;
}
return 1;
}
else
return 0;

}
int upright(int i,int j)
{
int h,k,t;
if(i+1>=strlen(word2)&&n-j>=strlen(word2))
{
for(k=i,t=j,h=0;h<strlen(word2);k--,t++,h++)
{
if(word1[k][t]==word2[h]||word1[k][t]+32==word2[h]
||word1[k][t]-32==word2[h])
	 ;
 else
return 0;
}
return 1;
}
else
return 0;
}
int downleft(int i,int j)
{
int h,k,t;
if(m-i>=strlen(word2)&&j+1>=strlen(word2))
{
for(k=i,t=j,h=0;h<strlen(word2);k++,t--,h++)
{
if(word1[k][t]==word2[h]||word1[k][t]+32==word2[h]
||word1[k][t]-32==word2[h])
	 ;
else
	return 0;
}
return 1;
}
else
return 0;
}
int downright(int i,int j)
{
int h,k,t;
if(m-i>=strlen(word2)&&n-j>=strlen(word2))
{
for(k=i,t=j,h=0;h<strlen(word2);k++,t++,h++)
{
if(word1[k][t]==word2[h]||word1[k][t]+32==word2[h]
||word1[k][t]-32==word2[h])
	;
else
	return 0;
}
return 1;
}
else
return 0;
}
int main()
{   
int T,i,j,k,h,r,flage;
scanf("%d",&T);
for(i=0;i<T;i++)
{        if(i!=0)
printf("\n");
memset(word1,0,sizeof(word1));
memset(word2,0,sizeof(word2));
scanf("%d%d",&m,&n);
getchar();
for(j=0;j<m;j++)
gets(word1[j]);
scanf("%d",&wnumber);
getchar();
for(k=1;k<=wnumber;k++)
{
 gets(word2);
flage=0;
for(h=0;h<m;h++)
{
for(r=0;r<n;r++)
if(word1[h][r]==word2[0]||word1[h][r]-32==word2[0]
||word1[h][r]+32==word2[0])
{
if(left(h,r)||right(h,r)||up(h,r)||down(h,r)||upleft(h,r)
||upright(h,r)||downleft(h,r)||downright(h,r)) 
{ 
printf("%d %d\n",h+1,r+1);
flage=1;
break;
}
}
if(flage)
break;
}
}
}
return 0;
}

发表评论

电子邮件地址不会被公开。

Post Navigation