576. Out of Boundary Paths

给你一个棋盘,并放一个东西在一个起始位置,上、下、左、右移动,移动n次,一共有多少种可能移出这个棋盘

https://www.cnblogs.com/grandyang/p/6927921.html

dp表示上一次移动,所有位置的路径数;t表示的是当前移动,所有位置的路径数。然后每次用t去更新dp,即当前次移动去更新上一次移动。

每次只要超过了边界,就记录可能的路径数更新最终的结果。

class Solution {
public:
int findPaths(int m, int n, int N, int i, int j) {
int res = ;
vector<vector<int>> dp(m,vector<int>(n,));
dp[i][j] = ;
for(int k = ;k < N;k++){
vector<vector<int>> tmp(m,vector<int>(n,));
for(int r = ;r < m;r++){
for(int c = ; c < n;c++){
for(auto dir:dirs){
int x = r + dir[];
int y = c + dir[];
if(x < || x >= m || y < || y >= n)
res = (res + dp[r][c])% ;
else
tmp[x][y] = (tmp[x][y] + dp[r][c])% ;
}
}
}
dp = tmp;
}
return res;
}
private:
vector<vector<int>> dirs{{-,},{,},{,-},{,}};
};

688. Knight Probability in Chessboard

https://www.cnblogs.com/grandyang/p/7639153.html

https://www.cnblogs.com/Dylan-Java-NYC/p/7633409.html

这道题给了我们一个大小为NxN国际象棋棋盘,上面有个骑士,相当于我们中国象棋中的马,能走‘日’字,给了我们一个起始位置,然后说允许我们走K步,问走完K步之后还能留在棋盘上的概率是多少。

反过来想,走完K步棋子在board上的哪个位置呢. 反过来走, 看board上所有位置走完K步后能到初始位置(r,c)的数目和。

这个题必须用double才不会越界,有点搞不懂为什么。

class Solution {
public:
double knightProbability(int N, int K, int r, int c) {
vector<vector<double>> dp(N,vector<double>(N,));
for(int k = ;k < K;k++){
vector<vector<double>> tmp(N,vector<double>(N,));
for(int i = ;i < N;i++){
for(int j = ;j < N;j++){
for(auto dir :dirs){
int x = i + dir[];
int y = j + dir[];
if(x < || x >= N || y < || y >= N)
continue;
else
tmp[x][y] += dp[i][j];
}
}
}
dp = tmp;
}
return dp[r][c]/pow(,K);
}
private:
vector<vector<int>> dirs{{-,},{,},{-,-},{,-},{,},{-,},{,-},{-,-}};
};

leetcode 576. Out of Boundary Paths 、688. Knight Probability in Chessboard的更多相关文章

  1. 【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 ...

  2. leetcode 576. Out of Boundary Paths

    leetcode 576 题意大概就是在一个m*n的网格中,在坐标为[i,j]的网格上放一个物体,在规定时间N(t<=N)中,有多少种方法把物体移动出去.物体只能上下左右移动,一次移动一格,移动 ...

  3. 第十一周 Leetcode 576. Out of Boundary Paths (HARD) 计数dp

    Leetcode 576 给定一个二维平面, 一个球在初始位置(i,j)每次可以转移到上下左右的一格. 问在N次转移内,有多少种路径可以转移出边境. dp[i][j][k]为 在点(i,j) 已经走了 ...

  4. LeetCode 688. Knight Probability in Chessboard

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

  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

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

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

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

  9. 【LeetCode】576. Out of Boundary Paths 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 状态搜索 记忆化搜索 相似题目 参考资料 ...

随机推荐

  1. MySQL中的char与varchar详解

    mysql中char与varchar的区别: char:定长,效率高,一般用于固定长度的表单提交数据存储  :例如:身份证号,手机号,电话,密码等 varchar:不定长,效率偏低 1.varchar ...

  2. 微信小程序API~地理位置location

    (1)使用微信内置地图查看位置 wx.openLocation(Object object) 使用微信内置地图查看位置 参数 Object object 属性 类型 默认值 必填 说明 latitud ...

  3. 零基础如何学好python之变量

    想要自学python,变量(variable)是必经之路,它是学习python初始时,就会接触到的一个新的知识点,也是一个需要熟知的概念.python是一种动态类型语言,在赋值的执行中可以绑定不同类型 ...

  4. 《The One!团队》第八次作业:ALPHA冲刺(一)

    项目 内容 作业所属课程 所属课程 作业要求 作业要求 团队名称 < The One !> 作业学习目标 (1)掌握软件测试基础技术.(2)学习迭代式增量软件开发过程(Scrum) 第一天 ...

  5. IDEA使用入门设置(从入门到放弃)

      激活 1:设置JDK路径 2:设置maven环境 3:设置tomcat 4:设置快捷键与eclipse相同 4.1:设置字体大小 5:创建maven  java 工程并进行编译打包等操作 6:创建 ...

  6. 九.Protobuf3特殊类型

    Protobuf3 Any类型 Any消息类型允许您将消息作为嵌入类型,而不需要它们 .proto定义.Any包含任意序列化的消息(字节),以及一个URL,该URL充当该消息的全局唯一标识符并解析为该 ...

  7. Oracle-分析函数之排序值rank()和dense_rank()

    聚合函数RANK 和 dense_rank 主要的功能是计算一组数值中的排序值. 在9i版本之前,只有分析功能(analytic ),即从一个查询结果中计算每一行的排序值,是基于order_by_cl ...

  8. Linux下dstat的安装(适用任何版本)

    dstat下载地址:https://pan.baidu.com/s/1jHTEoWe 1.上传后,解压: 2.进入解压后的目录:cd dstat-0.7.3/ 3.make 4.make instal ...

  9. SP1825 【FTOUR2 - Free tour II】

    # \(SP1825\) 看到没有人用老师的办法,于是自己写一下思路 思路第一步:排除旧方法 首先这道题和\(4178\)不一样,因为那道题是计数,而这道题是求最值,最值有个坏处,就是对于来自相同子树 ...

  10. php函数基本语法之自定义函数

    PHP提供了功能强大的函数,但这远远满足不了需要,程序员可以根据需要自己创建函数.本节就开始学习创建函数的方法.大理石平台价格表 我们在实际开发过程当中需要有很多功能都需要反复使用到,而这些反复需要使 ...