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. Scrum会议博客以及测试报告(β阶段)

    3组Alpha冲刺阶段博客目录 一.Scrum Meeting1. [第十周会议记录](链接地址:https://www.cnblogs.com/Cherrison-Time/articles/120 ...

  2. java基础(10)---stream流

    一.stream的应用场景 for遍历的冗余场景:  stream的写法: 二.获取Stream流的常用方式 三.Stream的map映射方法 更简单的写法: 四.Stream的filter过滤方法 ...

  3. 51nod 1396 还是01串

    给定一个0-1串s,长度为n,下标从0开始,求一个位置k,满足0<=k<=n, 并且子串s[0..k - 1]中的0的个数与子串s[k..n - 1]中1的个数相等. 注意: (1) 如果 ...

  4. ldconfig 让安装的 php 的rdkafka生效

    原文:https://www.cnblogs.com/schips/p/10183111.html linux中ldconfig的使用介绍   ldconfig是一个动态链接库管理命令,其目的为了让动 ...

  5. 10分钟学会 linux awk命令

    简介 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再 ...

  6. LINQ查询表达式(2) - 在 C# 中编写 LINQ 查询

    在 C# 中编写 LINQ 查询 C# 中编写 LINQ 查询的三种方式: 使用查询语法. 使用方法语法. 组合使用查询语法和方法语法. // 查询语法 IEnumerable<int> ...

  7. Yum 安装memcached 与缓存清空

    1.安装 root@pts/0 # yum -y install memcached 2.启动服务         root@pts/0 # /etc/init.d/memcached start 3 ...

  8. C# Apache Thrift Demo

    转载至 https://headsigned.com/posts/csharp-apache-thrift-demo/ This demo application shows how to imple ...

  9. 七.搭建基本的FTP服务

    1.安装vsftpd软件包 ]# yum -y install vsftpd 2.重起vsftpd服务 ]# systemctl restart vsftpd ]# systemctl enable ...

  10. Notepad++ 编译运行java,c,c++

    1.Java NPP_SAVE cd $(CURRENT_DIRECTORY) D:\tibco\bw6\tibcojre64\1.8.0\bin\javac.exe "$(FILE_NAM ...