leetcode 576. Out of Boundary Paths

https://leetcode.com/problems/out-of-boundary-paths/#/description

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

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

 1 class Solution {
 2 public:
 3     int findPaths(int m, int n, int N, int i, int j) {
 4         int dp[51][50][50];
 5         for(int Ni=0;Ni<=N;Ni++)
 6           for(int mi=0;mi<m;mi++)
 7             for(int ni=0;ni<n;ni++){
 8                 if(!Ni) {dp[Ni][mi][ni]==0; continue;}
 9                 dp[Ni][mi][ni]=((long long) (!mi?1:dp[Ni-1][mi-1][ni])+(mi==m-1?1:dp[Ni-1][mi+1][ni])+
10                                             (!ni?1:dp[Ni-1][mi][ni-1])+(ni==n-1?1:dp[Ni-1][mi][ni+1]))%1000000007;
11             }
12         return dp[N][i][j];
13     }
14 };

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

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

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

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

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

时间: 2024-08-30 01:09:35

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

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

Leetcode 576 给定一个二维平面, 一个球在初始位置(i,j)每次可以转移到上下左右的一格. 问在N次转移内,有多少种路径可以转移出边境. dp[i][j][k]为 在点(i,j) 已经走了k步的累积路径数. 最后答案就是边境点且k<=N-1之和. 转移方程是显而易见的. const int MOD=1000000007; class Solution { public: int dp[50][50][50]; int findPaths(int m, int n, int N, in

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 move the ball to adjacent cell or cross the grid boundary in four directions (up, down, left, right). However, you can at most move N times. Find out the numbe

【LeetCode】257. Binary Tree Paths 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51474128 Subject 出处:https://leetcode.com/problems/binary-tree-paths/ Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / \ 2 3 \ 5 All root-to-le

&lt;LeetCode OJ&gt; 257. Binary Tree Paths

257. Binary Tree Paths My Submissions Question Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / 2 3 5 All root-to-leaf paths are: ["

Leetcode题目:Binary Tree Paths

题目: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / 2 3 5 All root-to-leaf paths are: ["1->2->5", "1->3"] 题目解答:使用递归的方式来处理这道题目,每到叶子节点,就进行一次输出. 代码如下: /** * Definition for a b

LeetCode OJ:Binary Tree Paths(二叉树路径)

Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / 2 3 5 All root-to-leaf paths are: ["1->2->5", "1->3"] 简单的遍历查找路径问题,代码如下: 1 /** 2 * Definition for a binary tree node. 3 * str

【LeetCode】257 - Binary Tree Paths

Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / 2 3 5 All root-to-leaf paths are: ["1->2->5", "1->3"] Solution: /** * Definition for a binary tree node. * struct TreeNode {

LeetCode OJ 之 Binary Tree Paths(二叉树路径)

题目: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / 2 3 5 All root-to-leaf paths are: ["1->2->5", "1->3"] 思路: 前序递归即可. 代码1: /** * Definition for a binary tree node. * struct

【leetcode】1301. Number of Paths with Max Score

题目如下: You are given a square board of characters. You can move on the board starting at the bottom right square marked with the character 'S'. You need to reach the top left square marked with the character 'E'. The rest of the squares are labeled ei