This repository was archived by the owner on May 16, 2021. It is now read-only.
forked from zhuli19901106/leetcode-zhuli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathandroid-unlock-patterns_1_AC.cpp
115 lines (93 loc) · 2.66 KB
/
android-unlock-patterns_1_AC.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
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
// Just search it.
#include <vector>
using std::vector;
static const int offset[4][2] = {{-1, -1}, {-1, 0}, {0, -1}, {-1, +1}};
class Solution {
public:
Solution() {
b.resize(NR * NC, vector<bool>(NR * NC, true));
b[0][6] = b[6][0] = false;
b[1][7] = b[7][1] = false;
b[2][8] = b[8][2] = false;
b[0][2] = b[2][0] = false;
b[3][5] = b[5][3] = false;
b[6][8] = b[8][6] = false;
b[0][8] = b[8][0] = false;
b[2][6] = b[6][2] = false;
}
int numberOfPatterns(int m, int n) {
vector<int> a;
vector<bool> v(NR * NC, false);
int res = 0;
dfs(a, v, res, m, n);
return res;
}
~Solution() {
b.clear();
}
private:
static const int NR = 3;
static const int NC = 3;
vector<vector<bool>> b;
bool inbound(int x, int y) {
return x >= 0 && x <= NR - 1 && y >= 0 && y <= NC - 1;
}
void dfs(vector<int> &a, vector<bool> &v, int &res, int m, int n) {
if (a.size() >= m && a.size() <= n) {
++res;
}
if (a.size() >= n) {
return;
}
int i;
for (i = 0; i < NR * NC; ++i) {
if (v[i]) {
continue;
}
if (a.size() > 0 && !b[i][a.back()]) {
continue;
}
mark(i);
a.push_back(i);
v[i] = true;
dfs(a, v, res, m, n);
v[i] = false;
a.pop_back();
unmark(i);
}
}
void mark(int x) {
int i = x / NC;
int j = x % NC;
int k;
int i1, j1;
int i2, j2;
for (k = 0; k < 4; ++k) {
i1 = i + offset[k][0];
j1 = j + offset[k][1];
i2 = i - offset[k][0];
j2 = j - offset[k][1];
if (inbound(i1, j1) && inbound(i2, j2)) {
b[i1 * NC + j1][i2 * NC + j2] = true;
b[i2 * NC + j2][i1 * NC + j1] = true;
}
}
}
void unmark(int x) {
int i = x / NC;
int j = x % NC;
int k;
int i1, j1;
int i2, j2;
for (k = 0; k < 4; ++k) {
i1 = i + offset[k][0];
j1 = j + offset[k][1];
i2 = i - offset[k][0];
j2 = j - offset[k][1];
if (inbound(i1, j1) && inbound(i2, j2)) {
b[i1 * NC + j1][i2 * NC + j2] = false;
b[i2 * NC + j2][i1 * NC + j1] = false;
}
}
}
};