使用memoization但仍然代码永远运行

时间:2012-10-01 18:16:04

标签: python c algorithm recursion memoization

我正在尝试解决SPOJ问题“Cricket Tournament”。我在python和c中编写了代码。在python中,输入0.0 0/0 300需要大约2秒。但是在C中它会永远运行。 C中的代码正在运行一些较小的测试用例,如19.5 0/0 1

Code in C

#include<stdio.h>
float ans[10][120][300]={0};
float recursion(int balls, int reqRuns, int wickets);
int readScore(void);

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(ans,0,sizeof(ans));
        float overs;
        int myruns,wickets,target;
        scanf("%f",&overs);
        myruns=readScore();
        scanf("%d",&wickets);
        //printf("%d %d\n",myruns,wickets );
        scanf("%d",&target);
        //printf("%d %d %d\n",myruns,wickets,target);
        if(myruns>=target)
        {
            printf("%s\n","100.00");
            continue;
        }
        else if(wickets>=10)
        {
            printf("%s\n", "0.00");
            continue;
        }
        printf("overs = %f\n", overs);
        int ov = (int) overs;
        int ball = (int)(overs*10)%10;
        int totballs = 6*ov+ball;
        //printf("%d %d\n",ov,ball );
        //printf("%d %d %d\n",totballs, target- myruns,wickets );
        float finalAns = recursion(totballs,target-myruns, wickets)*100;
        printf("%.2f\n",finalAns);

    }
    return 0;
}

int readScore()
{
    char ch;
    int ans2=0;
    ch = getchar();
    //ch = getchar();
    //ans = ans*10 + ch-'0';
    //printf("sadasdas %d\n",ch );
    while(ch!='/')
    {
        ch=getchar();
        //printf(" ch = %d\n", ch-'0');
        if(ch!='/')
        ans2 = ans2*10 + ch-'0';

    }
    //printf("%d\n",ans );
    return ans2;
}

float recursion(int balls, int reqRuns, int wickets)
{
    if (reqRuns<=0)
        return 1;
    if (balls==120||wickets==10)
        return 0;
    if(ans[wickets][balls][reqRuns]!=0)
        return ans[wickets][balls][reqRuns];

    ans[wickets][balls][reqRuns] = (recursion(balls+1, reqRuns,wickets)+recursion(balls+1, reqRuns-1,wickets)+
    recursion(balls+1, reqRuns-2,wickets)+recursion(balls+1, reqRuns-3,wickets)+
    recursion(balls+1, reqRuns-4,wickets)+recursion(balls+1, reqRuns-5,wickets)+
    recursion(balls+1, reqRuns-6,wickets)+recursion(balls+1, reqRuns,wickets+1)+
    2*recursion(balls, reqRuns-1,wickets))/10;
    return ans[wickets][balls][reqRuns];
}

Code in Python

from __future__ import division

saved = {}
t = input()

def func(f):
    if f in saved:    return saved[f]
    x,y,z,n = f 
    if z >= n:    return 1
    if x == 120:    return 0 
    if y == 10:    return 0

    saved[f] = (func((x+1,y+1,z,n)) + func((x+1, y,z,n)) + func((x+1,y,z+1,n)) + func((x+1, y, z+2,n)) + func((x+1, y, z+3,n)) + func((x+1, y, z+4,n)) + func((x+1, y, z+5,n))+ func((x+1, y, z+6,n))+ func((x,y,z+1,n)) + func((x,y,z+1,n))) / 10
    return saved[f]

def converter(f):
    v = f.index('.')
    x,y = int(f[:v]), int(f[-1])
    return x*6+(y)

for i in range(t):
    x,y,z = raw_input().split()
    v = y.index('/')
    q = int(y[:v])
    x,y,z = converter(x), int(y[(v+1):]), int(z)
    print  '%.2f' % (100 * func((x,y,q,z)))

3 个答案:

答案 0 :(得分:3)

你的问题是很多递归的结果是0,所以

if(ans[wickets][balls][reqRuns]!=0)
    return ans[wickets][balls][reqRuns];
在许多情况下,

无法返回缓存的结果,因此您重新计算许多结果,而Python中的检查f in saved会阻止重新计算相同的值。

我更改了您的C代码,将ans的初始条目设置为包含负数(如果您知道您的平台的浮点表示为IEEE754,只需更改为memset(ans, 0x80, sizeof ans);即可),并用

替换了条件
if (ans[wickets][balls][reqRuns] >= 0)

立即得到结果:

$ time ./a.out  < spoj_inp.txt 
overs = 0.000000
18.03

real    0m0.023s
user    0m0.020s
sys     0m0.002s

答案 1 :(得分:0)

问题在于您使用scanf。它将空格或换行视为输入的终止符。您很可能在每次输入后输入enter。但问题是它将\ n留在缓冲区中并传递给下一个输入。

如果你没有使用严格的c,你可以打电话

cin.ignore()

每次scanf调用后

。我在你的代码上尝试了它并且能够获得成功的输出。

或者,您可以致电

fflush(stdin);

这也可能有用

scanf at stackoverflow

答案 2 :(得分:0)

我猜这次递归是值得责备的。代码适用于较小的目标。如果可能的话,摆脱递归。

目标较小:

输入

2
0.0 0/1 10
0.0 2/2 20

输出

100.00
99.99