-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuniquePathsIII.cpp
More file actions
35 lines (33 loc) · 1.04 KB
/
uniquePathsIII.cpp
File metadata and controls
35 lines (33 loc) · 1.04 KB
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
// Source: https://leetcode.com/problems/unique-paths-iii/
// Author: Miao Zhang
// Date: 2021-03-30
class Solution {
public:
int uniquePathsIII(vector<vector<int>>& grid) {
int sx = -1;
int sy = -1;
int n = 1;
for (int i = 0; i < grid.size(); i++) {
for (int j = 0; j < grid[0].size(); j++) {
if (grid[i][j] == 0) n++;
else if (grid[i][j] == 1) {
sx = i;
sy = j;
}
}
}
return dfs(grid, sx, sy, n);
}
private:
int dfs(vector<vector<int>>& grid, int x, int y, int n) {
if (x < 0 || x >= grid.size() || y < 0 || y >= grid[0].size() || grid[x][y] == -1) return 0;
if (grid[x][y] == 2) return n == 0;
grid[x][y] = -1;
int paths = dfs(grid, x + 1, y, n - 1) +
dfs(grid, x - 1, y, n - 1) +
dfs(grid, x, y + 1, n - 1) +
dfs(grid, x, y - 1, n - 1);
grid[x][y] = 0;
return paths;
}
};