Skip to content

Commit ced5a58

Browse files
committed
solved leetcode daily challenge, open the lock
See: Why: How: Tags:
1 parent 32733b2 commit ced5a58

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

LeetCode/Open_the_Lock/main.cxx

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#include <bits/stdc++.h>
2+
#include <gtest/gtest.h>
3+
using namespace std;
4+
5+
6+
//// START
7+
/*
8+
## Open the Lock
9+
10+
*/
11+
12+
13+
class Solution {
14+
public:
15+
int openLock(vector<string> &deadends, string target) {
16+
unordered_map<string, bool> visited;
17+
for (auto dead : deadends) {
18+
visited[dead] = true;
19+
}
20+
if (visited["0000"]) return -1;
21+
22+
int depth = 0;
23+
queue<string> qq;
24+
qq.push("0000");
25+
while (!qq.empty()) {
26+
int qlen = qq.size();
27+
while (qlen > 0) {
28+
qlen--;
29+
30+
string src = qq.front();
31+
qq.pop();
32+
if (src == target) return depth;
33+
for (int i = 0; i < 4; i++) {
34+
{
35+
36+
string tmp(src);
37+
if (tmp[i] <= '8') {
38+
tmp[i]++;
39+
} else {
40+
tmp[i] = '0';
41+
}
42+
if (!visited[tmp]) {
43+
visited[tmp]=true;
44+
qq.push(tmp);
45+
}
46+
}
47+
{
48+
49+
string tmp(src);
50+
if (tmp[i] >= '1') {
51+
tmp[i]--;
52+
} else {
53+
tmp[i] = '9';
54+
}
55+
if (!visited[tmp]) {
56+
visited[tmp]=true;
57+
qq.push(tmp);
58+
}
59+
}
60+
}
61+
}
62+
depth++;
63+
}
64+
return -1;
65+
}
66+
};
67+
68+
//// END
69+
struct T {
70+
71+
};
72+
73+
TEST(Solution, test) {
74+
T ts[] = {
75+
{
76+
77+
},
78+
{
79+
80+
},
81+
82+
};
83+
84+
for (T t : ts) {
85+
Solution solution;
86+
87+
}
88+
}
89+
90+
int main() {
91+
testing::InitGoogleTest();
92+
93+
return RUN_ALL_TESTS();
94+
}
95+
96+

0 commit comments

Comments
 (0)