This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// NK
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vll;
#define pb push_back
#define eb emplace_back
#define pob pop_back
#define psf push_front
#define pof pop_front
#define mkp make_pair
#define mkt make_tuple
#define all(x) x.begin(), x.end()
#define Bolivia_is_nice ios::sync_with_stdio(false), cin.tie(nullptr)
ll locationof(string s, ll nod){
for (int i=0; i<s.size(); i++){
if (s[i] == '1'){
nod = nod*2;
}else if (s[i] == '2'){
nod = nod*2 + 1;
}else if (s[i] == 'U'){
nod = nod / 2;
}else if (s[i] == 'L'){
nod = nod - 1;
}else{ // R
nod = nod + 1;
}
}
return nod;
}
void solve(){
string a,b; cin>>a>>b;
ll A = locationof(a, 1ll), B = locationof(b, 1ll);
ll MAXsteps = 50 * 2 + 2;
ll Log = 51;
queue< pair<ll, ll> > bfs; // node, distance
bfs.push(mkp(A,0));
unordered_set<ll> vis;
vis.insert(A);
while(true){
ll u = bfs.front().first;
ll d = bfs.front().second;
//cout<<u<<' '<<d<<endl;
bfs.pop();
if (u == B){
cout<<d<<'\n';
return;
}
ll left, right, L, R, U;
left = u*2;
right = u*2 + 1LL;
L = u-1;
R = u+1;
U = u/2;
if (left < (ll)(1LL << Log) && vis.find(left) == vis.end() && d+1LL < MAXsteps){
vis.insert(left);
bfs.push(mkp(left, d + 1LL));
}
if (right < (ll)(1LL << Log) && vis.find(right) == vis.end() && d+1LL < MAXsteps){
vis.insert(right);
bfs.push(mkp(right, d + 1LL));
}
if (L < (ll)(1LL << Log) && vis.find(L) == vis.end() && d+1LL < MAXsteps && L > 0LL){
ll cnt = 0;
for (ll bit = Log; bit >= 0; --bit){
if (u & (1LL << bit)) ++cnt;
}
if (cnt != 1){
vis.insert(L);
bfs.push(mkp(L, d + 1LL));
}
}
if (R < (ll)(1LL << Log) && vis.find(R) == vis.end() && d+1LL < MAXsteps){
bool sw = 0, ok = 1;
for (ll bit = Log; bit >= 0; --bit){
if (u & (1LL << bit) && !sw){
sw = 1;
}else if ( !(u & (1LL << bit)) && sw){
ok = 0;
}
}
if (!ok){
vis.insert(R);
bfs.push(mkp(R, d + 1LL));
}
}
if (U < (ll)(1LL << Log) && vis.find(U) == vis.end() && d+1LL < MAXsteps && U > 0LL){
vis.insert(U);
bfs.push(mkp(U, d + 1LL));
}
}
}
int main(){
Bolivia_is_nice;
int t = 1; //cin>>t;
while(t--)
solve();
return 0;
}
/*
~/.emacs
*/
Compilation message (stderr)
board.cpp: In function 'll locationof(std::string, ll)':
board.cpp:21:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | for (int i=0; i<s.size(); i++){
| ~^~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |