leetcode 576. Out of Boundary Paths
题意大概就是在一个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的更多相关文章
- leetcode 576. Out of Boundary Paths 、688. Knight Probability in Chessboard
576. Out of Boundary Paths 给你一个棋盘,并放一个东西在一个起始位置,上.下.左.右移动,移动n次,一共有多少种可能移出这个棋盘 https://www.cnblogs.co ...
- 第十一周 Leetcode 576. Out of Boundary Paths (HARD) 计数dp
Leetcode 576 给定一个二维平面, 一个球在初始位置(i,j)每次可以转移到上下左右的一格. 问在N次转移内,有多少种路径可以转移出边境. dp[i][j][k]为 在点(i,j) 已经走了 ...
- 【LeetCode】576. Out of Boundary Paths 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 状态搜索 记忆化搜索 相似题目 参考资料 ...
- 【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 ...
- 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 ...
- [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 ...
- [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 ...
- 【一天一道LeetCode】#257. Binary Tree Paths
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】257. Binary Tree Paths 解题报告(java & python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leet ...
随机推荐
- Optimizing subroutine calls based on architecture level of called subroutine
A technique is provided for generating stubs. A processing circuit receives a call to a called funct ...
- 10.异步SRAM的FPGA读写操作
异步SRAM:正如其名,不是与特定的时钟信号同步运行,而是根据输入信号的状态运行的.因为没有信号表明读取时已确定了有效数据,也没有信号表明写入时已接收到数据,所以,需要获取制造商的数据手册,根据时序图 ...
- java===java基础学习(4)---字符串操作
java中的字符串操作和python中的大致相同,需要熟悉的就是具体操作形式. 关于具体api的使用,详见:java===字符串常用API介绍(转) package testbotoo; public ...
- Pretrained models for Pytorch (Work in progress)
The goal of this repo is: to help to reproduce research papers results (transfer learning setups for ...
- php 读写 csv文件
读取csv function input_csv($handle) { $out = array (); $n = 0; while ($data = fgetcsv($handle, 10000)) ...
- PHP的输出方式
php中,用echo输出一个字符串有三种方式,分别是单引号,双引号和<<<方式.其中,单引号中的变量不会被解析,而会直接输出,而双引号和<<<时,变量会被解析.&l ...
- Linux下批量Kill多个进程的方法
转自http://www.jb51.net/LINUXjishu/43534.html ps -ef|grep tt.py|grep -v grep|cut -c 9-15|xargs kill -9 ...
- python_day5学习笔记
一.正则表达式 字符: \d 匹配任何十进制数:相当于类[0-9] \D 匹配任何非数字字符:相当于类[^0-9] \s 匹配任何空白字符:相当于类[ \t\n\r\f\v] \S 匹配任何非空 ...
- LeetCode解题报告—— 4Sum & Remove Nth Node From End of List & Generate Parentheses
1. 4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + ...
- hdu 1003(最大子段和)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...