leetcode 576. Out of Boundary Paths 、688. Knight Probability in Chessboard
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的更多相关文章
- 【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 576. Out of Boundary Paths
leetcode 576 题意大概就是在一个m*n的网格中,在坐标为[i,j]的网格上放一个物体,在规定时间N(t<=N)中,有多少种方法把物体移动出去.物体只能上下左右移动,一次移动一格,移动 ...
- 第十一周 Leetcode 576. Out of Boundary Paths (HARD) 计数dp
Leetcode 576 给定一个二维平面, 一个球在初始位置(i,j)每次可以转移到上下左右的一格. 问在N次转移内,有多少种路径可以转移出边境. dp[i][j][k]为 在点(i,j) 已经走了 ...
- LeetCode 688. Knight Probability in Chessboard
原题链接在这里:https://leetcode.com/problems/knight-probability-in-chessboard/description/ 题目: On an NxN ch ...
- 【LeetCode】688. Knight Probability in Chessboard 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/knight-pr ...
- LeetCode——688. Knight Probability in Chessboard
一.题目链接:https://leetcode.com/problems/knight-probability-in-chessboard/ 二.题目大意: 给定一个N*N的棋盘和一个初始坐标值(r, ...
- 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】576. Out of Boundary Paths 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 状态搜索 记忆化搜索 相似题目 参考资料 ...
随机推荐
- centos 安装vsftp 服务
安装软件yum install vsftpd -y 启动测试 # systemctl start vsftpd# netstat -nultp | grep 21tcp 0 0 0.0.0.0:21 ...
- typescript 接口
接口:用来建立某种代码约定,使得其他开发者在调用某个方法或创建新的类时必须遵循接口所定义的代码约定 在js里面没有接口这个概念,在ts里面通过两个关键字来支撑接口这个特性 interface ...
- docker版本lnmp
也不是全部的docker,比如php-fpm,这个可以用docker版. 但第三方插件就不灵活,所以原生的就好. 另外,在建设ftp服务时,以后要弃vsftpd而选用pure-ftp了. pure-f ...
- jupytext library using in jupyter notebook
目录 1. jupytext features 2. Way of using 3. usage 4. installation 1. jupytext features Jupytext can s ...
- 1210 BBS admin后台管理及侧边栏筛选个人站点
目录 昨日内容 django admin后台管理 使用 建表 用户图片的显示 MEDIA用户配置 查找照片 搭建个人站点 防盗链 新建css文件 侧边栏展示标签 定义分类栏与标签栏 定义时间栏 侧边栏 ...
- DOCclever--自动化接口测试用例
1.登录---UI模式 2.登录--代码模式
- spring依赖注入实例
依赖注入: BL public class T01BL implements Serializable { private static final Log logger = LogFactory.g ...
- vue自定义元素拖动
岗位序列拖动交换岗位 <span draggable="true" @dragstart="onDragstart($event,index,index2)&quo ...
- Yum 安装memcached 与缓存清空
1.安装 root@pts/0 # yum -y install memcached 2.启动服务 root@pts/0 # /etc/init.d/memcached start 3 ...
- K Edit Distance
Description Given a set of strings which just has lower case letters and a target string, output all ...