aboutsummaryrefslogtreecommitdiff
path: root/out_of_bounds_paths.cpp
diff options
context:
space:
mode:
authorjack <[email protected]>2024-06-14 13:13:15 -0600
committerjack <[email protected]>2024-06-14 13:13:15 -0600
commitd343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch)
tree96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /out_of_bounds_paths.cpp
downloadleetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz
leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip
initial
Diffstat (limited to 'out_of_bounds_paths.cpp')
-rw-r--r--out_of_bounds_paths.cpp35
1 files changed, 35 insertions, 0 deletions
diff --git a/out_of_bounds_paths.cpp b/out_of_bounds_paths.cpp
new file mode 100644
index 0000000..fa19333
--- /dev/null
+++ b/out_of_bounds_paths.cpp
@@ -0,0 +1,35 @@
+#include "leetcode.h"
+
+class Solution {
+public:
+ int findPaths(int m, int n, int maxMove, int startRow, int startColumn) {
+ if (!maxMove)
+ return 0;
+ vector<vector<int>> curr(m + 2, vector<int>(n + 2));
+ vector<vector<int>> last(m + 2, vector<int>(n + 2));
+ for (int i = 1; i <= m; i++) {
+ curr[i][1]++;
+ curr[i][n]++;
+ }
+ for (int j = 1; j <= n; j++) {
+ curr[1][j]++;
+ curr[m][j]++;
+ }
+ int ans = curr[startRow + 1][startColumn + 1];
+ for (int d = 1; d < maxMove; d++) {
+ curr.swap(last);
+ for (int i = 1; i <= m; i++)
+ for (int j = 1; j <= n; j++)
+ curr[i][j] = (int)(((long)last[i - 1][j] + last[i + 1][j] +
+ last[i][j - 1] + last[i][j + 1]) %
+ 1000000007L);
+ ans = (ans + curr[startRow + 1][startColumn + 1]) % 1000000007L;
+ }
+ return ans;
+ }
+};
+
+int main() {
+ Solution obj;
+ cout << obj.findPaths(2, 2, 2, 0, 0);
+}