-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy path11_uniquePathsiii.cpp
More file actions
63 lines (49 loc) · 1.65 KB
/
Copy path11_uniquePathsiii.cpp
File metadata and controls
63 lines (49 loc) · 1.65 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
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
static int X[4] = {-1, 0, 1, 0};
static int Y[4] = {0, -1, 0, 1};
class Solution {
public:
int countPaths(vector<vector<int>>& grid, int x, int y, int empty, const int& m, const int& n) {
/*
for(int i=0; i<m; i++) {
for(int j=0; j<n; j++) {
cout << grid[i][j] << " ";
}
cout << endl;
}
cout << x << " : " << y << " -> " << empty << endl << endl;
*/
// If we reach end cell and
// All empty cells are visited, then return 1
// Else return 0
if(grid[x][y] == 2) {
return (empty == 0);
}
int count = 0; // Count of possible paths from current cell {x,y}
grid[x][y] = -1; // Visit
// Check for all valid directions
for(int k=0; k<4; k++) {
// Possible adjacent coordinates
int i = x + X[k];
int j = y + Y[k];
// Valid Moves
if(i>=0 && j>=0 && i<m && j<n && grid[i][j] != -1) {
count += countPaths(grid, i, j, empty-1, m, n); // Add count of possible paths
}
}
// Backtrack
grid[x][y] = 0; // Unvisit
return count;
}
int uniquePathsIII(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size(), empty = 1;
pair<int, int> start;
for(int i=0; i<m; i++) {
for(int j=0; j<n; j++) {
if(grid[i][j] == 1) start = {i, j};
else if(grid[i][j] == 0) empty++; // Count of empty cells
}
}
// Explore grid from start cell
return countPaths(grid, start.first, start.second, empty, m, n);
}
};