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 问题描述: 问题求解: 本题本质上是个挺模板的题目.本质是一个求最后每个落点的数目,用总的数目来除有所可能生成的可能性.这种计数的问题可以使用动态规划来进行解决 ...
随机推荐
- Exception in thread "main" java.lang.NullPointerException at Class.main
出现这种问题,可能的原因之一可能是对象数组未初始化. Class[] class = new Class[N]; for(int i = 0; i < N; i++){ class[i] = n ...
- 每天一本电子书 - JavaScript for Kids: A Playful Introduction to Programming
JavaScript for Kids: A Playful Introduction to Programming 作者: Nick Morgan 出版社: No Starch Press 副标题 ...
- ECSIDE标签
ECSIDE标签之<ec:table>标签的属性说明与使用 EC side是基于jsp tag的开源列表组件,可以帮助我们快速实现墙大的列表的jsp标签.EC side可以展现列表(分 ...
- 关于grub修复引导系统
这周末遇到停电,机房的一台数据服务器启动不了,开机硬件自检以后,就停留在一个黑屏状态左上角有光标闪烁,却一直进入不了系统. 还好手里有centos6.5的系统盘,进入修复选项,具体进入修复请参照这里 ...
- 20175120彭宇辰 《Java程序设计》第六周学习总结
教材学习内容总结 第七章 一.内部类与外部类的关系 1.内部类可以使用外嵌类的成员变量和方法.2.类体中不可以声明类变量和类方法,外部类可以用内部类声明对象.3.内部类仅供外嵌类使用.4.类声明可以使 ...
- 列表中使用嵌套for循环[i*j for i in range(3) for j in range(3)]
利用嵌套for循环形成一个新列表 [i*j for i in range(3) for j in range(3)]相当于如下代码 li=[] for i in range(3): for j in ...
- python开发流程及项目目录规范
# 项目开发流程 1.调研 2.需求分析 ---产品经理 3.任务分配 ---项目经理 4.写项目demo ---项目经理.架构师.程序猿 5.架构分析 ---项目经理.架构师 6.编 ...
- 页面布局 ——图片自动按比例显示&&图片随外部div的增大而按比例增大
图片按比例显示,分为两种情况. 1.空的div内加图片 <div class="emty"><img src="img/my.png"> ...
- 锋利的jQuery初学(2)
js与jq事件处理程序区别: 1,事件源: document.getElementById('id'); $("#id") 2,事件: document.getElem ...
- nginx日志分割
mark 参考文章: https://blog.csdn.net/molaifeng/article/details/82667158