-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_0980_uniquePathsIII.cc
78 lines (73 loc) · 1.56 KB
/
Problem_0980_uniquePathsIII.cc
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
#include <functional>
#include <iostream>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
// 递归
int uniquePathsIII(vector<vector<int>> &grid)
{
int n = grid.size();
int m = grid[0].size();
int si = 0;
int sj = 0;
int total = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (grid[i][j] == 0)
{
total++;
}
else if (grid[i][j] == 1)
{
total++;
si = i;
sj = j;
}
}
}
vector<vector<int>> dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
function<int(int, int, int)> dfs = [&](int i, int j, int rest)
{
if (grid[i][j] == 2)
{
return rest == 0 ? 1 : 0;
}
int t = grid[i][j];
int ans = 0;
grid[i][j] = -1;
for (auto& dir : dirs)
{
int ni = i + dir[0];
int nj = j + dir[1];
if (ni >= 0 && ni < n && nj >= 0 && nj < m && (grid[ni][nj] == 0 || grid[ni][nj] == 2))
{
ans += dfs(ni, nj, rest - 1);
}
}
grid[i][j] = t;
return ans;
};
return dfs(si, sj, total);
}
};
void test()
{
Solution s;
vector<vector<int>> g1 = {{1, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 2, -1}};
vector<vector<int>> g2 = {{1, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 2}};
vector<vector<int>> g3 = {{0, 1}, {2, 0}};
EXPECT_EQ_INT(2, s.uniquePathsIII(g1));
EXPECT_EQ_INT(4, s.uniquePathsIII(g2));
EXPECT_EQ_INT(0, s.uniquePathsIII(g3));
EXPECT_SUMMARY;
}
int main()
{
test();
return 0;
}