-
Notifications
You must be signed in to change notification settings - Fork 0
/
0576.go
140 lines (118 loc) · 3.16 KB
/
0576.go
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Source: https://leetcode.com/problems/out-of-boundary-paths
// Title: Out of Boundary Paths
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// There is an m x n grid with a ball. The ball is initially at the position [startRow, startColumn]. You are allowed to move the ball to one of the four adjacent cells in the grid (possibly out of the grid crossing the grid boundary). You can apply at most maxMove moves to the ball.
//
// Given the five integers m, n, maxMove, startRow, startColumn, return the number of paths to move the ball out of the grid boundary. Since the answer can be very large, return it modulo 10^9 + 7.
//
// Example 1:
//
// Input: m = 2, n = 2, maxMove = 2, startRow = 0, startColumn = 0
// Output: 6
//
// Example 2:
//
// Input: m = 1, n = 3, maxMove = 3, startRow = 0, startColumn = 1
// Output: 12
//
// Constraints:
//
// 1 <= m, n <= 50
// 0 <= maxMove <= 50
// 0 <= startRow < m
// 0 <= startColumn < n
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
const modulo = int(1e9 + 7)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Use 3D DP
func findPaths(m int, n int, maxMove int, startRow int, startColumn int) int {
grid := make([][][]int, maxMove+1)
for k := 0; k <= maxMove; k++ {
grid[k] = make([][]int, m)
for i := 0; i < m; i++ {
grid[k][i] = make([]int, n)
}
}
res := 0
grid[0][startRow][startColumn] = 1
for move := 0; move < maxMove; move++ {
for x := 0; x < m; x++ {
for y := 0; y < n; y++ {
v := grid[move][x][y] % modulo
if v == 0 {
continue
}
if x == 0 {
res += v
} else {
grid[move+1][x-1][y] += v
}
if x == m-1 {
res += v
} else {
grid[move+1][x+1][y] += v
}
if y == 0 {
res += v
} else {
grid[move+1][x][y-1] += v
}
if y == n-1 {
res += v
} else {
grid[move+1][x][y+1] += v
}
}
}
}
return res % modulo
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Use 2D DP
func findPaths2(m int, n int, maxMove int, startRow int, startColumn int) int {
prevGrid := make([][]int, m)
nextGrid := make([][]int, m)
for i := 0; i < m; i++ {
prevGrid[i] = make([]int, n)
nextGrid[i] = make([]int, n)
}
res := 0
prevGrid[startRow][startColumn] = 1
for move := 0; move < maxMove; move++ {
for x := 0; x < m; x++ {
for y := 0; y < n; y++ {
v := prevGrid[x][y] % modulo
if v == 0 {
continue
}
prevGrid[x][y] = 0
if x == 0 {
res += v
} else {
nextGrid[x-1][y] += v
}
if x == m-1 {
res += v
} else {
nextGrid[x+1][y] += v
}
if y == 0 {
res += v
} else {
nextGrid[x][y-1] += v
}
if y == n-1 {
res += v
} else {
nextGrid[x][y+1] += v
}
}
}
prevGrid, nextGrid = nextGrid, prevGrid
}
return res % modulo
}