LeetCode——688. Knight Probability in Chessboard
一.题目链接:https://leetcode.com/problems/knight-probability-in-chessboard/
二.题目大意:
给定一个N*N的棋盘和一个初始坐标值(r,c),开始时骑士在初始坐标处,骑士会进行移动,并且骑士移动的时候这只能按照如下的移动方式:

即一共有8个可能移动的方向,并且骑士向每个方向移动的概率都是相同的(即$1/8$) 。求骑士移动K步后,还在棋盘之内的概率。
三.题解:
直观来看,这道题可有用DFS和DP,但不知道DFS会不会超超时,所以我就直接用了DP的思路去解决这个问题。具体如下:
定义一个三维double型数组$dp$,其中$dp[k][i][j]$表示骑士从坐标$(i,j)$处开始移动K步后还在棋盘内的概率。很显然,第K步的结果实收第K-1步的结果影响的,而K-1步的结果可能有8种,于是,可以写出状态转移方程:
$$dp[k][i][j] = 1/8(dp[k-1][i - 2][j + 1] + dp[k-1][i - 2][j - 1] + dp[k-1][i - 1][j - 2] + dp[k-1][i - 1][j + 2] + dp[k-1][i + 2][j + 1] + dp[k-1][i + 2][j - 1] + dp[k-1][i + 1][j - 2] + dp[k-1][i + 1][j + 2])$$
并且当K=0的时候,对于棋盘内所有的坐标点的,相应的概率必为1,所以初始状态的方程为:
$$ dp[0][i][j] = 1$$
好了,根据这两个方程,就可以直接写代码了:
class Solution
{
public:
double knightProbability(int N, int K, int r, int c)
{
double dp[101][26][26];//dp数组,用于存储状态
int direction[][2] = {{-2,1},{-2,-1},{-1,-2},{-1,2},{2,1},{2,-1},{1,-2},{1,2}};//方向向量,表示8个可能的方向
if(K == 0)//特殊输入的判断
return 1.0;
if(r < 0 || c < 0 || c >= N || r >= N)//特殊输入的判断
return 0.0;
for(int i = 0; i < N; i++)//初始状态
for(int j = 0; j < N; j++)
dp[0][i][j] = 1.0;
for(int num = 1; num <= K; num++)//注意循环的顺序
for(int i = 0; i < N; i++)
for(int j = 0; j < N; j++)
{
double tmp = 0;
for(int l = 0; l < 8; l++)//分别计算8个可能的方向
{
int x = i + direction[l][0];
int y = j + direction[l][1];
if(x < 0 || y <0 || x >= N || y >= N)//不符合条件,则进行下次循环
continue;
tmp += (1.0 / 8.0)*dp[num - 1][x][y];
} dp[num][i][j] = tmp;
}
return dp[K][r][c];//返回最终的结果 }
};
该算法的时间复杂度为O(k*N^2),空间复杂度为O(K*N^2),应该可以继续优化的,但不想继续做了....
此外,有几点需要注意的是:
1.最外层循环必须是K,这样的话才能保证,在求父问题的时候,相应的子问题已经求出结果了。
2.要对每种可能的方向,判断他们之前是不是还在棋盘内。
LeetCode——688. Knight Probability in Chessboard的更多相关文章
- LeetCode 688. Knight Probability in Chessboard
原题链接在这里:https://leetcode.com/problems/knight-probability-in-chessboard/description/ 题目: On an NxN ch ...
- leetcode 576. Out of Boundary Paths 、688. Knight Probability in Chessboard
576. Out of Boundary Paths 给你一个棋盘,并放一个东西在一个起始位置,上.下.左.右移动,移动n次,一共有多少种可能移出这个棋盘 https://www.cnblogs.co ...
- 【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 ...
- 【LeetCode】688. Knight Probability in Chessboard 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/knight-pr ...
- 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 ...
- 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 ...
- [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 ...
- [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 ...
- Knight Probability in Chessboard
2018-07-14 09:57:59 问题描述: 问题求解: 本题本质上是个挺模板的题目.本质是一个求最后每个落点的数目,用总的数目来除有所可能生成的可能性.这种计数的问题可以使用动态规划来进行解决 ...
随机推荐
- Activity简说
Activity 四个状态 running 运行:前台显示,当前焦点 paused 暂停:上面被其他activity覆盖,有一部分可见 stopped 停止:被其他activity完全覆盖,不可见 d ...
- 2186 Popular Cows
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 41771 Accepted: 16955 De ...
- IIS Express服务器遇到400/503/IIS Express Error的解决办法
目前脑子比较乱.下午想让室友从局域网中它的主机访问我电脑上自己部署的一个网站,可是接二连三遇到了400,503,...等问题,弄得有点头大.还好现在解决了.下面记录一下遇到的问题: (不知道为什么图片 ...
- React Native - 网页组件(WebView)的使用详解
一.WebView组件介绍 使用 WebView 组件我们可以通过 url 来加载显示一个网页,也可以传入一段 html 代码来显示.下面对其主要属性和方法进行介绍. 1,属性介绍 source: ...
- javascript中的Date对象
Date是什么? Date是日期类的构造函数 也是个对象,用于构造日期对象的实例. 有一个 now()方法,返回截止目前的时间戳(1970.1.1日始). Date.parse()接受 一定格式的日期 ...
- erlang开发工具之intellij idea基本使用
其他废话就不多说了,接下来主要是介绍怎么来用idea搭建项目让我们能更好的使用好开发工具. (这边假设你已经下载好了intellij idea关于erlang的插件,如果没有安装好,请先去google ...
- Golang中的函数
函数 在go语言中,函数的基本组成为:关键字func.函数名.参数列表.返回值.函数体和返回语句 函数的定义 定义一个最简单的加法函数 func Add(a int,b int)(ret int,er ...
- oo第四次博客
一.测试与正确性论证比较 正确性论证是论证程序达到预期目的的一般性陈述,而该论证与程序输入数据的特定值无关,能够代表穷举性测试. 程序测试是指测试者特意跳出一批输入数据,通过运行程序,检查每个输入数据 ...
- CentOS 7 / RHEL 7:修改OpenSSH 默认端口
1.备份sshd_config cp -p /etc/ssh/sshd_config /etc/ssh/sshd_config.orig.$(date +%F) 2.vi /etc/ssh/sshd_ ...
- Fiddler实现手机抓包——小白入门
方法1:https://www.cnblogs.com/hzg1981/p/5610530.html 方法2:转载自:http://blog.csdn.net/gld824125233/article ...