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/ 目录 题目描述 题目大意 解题方法 动态规划 状态搜索 记忆化搜索 相似题目 参考资料 ...
随机推荐
- 使用VS.NET2019做为C++开发专用IDE
一.下载安装包: https://visualstudio.microsoft.com/zh-hans/downloads/ 我下载的是企业版本. 二.安装支持C++ 序列号: Visual Stud ...
- 思想家:潘石屹学python
1.python在一些算法,图像处理,机器视觉方面越来越重要 2.计算机语言像英语一样,渐渐成为一种非专业技术,不能成为专业,而只能成为一种工具 3.想发挥工具价值,需要与别的专业结合,例如潘总的房地 ...
- 《逆袭团队》第七次作业:团队项目设计完善&编码
实验十一 团队作业7:团队项目设计完善&编码 内容 项目 软件工程 任课教师博客主页链接 作业链接地址 团队作业7:团队项目设计完善&编码 团队名称 逆袭团队 具体目标 (1)完善团队 ...
- destoon6.0搜索页熊掌号页面改造技巧【原创】
大家都知道,DT官方是封禁搜索页的,是不让百度蜘蛛抓取的,但是搜索页又是大型网站优化的重点,今天来说说关于DT6.0搜索页熊掌号的改造方法,如果您要改造内容页面可以查看我前几期的分享! 首先要开启百度 ...
- yum 安装 出错 Error: Protected multilib versions:
例如:安装zlib出错 yum install zlib-1.2.3-29.el6.i686 Error: Protected multilib versions: zlib-1.2.3-29.el6 ...
- 列举 Python2和Python3的区别?
1.print 在python2中,print被视为一个语句而不是一个函数,python3中,print()被视为一个函数 2.整数的除法 在python2中,键入的任何不带小数的数字,将被视为整数的 ...
- 4、MapReduce思想、运行机制
MapReduce 离线计算框架 分而治之 input > map > shuffle > reduce > output 分布式并行的计算框架 将计算过程分为两个阶段,Map ...
- LeetCode 311. Sparse Matrix Multiplication
原题链接在这里:https://leetcode.com/problems/sparse-matrix-multiplication/description/ 题目: Given two sparse ...
- SQL 删除字段 增加字段
SQL增加字段需要用到sql语句 ALTER TABLE 加(表格名称) ADD 加(字段名称) 加(字段类型)实例:ALTER TABLE T_Basic ADD SEODescription Nv ...
- AtomicIntegerFieldUpdater字段原子更新类
本文链接:https://blog.csdn.net/anLA_/article/details/78662383前面讲的两个AtomicInteger和AtomicIntegerArray,这两个都 ...