读书人

单词博弈-这个代码哪里有有关问题

发布时间: 2013-12-22 15:06:55 作者: rapoo

单词博弈-这个代码哪里有问题?
庞果网单词博弈的题,我在VS2012上跑了,都没有问题,似乎没有超过3秒。结果提交时说是超过3秒失败。
想请高手看看,到底是哪里的问题?
题目如下地址:
http://hero.pongo.cn/Question/Details?ID=168&ExamID=163
附上我的代码:



#include<stdio.h>
#include <string.h>

#define MAX_LEN 15

static int g_bestPick;

int Eveluate(char * word)
{
char * p = word;
char chPrev = *p;

if((p == NULL) || (chPrev == 0))
{
return -10000 - MAX_LEN;
}

/* 只有1个字符,认为是升序 */
p++;
if(*p == 0)
{
return -10000 - MAX_LEN;
}
else
{
while(*p != 0)
{
if(*p <= chPrev)
{
return 0;
}
else
{
chPrev = *p;
p++;
}
}

return -10000 - MAX_LEN;
}
}

int Think(char * word, int iDepth)
{
int iState;
int i;
char newChar[MAX_LEN + 1] = "";
char oldChar[MAX_LEN + 1] = "";
int iLength;
int iScore;
int iCurrent = -10000 - MAX_LEN;

iState = Eveluate(word);

if(iState != 0)
{
return iState + iDepth;
}

/* 否则游戏还未结束,继续游戏 */
/* 查找所有步数,就是字符串长度 */
iLength = (int)strlen(word);
for(i=0; i<iLength; i++)
{
/* 保存原来字串 */
//strcpy(oldChar, word);
/* 构造新字符串,即删除第i个字符 */
strcpy(newChar, "");
strncpy(newChar, word, i);
newChar[i] = 0;
strcat(newChar, word + i + 1);
/* 继续判断结果,如果找到了胜利的走法,那么保存 */
iScore = -Think(newChar, iDepth + 1);
/* 恢复原来的字串 */
//strcpy(word, oldChar);
if(iScore > iCurrent)
{
iCurrent = iScore;
/* 到了根部,保存最好做法 */
if(iDepth == 0)
{
g_bestPick = i;
}
/* 赢了,则不需再深度搜索 */
if((iScore) >= 10000)
{
break;
}
}
}

return iCurrent;
}

int who (char* word)
{
char str[MAX_LEN + 1];
//strcpy(str, word);
if(Think(word, 0) <= -10000)
{
return 0;
}


else
{
return 1;
}
}
//start 提示:自动阅卷起始唯一标识,请勿删除或增加。
int main()
{
char str1[16];
str1[0]=0;
str1[1]='a';
strcpy(str1, "");
strncpy(str1, "aaa", 1);
str1[1]=0;
printf("%d\n",who("Test"));
printf("%d\n",who("bad"));
printf("%d\n",who("aaa"));
printf("%d\n",who(str1));
printf("%d\n",who("ponmlkjihgfedcb"));
printf("%d\n",who("ba"));
printf("%d\n",who("123456789"));
}
//end //提示:自动阅卷结束唯一标识,请勿删除或增加。

读书人网 >C语言

热点推荐