刚刚写了个单词争霸的递归程序,感觉判断的方法比较麻烦,求简化!
#include<stdio.h>#include<string.h>
#include<stdlib.h>
void main()
{
int play(char **dic,int *save,int who,int n);
int i,n,*save;
char **dic;
scanf("%d",&n);
save=(int*)malloc(4*n);
dic=(char**)malloc(sizeof(char*)*n);
for(i=0;i<n;i++)
{
save[i]=0;
dic[i]=(char*)malloc(100);
scanf("%s",dic[i]);
}
if(play(dic,save,0,n)==0)printf("Cannot win at all!!");
printf("\n");
}
int play(char **dic,int *save,int who,int n)
{
int panduan(char **dic,int *save,int m,int n);
int have,i,check;
if(who%2==0)check=0;
else check=1;
for(i=0,have=0;i<n;i++)
{
if(panduan(dic,save,i,n))continue;
else
{
have=1;save[i]=1;
if(who%2==0&&play(dic,save,who+1,n)!=0)check=1;
else if(who%2!=0&&play(dic,save,who+1,n)==0)check=0;
if(who==0&&check==1)printf("%s",dic[i]);
save[i]=0;
}
}
return check;
}
int panduan(char **dic,int *save,int m,int n)
{
int i,check=0;
for(i=0;i<n;i++)
{
if(save[i]==1&&strncmp(dic[m],dic[i],strlen(dic[m]))==0)
check=1;
}
return check;
}