From d554d6c35f4c28c3f207cd6714557b6f998e3e3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=97=BC=ED=98=9C=EC=A0=95?= <122238744+cyzlcyzl@users.noreply.github.com> Date: Sat, 15 Aug 2026 21:50:33 +0900 Subject: [PATCH] Create 576. Out of Boundary Paths.java --- .../576. Out of Boundary Paths.java" | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 "leetcode3/\354\227\274\355\230\234\354\240\225/576. Out of Boundary Paths.java" diff --git "a/leetcode3/\354\227\274\355\230\234\354\240\225/576. Out of Boundary Paths.java" "b/leetcode3/\354\227\274\355\230\234\354\240\225/576. Out of Boundary Paths.java" new file mode 100644 index 00000000..0c6247b4 --- /dev/null +++ "b/leetcode3/\354\227\274\355\230\234\354\240\225/576. Out of Boundary Paths.java" @@ -0,0 +1,32 @@ +// bfs + dp + +class Solution { + public int findPaths(int m, int n, int maxMove, int startRow, int startColumn) { + final int MOD = 1_000_000_007; + long[][] dp = new long[m][n]; + dp[startRow][startColumn] = 1; + int count = 0; + int[][] dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; + + for (int move = 0; move < maxMove; move++) { + long[][] next = new long[m][n]; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (dp[i][j] == 0) continue; + for (int[] d : dirs) { + int ni = i + d[0]; + int nj = j + d[1]; + if (ni < 0 || ni >= m || nj < 0 || nj >= n) { + count = (int) ((count + dp[i][j]) % MOD); + } else { + next[ni][nj] = (next[ni][nj] + dp[i][j]) % MOD; + } + } + } + } + dp = next; + } + + return count; + } +}