题目:

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.

分析:

给定一个N*N大小的棋盘,求走K步之后,马还停留在棋盘上的概率,规定马走出棋盘后就不再跳回来。

那么我们知道马可以向八个方向去跳,我们可以求出跳k步之后,在棋盘上停留的位置数,也就是k步后的情况个数,而每次跳8个方向,一共有8^K中情况,最后求比值就是概率。

通过dp二维数组保存前一步马所在的位置,遍历棋盘上每一个位置,再遍历八个方向,如果符合要求,即没有跳出棋盘,就将前一步所在位置的数量加到新的数组中,然后将新数组重新赋值给dp,继续下一步的遍历即可,最后返回位置数的和除以8^K便是答案。

程序:

C++

class Solution {
public:
double knightProbability(int N, int K, int r, int c) {
vector<vector<double>> dp(N, vector<double>(N, 0.0));
dp[r][c] = 1.0;
int steps[8][2] = {{1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}, {-2, -1}, {-2, 1}, {-1, 2}};
for(int k = 0; k < K; ++k){
vector<vector<double>> temp(N, vector<double>(N, 0.0));
for(int i = 0; i < N; ++i){
for(int j = 0; j < N; ++j){
for(int l = 0; l < 8; ++l){
int x = i + steps[l][0];
int y = j + steps[l][1];
if(x < 0 || x >= N || y < 0 || y >= N)
continue;
temp[x][y] += dp[i][j];
}
}
}
swap(dp, temp);
}
double sum = 0.0;
for(int i = 0; i < N; ++i)
for(int j = 0; j < N; ++j)
sum += dp[i][j];
return sum / pow(8, K);
}
};

Java

class Solution {
public double knightProbability(int N, int K, int r, int c) {
double[][] dp = new double[N][N];
dp[r][c] = 1.0;
int[][] steps = new int[][]{{1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}, {-2, -1}, {-2, 1}, {-1, 2}};
for(int k = 0; k < K; ++k){
double[][] temp = new double[N][N];
for(int i = 0; i < N; ++i){
for(int j = 0; j < N; ++j){
for(int l = 0; l < 8; ++l){
int x = i + steps[l][0];
int y = j + steps[l][1];
if(x < 0 || x >= N || y < 0 || y >= N)
continue;
temp[x][y] += dp[i][j];
}
}
}
dp = temp;
}
double sum = 0.0;
for(int i = 0; i < N; ++i)
for(int j = 0; j < N; ++j)
sum += dp[i][j];
return sum / Math.pow(8, K);
}
}

LeetCode 688. Knight Probability in Chessboard “马”在棋盘上的概率 (C++/Java)的更多相关文章

  1. 688. Knight Probability in Chessboard棋子留在棋盘上的概率

    [抄题]: On an NxN chessboard, a knight starts at the r-th row and c-th column and attempts to make exa ...

  2. LeetCode 688. Knight Probability in Chessboard

    原题链接在这里:https://leetcode.com/problems/knight-probability-in-chessboard/description/ 题目: On an NxN ch ...

  3. LeetCode——688. Knight Probability in Chessboard

    一.题目链接:https://leetcode.com/problems/knight-probability-in-chessboard/ 二.题目大意: 给定一个N*N的棋盘和一个初始坐标值(r, ...

  4. leetcode 576. Out of Boundary Paths 、688. Knight Probability in Chessboard

    576. Out of Boundary Paths 给你一个棋盘,并放一个东西在一个起始位置,上.下.左.右移动,移动n次,一共有多少种可能移出这个棋盘 https://www.cnblogs.co ...

  5. 【LeetCode】688. Knight Probability in Chessboard 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/knight-pr ...

  6. 【leetcode】688. Knight Probability in Chessboard

    题目如下: On an NxN chessboard, a knight starts at the r-th row and c-th column and attempts to make exa ...

  7. 688. 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 ...

  8. Java实现 LeetCode 688 “马”在棋盘上的概率(DFS+记忆化搜索)

    688. "马"在棋盘上的概率 已知一个 NxN 的国际象棋棋盘,棋盘的行号和列号都是从 0 开始.即最左上角的格子记为 (0, 0),最右下角的记为 (N-1, N-1). 现有 ...

  9. [LeetCode] 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 ...

  10. [Swift]LeetCode688. “马”在棋盘上的概率 | 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 ...

随机推荐

  1. 力扣620(MySQL)-有趣的电影(简单)

    题目: 某城市开了一家新的电影院,吸引了很多人过来看电影.该电影院特别注意用户体验,专门有个 LED显示板做电影推荐,上面公布着影评和相关电影描述. 作为该电影院的信息部主管,您需要编写一个 SQL查 ...

  2. T级内存,创建效率提升10倍以上,阿里云 KVM异构虚拟机启动时间优化实践

    简介: 阿里云工程师李伟男和郭成在 KVM Forum 2020 上详细介绍了阿里云 KVM 虚拟机创建及启动时间优化的具体技术实现,本文根据其演讲整理而成. 对于云计算用户来说,过长的 KVM 虚拟 ...

  3. [PHP] 业务逻辑大内存占用的优化思路, yield 和 chunk

      示例: header("content-type:text/html;charset=utf-8"); function readTxt() { $handle = fopen ...

  4. 安全机密管理:Asp.Net Core中的本地敏感数据保护技巧

    前言 在我们开发过程中基本上不可或缺的用到一些敏感机密数据,比如SQL服务器的连接串或者是OAuth2的Secret等,这些敏感数据在代码中是不太安全的,我们不应该在源代码中存储密码和其他的敏感数据, ...

  5. dotnet C# 通过 Vortice 使用 Direct2D 的 ID2D1CommandList 入门

    本文将告诉大家如何通过 Vortice 使用 D2D 的 CommandList 功能 本文属于 DirectX 系列博客,更多 DirectX 和 D2D 以及 Vortice 库的博客,请参阅我的 ...

  6. dotnet 在 UOS 国产系统上安装 dotnet sdk 的方法

    本文告诉大家如何在 UOS 国产系统上安装 dotnet sdk 的方法 使用的 UOS 是 UOS 20 x64 版本,这个系统版本是基于 debian 10 的,可以使用 debian 10 的方 ...

  7. rails 上传文件

    控制器文件 app/controllers/api/v1/order_controller.rb def create # 从本地读取 log_dir = File.expand_path(File. ...

  8. OpenAirInterface,开源的 4G EPS 实现

    目录 文章目录 目录 前文列表 OSA OpenAirInterface OAI 的仿真 物理信道仿真 系统级仿真 OAI 的 SDR LTE 参考文档 前文列表 <USRP B210 软件定义 ...

  9. 编译mmdetection3d时,无root权限下为虚拟环境单独创建CUDA版本

    在跑一些深度学习代码的时候,如果需要使用mmdetection3d框架,下载的pytorch的cudatoolkit最好需要和本机的cuda版本是一样的,即输入nvcc -V命令后显示的版本一样. 但 ...

  10. 如此丝滑的API设计,用起来真香

    分享是最有效的学习方式. 博客:https://blog.ktdaddy.com/ 故事 工位上,小猫一边撸着代码,一边吐槽着前人设计的接口. 如下: "我艹,货架模型明明和商品SKU模型是 ...