Clean JavaScript Backtracking and Dynamic Programming solutions
Dynamic Programming\n\n\nconst knightProbability = (N, K, r, c) => {\n  const dirs = [[-2, -1], [-1, -2], [1, -2], [2, -1], [2, 1], [1, 2], [-1, 2], [-2, 1]];\n
**Dynamic Programming**\n\n```\nconst knightProbability = (N, K, r, c) => {\n  const dirs = [[-2, -1], [-1, -2], [1, -2], [2, -1], [2, 1], [1, 2], [-1, 2], [-2, 1]];\n  const dp = [...Array(K + 1)].map(() => [...Array(N)].map(() => Array(N).fill(0)));\n  dp[0][r][c] = 1;\n  for (let k = 1; k <= K; k++) {\n    for (let i = 0; i < N; i++) {\n      for (let j = 0; j < N; j++) {\n        for (const [dx, dy] of dirs) {\n          const x = i + dx;\n          const y = j + dy;\n          if (x >= 0 && x < N && y >= 0 && y < N) {\n            dp[k][i][j] += dp[k - 1][x][y] / 8;\n          }\n        }\n      }\n    }\n  }\n  let res = 0;\n  for (let i = 0; i < N; i++) {\n    for (let j = 0; j < N; j++) {\n      res += dp[K][i][j];\n    }\n  }\n  return res;\n};\n```\n\n**Backtracking**\n\n```\nconst knightProbability = (N, K, r, c) => {\n  const dirs = [[-2, -1], [-1, -2], [1, -2], [2, -1], [2, 1], [1, 2], [-1, 2], [-2, 1]];\n\n  const dp = [...Array(K + 1)].map(() => [...Array(N)].map(() => Array(N).fill(0)));\n  dp[0][r][c] = 1;\n\n  for (let k = 1; k <= K; k++) {\n    for (let i = 0; i < N; i++) {\n      for (let j = 0; j < N; j++) {\n        for (const [dx, dy] of dirs) {\n          const x = i + dx;\n          const y = j + dy;\n          if (x >= 0 && x < N && y >= 0 && y < N) {\n            dp[k][i][j] += dp[k - 1][x][y] / 8;\n          }\n        }\n      }\n    }\n  }\n\n  let res = 0;\n  for (let i = 0; i < N; i++) {\n    for (let j = 0; j < N; j++) {\n      res += dp[K][i][j];\n    }\n  }\n  return res;\n};\n```\n\n
['Dynamic Programming', 'Backtracking', 'JavaScript']