-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution576.cs
44 lines (39 loc) · 1.39 KB
/
Solution576.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System.Text;
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution576
{
/// <summary>
/// 576. Out of Boundary Paths - Medium
/// <a href="https://leetcode.com/problems/out-of-boundary-paths">See the problem</a>
/// </summary>
public int FindPaths(int m, int n, int maxMove, int startRow, int startColumn)
{
var dp = new int[m, n, maxMove + 1];
var mod = 1000000007;
var directions = new int[] { 0, 1, 0, -1, 0 };
for (var move = 1; move <= maxMove; move++)
{
for (var row = 0; row < m; row++)
{
for (var column = 0; column < n; column++)
{
for (var i = 0; i < 4; i++)
{
var newRow = row + directions[i];
var newColumn = column + directions[i + 1];
if (newRow < 0 || newRow >= m || newColumn < 0 || newColumn >= n)
{
dp[row, column, move] += 1;
}
else
{
dp[row, column, move] = (dp[row, column, move] + dp[newRow, newColumn, move - 1]) % mod;
}
}
}
}
}
return dp[startRow, startColumn, maxMove];
}
}