解题思路:
观察发现:
f(i,j)=f(i−1,j)+f(i,j−1)
注意事项:
防止马的坐标出界,直接加二(单独判断也可以)
参考代码:
m,n,i,j=map(int,input().split())
# 定义马可以到达的坐标
direction=[(0,0),(-1,-2),(1,-2),(-1,2),(1,2),(2,1),(2,-1),(-2,1),(-2,-1)]
def fun(m,n,i,j):
f=[[0 for _ in range(30)] for _ in range(30)]
t=[[0 for _ in range(30)] for _ in range(30)]
m+=2
n+=2
i+=2
j+=2
f[2][1]=1
for dx,dy in direction:
t[i+dx][j+dy]=1
for c in range(2,m+1):
for d in range(2,n+1):
if t[c][d]:
continue
else:
f[c][d]=f[c-1][d]+f[c][d-1]
# for _ in f:
# print(_)
return f[m][n]
print(fun(m,n,i,j))
# fun(m,n,i,j)
0.0分
2 人评分