请计算n*m的棋盘格子(n为横向的格子数,m为竖向的格子数)从棋盘左上角出发沿着边缘线从左上角走到右下角,总共有多少种走法,要求不能走回头路,即:只能往右和往下走,不能往左和往上走。
注:沿棋盘格之间的边缘线行走
数据范围:
a = list(map(int,input().split())) def loop(location): if not location[0]&nbs***bsp;not location[1]: return 1 else: return loop([location[0] -1,location[1]]) + loop([location[0],location[1]-1]) print(loop(a))
def fun(n:int,m:int): if n == 0&nbs***bsp;m == 0: return 1 else: return fun(n-1,m)+fun(n,m-1) n,m = map(int,input().split()) print(fun(n,m))
get_str = input().split(' ') n, m = int(get_str[0]), int(get_str[1]) # (n+1)*(m+1)的二维dp数组 dp = [[0 for _ in range(m+1)] for _ in range(n+1)] for i in range(n+1): for j in range(m+1): # dp数组初始化 # 原点时0种走法 if i==0 and j==0: dp[i][j] = 0 # 从原点向下走一步共1种走法 # elif i==0 and j==1: # 这里可以考虑优化?当退到左边界或上边界时仅剩一种走法 elif i==0: dp[i][j] = 1 # 从原点向右走一步共1种走法 # elif i==1 and j==0: elif j==0: dp[i][j] = 1 # [i][j]点的走法=左边一个点走法+上面一个点的走法 # 即:dp[i][j] = dp[i-1][j] + dp[i][j-1] else: dp[i][j] = dp[i-1][j] + dp[i][j-1] print(dp[n][m])
''' Tag: 动态规划 ''' def walk_solutions(n, m): n += 1 m += 1 dp = [[0 for _ in range(n)] for _ in range(m)] for i in range(1, m): for j in range(1, n): if i == 1: dp[i][j] = j + 1 elif j == 1: dp[i][j] = i + 1 else: dp[i][j] = dp[i - 1][j] + dp[i][j - 1] return dp[i][j] n, m = map(int, input().split()) print(walk_solutions(n, m))
while 1: try: m,n = map(int,input().split()) dp=[[0 for i in range(n+1)] for j in range(m+1)] for j in range(1,n+1): dp[0][j]=1 for i in range(1,m+1): dp[i][0]=1 for i in range(1,m+1): for j in range(1,n+1): dp[i][j]=dp[i-1][j]+dp[i][j-1] print(dp[m][n]) except: break
#参考:https://leetcode.cn/problems/unique-paths/solution/dai-ma-sui-xiang-lu-dong-gui-wu-bu-qu-xi-1vkb/ #DFS m,n = map(int,input().split(" ")) directions=[(1,0),(0,1)] def dfs(x,y): # pass if x > m&nbs***bsp;y > n: return 0 if x == m and y == n: return 1 return dfs(x+1,y)+dfs(x, y+1) res = dfs(0,0) print(res) #DP n,m = map(int,input().split(" ")) n=n+1 m=m+1 dp=[[0]*m for _ in range(n)] for j in range(m): dp[0][j] = 1 for i in range(n): dp[i][0] = 1 for i in range(1,n): for j in range(1,m): dp[i][j]= dp[i-1][j]+ dp[i][j-1] print(dp[-1][-1])
s = input().split(" ") l = list(map(int,s)) a=l[0] b=l[1] ll = [] for i in range(0, l[0]+1): ll.append([1]) for j in range(0, l[1]): ll[i].append(0) for i in range(0, b+1): ll[0][i]=1 for i in range(1,a +1): #hang for j in range(1, b+ 1): #lie ll[i][j]=ll[i][j-1]+ll[i-1][j] print(ll[a][b])
m,n=input().split(' ') d=1 s=1 for i in range(int(m)+1,int(m)+int(n)+1): d=d*i for i in range(1,int(n)+1): s=s*i print(round(d/s))
#容易想到的是,在第一步时,可以选择向右或者向下, #只需要当前的路径选择上加上(n,m−1)(n,m-1)(n,m−1)和(n−1,m)(n-1,m)(n−1,m)的矩阵路径选择即可。而(n,m−1)(n,m-1)(n,m−1)与(n−1,m)(n-1,m)(n−1,m)又是(n,m)(n,m)(n,m)的子问题, #可以继续递归下去。 #只需要用iii,jjj表示当前位置,后续限制边界的向下或者向右即可。 def func(x,y): if x < 0&nbs***bsp;y < 0: return 0 elif x == 0&nbs***bsp;y == 0: return 1 else: return func(x-1, y)+func(x, y-1) while True: try: a,b = map(int,input().split()) c = func(a, b) print(c) except: break
# 输入行数和列数 n,m=map(int,input().split(" ")) # 边界条件 # 当只有一行的时候 if m ==1: print(n+1) # 当只有一列的时候 elif n ==1: print(m+1) else: # 构建dp数组 dp=[[1]*(n+1) for _ in range(m+1)] # print(dp) dp[0][1]=1 dp[1][0]=1 for i in range(1,m+1): for j in range(1,n+1): # 状态方程 # 表示达到第i行第j列的方法 dp[i][j]=dp[i-1][j] +dp[i][j-1] print(dp[m][n])