-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoublenot.cpp
68 lines (55 loc) · 1.22 KB
/
doublenot.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
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pi = pair<ll, ll>;
using vi = vector<ll>;
const ll maxn = (ll)pow(2, 10);
ll inv(ll val) {
if (val == 0) return 1;
ll x = log2(val) + 1;
for (ll i = 0; i < x; i++) val = (val ^ (1 << i));
return val;
}
void solve() {
ll start;
ll end;
cin >> start;
cin >> end;
ll s, e;
s = stoi(to_string(start), nullptr, 2);
e = stoi(to_string(end), nullptr, 2);
// bfs on states
queue<ll> ready;
vi dist(maxn, INT_MAX);
ready.push(s);
dist[s] = 0;
while (!ready.empty()) {
ll x = ready.front();
ready.pop();
ll notx = inv(x);
ll dx = x << 1;
if (notx < maxn && dist[notx] == INT_MAX) {
dist[notx] = dist[x] + 1;
ready.push(notx);
}
if (dx < maxn && dist[dx] == INT_MAX) {
dist[dx] = dist[x] + 1;
ready.push(dx);
}
}
if (dist[e] == INT_MAX) {
cout << "IMPOSSIBLE";
} else {
cout << dist[e];
}
}
int main() {
ll t;
cin >> t;
for (ll i = 1; i <= t; i++) {
cout << "Case #" << i << ": ";
solve();
cout << endl;
}
return 0;
}