leetcode 576

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

求总的个数,并且每个N都是来自四个方向的N-1之和。很明显用dp做的。还是比较经典的一个dp题把。。

 class Solution {
public:
int findPaths(int m, int n, int N, int i, int j) {
int dp[][][];
for(int Ni=;Ni<=N;Ni++)
for(int mi=;mi<m;mi++)
for(int ni=;ni<n;ni++){
if(!Ni) {dp[Ni][mi][ni]==; continue;}
dp[Ni][mi][ni]=((long long) (!mi?:dp[Ni-][mi-][ni])+(mi==m-?:dp[Ni-][mi+][ni])+
(!ni?:dp[Ni-][mi][ni-])+(ni==n-?:dp[Ni-][mi][ni+]))%;
}
return dp[N][i][j];
}
};

要注意的是来自边界外面的个数全都为1,结合图像看一下就明白了。

有一个地方比较有意思,就是我看Sloution的答案,他的Sloution中没有初始Ni==0的情况,我想这样不是不能直接用嘛,因为这时候数组元素不是任意值嘛。。后来我实验了一下。。

 int dp[51][50][50]={};

原来这样可以直接初始化为0。。

学到了学到了。。比较特别。。

leetcode 576. Out of Boundary Paths的更多相关文章

  1. leetcode 576. Out of Boundary Paths 、688. Knight Probability in Chessboard

    576. Out of Boundary Paths 给你一个棋盘,并放一个东西在一个起始位置,上.下.左.右移动,移动n次,一共有多少种可能移出这个棋盘 https://www.cnblogs.co ...

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

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

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

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

  4. 【leetcode】576. Out of Boundary Paths

    题目如下: There is an m by n grid with a ball. Given the start coordinate (i,j) of the ball, you can mov ...

  5. 576. Out of Boundary Paths

    Problem statement: There is an m by n grid with a ball. Given the start coordinate (i,j) of the ball ...

  6. [LeetCode] Out of Boundary Paths 出界的路径

    There is an m by n grid with a ball. Given the start coordinate (i,j) of the ball, you can move the ...

  7. [Swift]LeetCode576. 出界的路径数 | Out of Boundary Paths

    There is an m by n grid with a ball. Given the start coordinate (i,j) of the ball, you can move the ...

  8. 【一天一道LeetCode】#257. Binary Tree Paths

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  9. 【LeetCode】257. Binary Tree Paths 解题报告(java & python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leet ...

随机推荐

  1. 設定 gpio 為 讀取用途,需注意的參數

    Schematic 解說 上面的 線路圖, R1 R2 只能有一個被接上, R3 R4 只能有一個被接上, 是使用 gpio 讀取 電壓 判斷為0 或是 1 這時的 gpio 設定,其中一個參數需設為 ...

  2. 网络知识===《图解TCP/IP》学习笔记——网络的构成要素

    首先引入网络构成要素图 图片来自<图解TCP/IP--P37> 1.通信媒介与数据链路 计算机之间通过电缆相互连接,电缆可以分为多种,包括双绞线电缆,光纤电缆,同轴电缆,串行电缆等. 图片 ...

  3. 002 Lock和synchronized的区别和使用

    转自 https://www.cnblogs.com/baizhanshi/p/6419268.html 今天看了并发实践这本书的ReentantLock这章,感觉对ReentantLock还是不够熟 ...

  4. awk处理之案例六:awk根据条件插入文本

    编译环境 本系列文章所提供的算法均在以下环境下编译通过. [脚本编译环境]Federa 8,linux 2.6.35.6-45.fc14.i686 [处理器] Intel(R) Core(TM)2 Q ...

  5. HDU 2829 Lawrence(四边形优化DP O(n^2))

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2829 题目大意:有一段铁路有n个站,每个站可以往其他站运送粮草,现在要炸掉m条路使得粮草补给最小,粮草 ...

  6. [PAT] 1143 Lowest Common Ancestor(30 分)

    1143 Lowest Common Ancestor(30 分)The lowest common ancestor (LCA) of two nodes U and V in a tree is ...

  7. 自建yum源(只演示nginx服务,其它都一样)

    (1)概述 (2)yum server端配置 1)关闭防火墙和selinux systemctl stop firewalld systemctl disable firewalld sed -ri ...

  8. python模式匹配,提取指定字段

    re匹配时分多行模式(re.M)与单行模式(rs.S),多行模式是每一行单独匹配,单行模式是把所有的行当成一行来匹配. 单行模式下.可以匹配换行符. ^$匹配所有字符 import re s='1_2 ...

  9. 【笔试题】精选30道Java笔试题解答

    转自于:精选30道Java笔试题解答 精选30道Java笔试题解答 1. 下面哪些是Thread类的方法() A. start() B. run() C. exit() D. getPriority( ...

  10. ajaxReturn 之前dump调试,导致$.ajax不能正常运行

    ajaxReturn 之前dump调试,导致$.ajax不能正常运行 以后调试的时候,注意下这个情况