【刷题喽】机器人路径

【刷题喽】机器人路径_第1张图片

方案1:

采用递归式的动态规划算法:

def f(x,y):
    a=[[0]*y for _ in range(x)]
    return calc(x-1,y-1,a)
def calc(x,y,a):
    if x<=0:
        return 1
    if y<=0:
        return 2
    if a[x][y]!=0:
        return a[x][y]
    a[x][y]=calc(x-1,y,a)+calc(x,y-1,a)
    return a[x][y]
print(f(3,7))

采用循环式的动态规划:

def f(x,y):
    a=[[1]*y for _ in range(x)]
    for i in range(1,x):
        for j in range(1,y):
            a[i][j]=a[i-1][j]+a[i][j-1]
    return a[x-1][y-1]
print(f(3,7))

 

你可能感兴趣的:(动态规划,算法)