LeetCode //C - 688. Knight Probability in Chessboard

打印 上一主题 下一主题

主题 1709|帖子 1709|积分 5127

688. Knight Probability in Chessboard

On an n x n chessboard, a knight starts at the cell (row, column) and attempts to make exactly k moves. The rows and columns are 0-indexed, so the top-left cell is (0, 0), and the bottom-right cell is (n - 1, n - 1).
A chess knight has eight possible moves it can make, as illustrated below. Each move is two cells in a cardinal direction, then one cell 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 1:

   Input: n = 3, k = 2, row = 0, column = 0
Output: 0.06250
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.
  Example 2:

   Input: n = 1, k = 0, row = 0, column = 0
Output: 1.00000
  Constraints:



  • 1 <= n <= 25
  • 0 <= k <= 100
  • 0 <= row, column <= n - 1
From: LeetCode
Link: 688. Knight Probability in Chessboard

Solution:

Ideas:



  • Directions: The 8 possible knight moves are defined in the directions array.
  • DP Array: We maintain a DP array dp[k][j] to store the probability of being at position (i, j) after k moves.
  • Base Case: Initially, the knight is at (row, column) with probability 1.0.
  • Transition: For each move k, we update the probability at each cell (i, j) by considering all the possible previous positions from which the knight could have arrived.
  • Final Result: After k moves, the final result is the sum of the probabilities for all valid positions (i, j) on the board.
Code:

  1. double knightProbability(int n, int k, int row, int column) {
  2.     // Define the 8 possible moves of a knight
  3.     int directions[8][2] = {
  4.         {2, 1}, {2, -1}, {-2, 1}, {-2, -1},
  5.         {1, 2}, {1, -2}, {-1, 2}, {-1, -2}
  6.     };
  7.    
  8.     // dp[k][i][j] represents the probability of the knight being at (i, j) after k moves
  9.     double dp[101][25][25] = {0}; // Maximum k = 100, n = 25
  10.    
  11.     // Initial position
  12.     dp[0][row][column] = 1.0;
  13.    
  14.     // Perform k moves
  15.     for (int move = 1; move <= k; ++move) {
  16.         for (int i = 0; i < n; ++i) {
  17.             for (int j = 0; j < n; ++j) {
  18.                 // If knight is at position (i, j), check all previous 8 moves
  19.                 for (int d = 0; d < 8; ++d) {
  20.                     int prev_i = i + directions[d][0];
  21.                     int prev_j = j + directions[d][1];
  22.                     // Check if the previous position is valid
  23.                     if (prev_i >= 0 && prev_i < n && prev_j >= 0 && prev_j < n) {
  24.                         dp[move][i][j] += dp[move - 1][prev_i][prev_j] / 8.0;
  25.                     }
  26.                 }
  27.             }
  28.         }
  29.     }
  30.    
  31.     // Calculate the probability that the knight remains on the board after k moves
  32.     double result = 0.0;
  33.     for (int i = 0; i < n; ++i) {
  34.         for (int j = 0; j < n; ++j) {
  35.             result += dp[k][i][j];
  36.         }
  37.     }
  38.    
  39.     return result;
  40. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

渣渣兔

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表