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. Pandas进阶笔记 (0)为什么写这个系列

    使用Pandas数年之久了,从最早的0.17版本开始接触Pandas,到现在0.25版本,踩过不少坑,面对各种稀奇古怪的bug抓耳挠腮.每每想要解决bug,或者想要实现一个特定的数据操作需求,首先想到 ...

  2. substr()用法

    知识点链接:http://www.cplusplus.com/reference/string/string/substr/ 注意: std::string str2 = str.substr (po ...

  3. puppeteer UI自动化测试demo(一)

    一.简介 这个不大常见,比较常见的是selenium和weddriver: 所以就增加一个说明,来自官网的. Puppeteer 是一个 Node 库,它提供了一个高级 API 来通过 DevTool ...

  4. java.lang.RuntimeException: org.springframework.dao.DuplicateKeyException:

    java.lang.RuntimeException: org.springframework.dao.DuplicateKeyException: ### Error updating databa ...

  5. 使用Redis分布式锁处理并发,解决超卖问题

    一.使用Apache ab模拟并发压测 1.压测工具介绍 $ ab -n 100 -c 100 http://www.baidu.com/ -n表示发出100个请求,-c模拟100个并发,相当是100 ...

  6. 通过 ffmpeg 获取视频第一帧(指定时间)图片

    最近做一个上传教学视频的方法,上传视频的同时需要上传视频缩略图,为了避免用户上传的缩略图与视频内容不符,经理要求直接从上传的视频中截图视频的某一帧作为缩略图,并给我推荐了FFMPEG.FFMPEG 功 ...

  7. char[] byte[] string

    C#  byte 和 char 可以认为是等价的.但是在文本显示的时候有差异. char 占两个字节,unicode字符 1.内存转换: char转化为byte: public static byte ...

  8. php自定义函数之静态变量

    如果我想知道函数被调用了多少次怎么办?在没有学习静态变量的时候,我们没有好的办法来解决. 静态变量的特点是:声明一个静态变量,第二次调用函数的时候,静态变量不会再初始化变量,会在原值的基础上读取执行. ...

  9. AtomicIntegerFieldUpdater字段原子更新类

    本文链接:https://blog.csdn.net/anLA_/article/details/78662383前面讲的两个AtomicInteger和AtomicIntegerArray,这两个都 ...

  10. learning java 字符串常用操作

    // 字符串索引取值 "; System.)); // 字符串比较 "; "; "; System.out.println(s1.compareTo(s2)); ...