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 ...
随机推荐
- Vue组件-组件的事件
自定义事件 通过prop属性,父组件可以向子组件传递数据,而子组件的自定义事件就是用来将内部的数据报告给父组件的. <div id="app3"> <my-com ...
- Linux-进程间通信(六): 记录锁
1. 记录锁:记录锁的功能是,当一个进程正在读或者修改文件的某个部分的时候,它可以阻止其他进程修改同一文件区: 2. fcntl记录锁: #include <fcntl.h> int fc ...
- 仿照linux dpm机制,实现自己的dpm【转】
转自:http://blog.csdn.net/lixiaojie1012/article/details/23788713 前边我们讨论分析了linux 内核的dpm实现,分析的目的在于学以致用:在 ...
- 64_m2
mimetic-devel-0.9.8-7.fc26.i686.rpm 12-Feb-2017 05:40 288474 mimetic-devel-0.9.8-7.fc26.x86_64.rpm 1 ...
- C语言的小括号----其实是逗号运算符
比如下面的代码: #include <stdio.h> void fun() { int a, b, c, d; a = (, b = ); c = (, ); d = (, ); pri ...
- 【POJ3254】coinfield
状压dp初步. #include<iostream> #include<cstdio> #include<cstring> #include<algorith ...
- 【bzoj4868】期末考试
我还第一次见到省选考三分……? #include<bits/stdc++.h> #define N 200005 using namespace std; typedef long lon ...
- HDU 6112 今夕何夕 蔡勒公式
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6112题意:中文题目 分析:关键点在与如何计算一个日期是星期几,这个可以通过蔡勒公式来计算.基姆拉尔森计 ...
- jps命令学习
jps命令学习 标签(空格分隔): jvm jps介绍 ( JVM Process Status Tool ) 网文 jps命令用于查看当前Java进程及其pid等相关信息,同ps -ef | gre ...
- linux系统使用过程遇到的bug
使用windows与ubuntu双系统,重装windows系统后需要修复ubuntu grub reference ubuntu系统中挂载的windows硬盘点不开 需要重新挂载 reference ...