# 688. Knight Probability in Chessboard

**<font color=red>难度: Medium</font>**

## 刷题内容

> 原题连接

* https://leetcode.com/problems/knight-probability-in-chessboard/

> 内容描述

```
On an NxN chessboard, a knight starts at the r-th row and c-th column and attempts to make exactly K moves. The rows and columns are 0 indexed, so the top-left square is (0, 0), and the bottom-right square is (N-1, N-1).

A chess knight has 8 possible moves it can make, as illustrated below. Each move is two squares in a cardinal direction, then one square in an orthogonal direction.

 



 

Each time the knight is to move, it chooses one of eight possible moves uniformly at random (even if the piece would go off the chessboard) and moves there.

The knight continues moving until it has made exactly K moves or has moved off the chessboard. Return the probability that the knight remains on the board after it has stopped moving.

 

Example:

Input: 3, 2, 0, 0
Output: 0.0625
Explanation: There are two moves (to (1,2), (2,1)) that will keep the knight on the board.
From each of those positions, there are also two moves that will keep the knight on the board.
The total probability the knight stays on the board is 0.0625.
 

Note:

N will be between 1 and 25.
K will be between 0 and 100.
The knight always initially starts on the board.
```

## 解题方案

> 思路 1
******- 时间复杂度: O(K * N^2)******- 空间复杂度: O(K * N^2)******

典型DFS, beats 51.69%

```python
class Solution:
    def knightProbability(self, N, K, r, c):
        """
        :type N: int
        :type K: int
        :type r: int
        :type c: int
        :rtype: float
        """
        self.dires = [(1,2), (1,-2), (2,1), (2,-1), (-1,2), (-1,-2), (-2,1), (-2,-1)]
        self.cache = {}
        def dfs(step, x, y):
            if not (0 <= x < N and 0 <= y < N):
                return 0
            if step == K:
                return (1 / 8) ** step
            res = 0
            for dx, dy in self.dires:
                x_next, y_next = x + dx, y + dy
                if (x_next, y_next, step+1) not in self.cache:
                    self.cache[(x_next, y_next, step+1)] = dfs(step+1, x_next, y_next)
                res += self.cache[(x_next, y_next, step+1)]
            return res
        return dfs(0, r, c)
```




> 思路 2
******- 时间复杂度: O(K * N^2)******- 空间复杂度: O(N^2)******

刚才递归思路的空间复杂度为O(K * N^2), 是因为我们的step总共有K步，(step, r, c)的组合有K * N^2种，我们可以用dp的思路来减小空间复杂度

- cur_dp[r][c] 代表 第n步时候，我们处于(r, c)这个点继续往下走不出界的概率
- dp[r][c] 代表 第n-1步时候，我们处于(r, c)这个点继续往下走不出界的概率

这样一直循环K次，我们的dp代表的就是走K步之后所有点继续走不出界的概率，求和即可得到总概率

beats 36.23%

```python
class Solution:
    def knightProbability(self, N, K, r, c):
        """
        :type N: int
        :type K: int
        :type r: int
        :type c: int
        :rtype: float
        """
        self.dires = [(1,2), (1,-2), (2,1), (2,-1), (-1,2), (-1,-2), (-2,1), (-2,-1)]
        dp = [[0] * N for _ in range(N)]
        dp[r][c] = 1
        for _ in range(K):
            cur_dp = [[0] * N for _ in range(N)]
            for r, row in enumerate(dp):
                for c, val in enumerate(row):
                    for dr, dc in self.dires:
                        if 0 <= r + dr < N and 0 <= c + dc < N:
                            cur_dp[r+dr][c+dc] += val / 8.0
            dp = cur_dp
        return sum(map(sum, dp))
```






























