-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path752.cpp
More file actions
100 lines (93 loc) · 2.91 KB
/
Copy path752.cpp
File metadata and controls
100 lines (93 loc) · 2.91 KB
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
#include<string>
#include<stdio.h>
#include<vector>
#include<queue>
#include<unordered_set>
using namespace std;
class Solution {
public:
int openLock(vector<string>& deadends, string target) {
if (target == "0000") return 0;
queue<int> searchq;
unordered_set<int> deadends2;
for (int i = 0; i < deadends.size(); ++i) {
int v = deadends[i][3] - '0';
v += (deadends[i][2]-'0') * 10;
v += (deadends[i][1]-'0') * 100;
v += (deadends[i][0]-'0') * 1000;
deadends2.insert(v);
}
int tt = target[3] - '0';
tt += (target[2]-'0') * 10;
tt += (target[1]-'0') * 100;
tt += (target[0]-'0') * 1000;
if (deadends2.count(0) > 0) return -1;
searchq.push(0);
int round = 0;
while (searchq.size() > 0) {
round++;
int round_size = searchq.size();
//printf("round=%d\n", round);
while (round_size > 0) {
round_size--;
int step = searchq.front();
searchq.pop();
#define PUSHSTEP(pos, how) if (push(tt, step, pos, how, searchq, deadends2)) return round
PUSHSTEP(1, 1);
PUSHSTEP(10, 1);
PUSHSTEP(100, 1);
PUSHSTEP(1000, 1);
PUSHSTEP(1, -1);
PUSHSTEP(10, -1);
PUSHSTEP(100, -1);
PUSHSTEP(1000, -1);
deadends2.insert(step);
}
}
return -1;
}
bool push(int tt, int step, int pos, int how, queue<int>& searchq, unordered_set<int>& deadends2) {
step += 30000;
int step2 = step + pos*how;
int up = pos*10;
if (up < 0) {
up = -up;
}
if (step/up*up != step2/up*up) {
step2 -= pos*how*10;
}
step2 -= 30000;
if (tt == step2) return true;
if (deadends2.count(step2) > 0) return false;
deadends2.insert(step2);
searchq.push(step2);
return false;
}
};
int main()
{
vector<string> deadends;
#if 0
string target = "0202";
deadends.push_back("0201");
deadends.push_back("0101");
deadends.push_back("0102");
deadends.push_back("1212");
deadends.push_back("2002");
#endif
#if 1
string target = "8888";
deadends.push_back("8887");
deadends.push_back("8889");
deadends.push_back("8878");
deadends.push_back("8898");
deadends.push_back("8788");
deadends.push_back("8988");
deadends.push_back("7888");
deadends.push_back("9888");
#endif
//deadends.push_back("");
Solution s;
printf("%d\n", s.openLock(deadends, target));
return 0;
}