-
Notifications
You must be signed in to change notification settings - Fork 0
/
753. Cracking the Safe.cpp
42 lines (37 loc) · 1.06 KB
/
753. Cracking the Safe.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
class Solution {
public:
string crackSafe(int n, int k) {
int target = pow(k, n);
string res;
for (int i = 0; i < n; ++i) {
res += '0';
}
string cur = res;
unordered_set<string>visited;
dfs(res, target, visited, n, k, cur);
return res;
}
bool dfs(string& res, int& target, unordered_set<string>& visited, int n, int k, string cur) {
if (visited.count(cur)) {
return false;
}
visited.insert(cur);
if (visited.size() == target) {
return true;
}
string next = cur.substr(1);
for (int i = 0; i < k; ++i) {
next.push_back('0' + i);
if (!visited.count(next)) {
res.push_back('0' + i);
if (dfs(res, target, visited, n, k, next)) {
return true;
}
res.pop_back();
}
next.pop_back();
}
visited.erase(cur);
return false;
}
};