我想用Python 3解決以下問題:https://www.hackerrank.com/contests/projecteuler/challenges/euler015。python中自上而下的動態編程
以下是我的代碼:
def dp(x,y):
global MEMO,N,M
if x>N or y>M:return 0
if x==N and y==M:
return 1
if MEMO[x][y]!=-1:return MEMO[x][y]
MEMO[x][y]=(dp(x+1,y)+dp(x,y+1))%1000000007
return MEMO[x][y]
MEMO=[[-1]]
N=0
M=0
tc=int(input())
while tc:
tc-=1
N,M=map(int,input().split())
MEMO=[[-1]*(M+1)]*(N+1)
print(dp(0,0))
然而,對於給定的測試用例,它給了答案3(不是6)。我認爲這與本地/全球範圍內的變量範圍有關。我哪裏錯了?