-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ18112.cpp
More file actions
94 lines (85 loc) · 1.47 KB
/
BOJ18112.cpp
File metadata and controls
94 lines (85 loc) · 1.47 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
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#define INF 2123456789
using namespace std;
bool tf[1025];
struct nn {
int dex;
int cnt = 0;
string bin;
nn() {}
nn(int c,int input) {
dex = input;
dtob();
cnt = c;
}
nn(int c, string input) {
bin = input;
btod();
cnt = c;
}
void btod() {
int ret = 0, cur = 1;
for (int i = bin.length() - 1; i >= 0; i--) {
ret += (bin[i] - '0') * cur;
cur *= 2;
}
dex = ret;
}
void dtob() {
vector<string> v;
int tmp = dex;
while (tmp != 1) {
v.push_back(to_string(tmp % 2));
tmp /= 2;
}
v.push_back(to_string(1));
bin.clear();
while (!v.empty()) {
bin.append(v.back());
v.pop_back();
}
}
}typedef num;
queue<num> q;
int main()
{
string str1, str2;
cin >> str1 >> str2;
num n1(0,str1), n2(0,str2);
q.push(n1);
while (!q.empty()) {
num tmp = q.front();
q.pop();
if (tmp.dex == n2.dex) {
cout << tmp.cnt;
return 0;
}
int nxt = tmp.dex + 1;
if (nxt<= 1024 && !tf[nxt]) {
num n3(tmp.cnt+1,nxt);
tf[nxt] = true;
q.push(n3);
}
nxt = tmp.dex - 1;
if (1 <= nxt && !tf[nxt]) {
num n3(tmp.cnt+1,nxt);
tf[nxt] = true;
q.push(n3);
}
for (int i = 1; i < tmp.bin.size(); i++) {
string str3 = tmp.bin;
str3.at(i) = str3.at(i) == '0' ? '1' : '0';
num n3(tmp.cnt + 1, str3);
if (1 <= n3.dex && n3.dex <= 1024 && !tf[n3.dex]) {
tf[n3.dex] = true;
q.push(n3);
}
}
}
return 0;
}