P1002 [NOIP2002 普及组] 过河卒 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
1.DFS做超时40分
n, m, x, y = map(int,input().split())
flag = [[0]*(n+10) for _ in range(m+10)]
maps = [[0]*(n+10) for _ in range(m+10)]
d = [[2,1],[2,-1],[-2,1],[-2,-1],[1,2],[1,-2],[-1,2],[-1,-2],[0,0]]
for xx,yy in d:
dx = x+xx
dy = y+yy
if 0<=dx<=n and 0<=dy<=m:
maps[dx][dy] = 1
flag[0][0] = 1
ans = 0
dd = [[0,1],[1,0]]
def dfs(sx,sy):
global ans
if sx == n and sy == m:
ans += 1
return
for xx, yy in dd:
dx = sx+xx
dy = sy+yy
if 0<=dx<=n and 0<=dy<=m:
if flag[dx][dy] == 0 and maps[dx][dy] == 0:
flag[dx][dy] = 1
dfs(dx,dy)
flag[dx][dy] = 0
dfs(0,0)
print(ans)
2.DP做100分
n, m, x, y = map(int,input().split())
dp = [[0]*(n+10) for _ in range(m+10)]
flag = [[0]*(n+10) for _ in range(m+10)]
d = [[2,1],[2,-1],[-2,1],[-2,-1],[1,2],[1,-2],[-1,2],[-1,-2]]
for xx,yy in d:
dx = x+xx
dy = y+yy
if 0<=dx<=n and 0<=dy<=m:
flag[dx][dy] = 1
flag[x][y] = 1
flag[0][0] = 1
dp[0][0] = 1
for i in range(n+1):
for j in range(m+1):
if flag[i][j] == 0:
if i == 0:
dp[i][j] = dp[i][j-1]
else:
if j == 0:
dp[i][j] = dp[i-1][j]
else:
dp[i][j] = dp[i-1][j] + dp[i][j-1]
print(dp[n][m])