[Swift]LeetCode576. 出界的路徑數 | Out of Boundary Paths

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-vhqtkwhd-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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 Ntimes. Find out the number of paths to move the ball out of grid boundary. The answer may be very large, return it after mod 109 + 7. git

Example 1:github

Input: m = 2, n = 2, N = 2, i = 0, j = 0
Output: 6
Explanation:

Example 2:微信

Input: m = 1, n = 3, N = 3, i = 0, j = 1
Output: 12
Explanation:

Note:spa

  1. Once you move the ball out of boundary, you cannot move it back.
  2. The length and height of the grid is in range [1,50].
  3. N is in range [0,50].

給定一個 m × n 的網格和一個球。球的起始座標爲 (i,j) ,你能夠將球移到相鄰的單元格內,或者往上、下、左、右四個方向上移動使球穿過網格邊界。可是,你最多能夠移動 N 次。找出能夠將球移出邊界的路徑數量。答案可能很是大,返回 結果 mod 109 + 7 的值。code

示例 1:htm

輸入: m = 2, n = 2, N = 2, i = 0, j = 0
輸出: 6
解釋:

示例 2:blog

輸入: m = 1, n = 3, N = 3, i = 0, j = 1
輸出: 12
解釋:

說明:get

  1. 球一旦出界,就不能再被移動回網格內。
  2. 網格的長度和高度在 [1,50] 的範圍內。
  3. N 在 [0,50] 的範圍內。

 


44ms博客

 1 class Solution {
 2     func findPaths(_ m: Int, _ n: Int, _ N: Int, _ i: Int, _ j: Int) -> Int {
 3         var dp = [[[Int]]](repeating: [[Int]](repeating: [Int](repeating: -1, count: N+1), count: n), count: m)
 4         return findPaths(m, n, N, i, j, &dp)
 5     }
 6     
 7     func findPaths(_ m: Int, _ n: Int, _ N: Int, _ i: Int, _ j: Int, _ dp: inout [[[Int]]]) -> Int {
 8         // print("\(i), \(j), \(N)")
 9         let minMoves = min(min(i+1, m-i), min(j+1, n-j))
10         if minMoves > N { return 0 }
11         if dp[i][j][N] > -1 { return dp[i][j][N] }
12         if dp[m-i-1][j][N] > -1 { return dp[m-i-1][j][N] }
13         if dp[i][n-j-1][N] > -1 { return dp[i][n-j-1][N] }
14         if dp[m-i-1][n-j-1][N] > -1 { return dp[m-i-1][n-j-1][N] }
15         var res = 0
16         for (d_i, d_j) in [(-1, 0), (1, 0), (0, -1), (0, 1)] {
17             let next_i = i + d_i
18             let next_j = j + d_j
19             if next_i < 0 || next_i >= m || next_j < 0 || next_j >= n {
20                 res += 1
21             } else {
22                 res += findPaths(m, n, N-1, next_i, next_j, &dp)
23                 res = res % 1000000007
24             }
25         }
26         
27         dp[i][j][N] = res 
28         return res
29     }
30 }

104ms

 1 class Solution {    
 2     private let dirs = [[-1, 0], [1, 0], [0, -1], [0, 1]]
 3     private let mod = 1000000000 + 7
 4     
 5     func findPaths(_ m: Int, _ n: Int, _ N: Int, _ i: Int, _ j: Int) -> Int {
 6         var memo: [[[Int]]] = Array(repeating: Array(repeating: Array(repeating: -1, count: N + 1), count: n), count: m)
 7         return dfs(m, n, N, i, j, &memo)
 8     }
 9     
10     private func dfs(_ m: Int, _ n: Int, _ N: Int, _ i: Int, _ j: Int, _ memo: inout [[[Int]]]) -> Int {
11         if i < 0 || i >= m || j < 0 || j >= n {
12             return 1
13         }
14         if N == 0 { return 0 }
15         if memo[i][j][N] != -1 { return memo[i][j][N] }
16         memo[i][j][N] = 0
17         for dir in dirs {
18             let x = i + dir[0]
19             let y = j + dir[1]
20             memo[i][j][N] = (memo[i][j][N] + dfs(m, n, N - 1, x, y, &memo) % mod) % mod
21         }
22         return memo[i][j][N]
23     }
24 }

180ms

 1 class Solution {
 2     func findPaths(_ m: Int, _ n: Int, _ N: Int, _ i: Int, _ j: Int) -> Int {
 3         let M = 1000000000 + 7
 4         let memoZero = Array(repeating: Array(repeating: 0, count: n), count: m)
 5         var memo = memoZero
 6         memo[i][j] = 1
 7         var count = 0
 8         for _ in 0..<N {
 9             var temp = memoZero
10             for x in 0..<m {
11                 for y in 0..<n {
12                     let curr = memo[x][y]
13                     if x == m - 1 { count += curr }
14                     if y == n - 1 { count += curr }
15                     if x == 0 { count += curr }
16                     if y == 0 { count += curr }
17                     count %= M
18                     temp[x][y] += x > 0 ? memo[x - 1][y]: 0
19                     temp[x][y] += x < m - 1 ? memo[x + 1][y]: 0
20                     temp[x][y] += y > 0 ? memo[x][y - 1]: 0
21                     temp[x][y] += y < n - 1 ? memo[x][y + 1]: 0
22                     temp[x][y] %= M
23                 }
24             }
25             memo = temp
26         }
27         return count
28     }
29 }

Runtime: 332 ms
Memory Usage: 19 MB
 1 class Solution {
 2     func findPaths(_ m: Int, _ n: Int, _ N: Int, _ i: Int, _ j: Int) -> Int {
 3         var res:Int = 0
 4         var dp:[[Int]] = [[Int]](repeating:[Int](repeating:0,count:n),count:m)
 5         dp[i][j] = 1
 6         var dirs:[[Int]] = [[0,-1],[-1,0],[0,1],[1,0]]
 7         for k in 0..<N
 8         {
 9             var t:[[Int]] = [[Int]](repeating:[Int](repeating:0,count:n),count:m)
10             for r in 0..<m
11             {
12                 for c in 0..<n
13                 {
14                     for dir in dirs
15                     {
16                         var x:Int = r + dir[0]
17                         var y:Int = c + dir[1]
18                         if x < 0 || x >= m || y < 0 || y >= n
19                         {
20                             res = (res + dp[r][c]) % 1000000007
21                         }
22                         else
23                         {
24                             t[x][y] = (t[x][y] + dp[r][c]) % 1000000007
25                         }
26                     }
27                 }
28             }
29             dp = t
30         }
31         return res
32     }
33 }
相關文章
相關標籤/搜索