forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
86 lines (70 loc) · 1.94 KB
/
main.cpp
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
/// Source : https://leetcode.com/problems/unique-paths-iii/
/// Author : liuyubobobo
/// Time : 2019-01-21
#include <iostream>
#include <vector>
using namespace std;
/// Backtrack
/// Time Complexity: O(4 ^ (m * n))
/// Space Complexity: O(m * n)
class Solution {
private:
int m, n, end;
const int d[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
public:
int uniquePathsIII(vector<vector<int>>& grid) {
m = grid.size();
n = grid[0].size();
int start, todo = 0;
for(int i = 0; i < m; i ++)
for(int j = 0; j < n; j ++){
int pos = i * n + j;
if(grid[i][j] == 1)
start = pos;
else if(grid[i][j] == 2)
end = pos, grid[i][j] = 0, todo ++;
else if(grid[i][j] == 0)
todo ++;
}
return dfs(grid, start, todo);
}
private:
int dfs(vector<vector<int>>& grid, int pos, int todo){
if(pos == end)
return !todo;
int x = pos / n, y = pos % n, res = 0;
for(int i = 0; i < 4; i ++){
int nextx = x + d[i][0], nexty = y + d[i][1];
if(nextx >= 0 && nextx < m && nexty >= 0 && nexty < n && grid[nextx][nexty] == 0){
grid[nextx][nexty] = 1;
res += dfs(grid, nextx * n + nexty, todo - 1);
grid[nextx][nexty] = 0;
}
}
return res;
}
};
int main() {
vector<vector<int>> g0 = {
{1,0},
{0,0},
{0,2}
};
cout << Solution().uniquePathsIII(g0) << endl;
// 1
vector<vector<int>> g1 = {
{1,0,0,0},
{0,0,0,0},
{0,0,2,-1}
};
cout << Solution().uniquePathsIII(g1) << endl;
// 2
vector<vector<int>> g2 = {
{1,0,0,0},
{0,0,0,0},
{0,0,0,2}
};
cout << Solution().uniquePathsIII(g2) << endl;
// 4
return 0;
}