初始化棋盘
m, n = 8, 8
matrics = [['空地' for i in range(m)] for i in range(n)]
matrics[0][2], matrics[2][2], matrics[5][5], matrics[6][7] = 0, 0, 0, 0
def count_path(start_x, start_y):
# 声明状态变量,用于记忆。这里是二维棋盘,所以用二维数组存储。
opt = [[0 for i in range(m)] for i in range(n)]
for i in range(m-1, start_x-1, -1):
for j in range(n-1, start_y-1, -1):
# 不考虑石头在最低行和最右列的情况
if i == m-1 or j == n-1:
opt[i][j] = 1
else:
# 如果这个位置是空地,则说明可以行走
if matrics[i][j] == '空地':
# 递推公式
opt[i][j] = opt[i+1][j] + opt[i][j+1]
else:
opt[i][j] = 0
print(opt)
return opt[start_x][start_y]