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 ...
随机推荐
- Development tools[重点]
Development tools yum groupinfo "Development tools" Loaded plugins: product-id, security, ...
- $scope作用及模块化解决全局问题
$scope对象就是一个普通的JavaScript对象,我们可以在其上随意修改或添加属性.$scope对象在AngularJS中充当数据模型,但与传统的数据模型不一样,$scope并不负责处理和操作数 ...
- 【bzoj3224】普通平衡树
看有没有人能发现咯. #include<bits/stdc++.h> #define N 300005 #define rat 4 #define pushup(o) if(o->l ...
- #error This file was generated by a newer version of protoc
pattern@pattern89:/raid0/workspace/houjun/caffe-ssd$ sudo make all -j8PROTOC src/caffe/proto/caffe.p ...
- service XXX start启动报start: Rejected send message, 1 matche
转,原文地址:http://blog.sina.com.cn/s/blog_56d8ea9001018w1l.html [问题]start: Rejected send messag现象:crifan ...
- Django在根据models生成数据库表时报错
报错信息: E:\Python\s6day103>python manage.py makemigrations Traceback (most recent call last): File ...
- 《java并发编程实战》读书笔记6--取消与关闭
第7章 取消与关闭 这章的主要内容是关于如何使任务和线程安全,快速,可靠的停止下来. 7.1 任务取消 在Java中没有一种安全的抢占方式来停止线程,但是可以使用一些协作机制,比如: 让素数生成器运行 ...
- hdu 3081(二分+并查集+最大流||二分图匹配)
Marriage Match II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- window 下 nginx+php+fastcgi 架设备忘
1.配置Php.ini 1)extension_dir = "./ext" 修改这个路径为真实的php的ext路径 2);extension=php_mysql.dll ;exte ...
- [水煮 ASP.NET Web API2 方法论](1-7)CSRF-Cross-Site Request Forgery
问题 通过 CSRF(Cross-Site Request Forgery)防护,保护从 MVC 页面提交到ASP.NET Web API 的数据. 解决方案 ASP.NET 已经加入了 CSRF 防 ...