688. Knight Probability in Chessboard
来源:互联网 发布:ubuntu设置中文环境 编辑:程序博客网 时间:2024/06/07 17:37
https://leetcode.com/problems/knight-probability-in-chessboard/description/
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.
#include<iostream>#include<algorithm>#include<vector>using namespace std;class Solution {public: int dirs[8][2] = { { 2, 1 },{ 2, -1 },{ -2, -1 },{ -2, 1 },{ 1, 2 },{ 1, -2 },{ -1, 2 },{ -1, -2 } }; bool check(int i, int j, int N) { return i >= 0 && i < N && j >= 0 && j < N; } double knightProbability(int N, int K, int r, int c) { double dp[101][25][25] = {0};//!!! dp[0][r][c] = 1; for (int step = 1; step <= K; step++) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { for (auto dir : dirs) { int x = dir[0] + i; int y = dir[1] + j; if (check(x, y, N)) { dp[step][i][j] += dp[step - 1][x][y] * 0.125; } } } } } double res = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { res += dp[K][i][j]; } } return res; }};
- 688. Knight Probability in Chessboard
- 688. Knight Probability in Chessboard
- 688. Knight Probability in Chessboard
- 688. Knight Probability in Chessboard
- Knight Probability in Chessboard
- LWC 52:688. Knight Probability in Chessboard
- [LeetCode] 688. Knight Probability in Chessboard
- leetcode 688. Knight Probability in Chessboard
- 【第十二周】688. Knight Probability in Chessboard
- (M)Dynamic Programming:688. Knight Probability in Chessboard
- Knight Probability in Chessboard问题及解法
- Leetcode——Knight Probability in Chessboard
- leetcode编程记录12 #688 Knight Probability in Chessboard
- 算法课作业系列8——Knight Probability in Chessboard
- Knight Probability in Chessboard:棋盘上计算K步之后棋子仍在棋盘上的概率
- Knights in Chessboard
- lightoj-1010-Knights in Chessboard
- LightOJ 1010 Knights in Chessboard
- 【我的Java笔记】常用类_BigDecimal
- HashMap
- 【C++】异常处理!!!
- 函数和预处理等概念扫盲
- 【C】1~n 的全排列
- 688. Knight Probability in Chessboard
- idea链接Git
- tf常用集合及其获取方式
- [leetcode]第九周作业
- Binary Tree Longest Consecutive Sequence
- jenkins 实现自动化构建(五)
- 【JavaSE_学习笔记】Integer和Character
- MyBatis简介
- PHP讲述