# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
320633 |
2020-11-09T11:12:08 Z |
monus1042 |
Board (CEOI13_board) |
C++17 |
|
3 ms |
1516 KB |
// NK
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
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)
//typedef tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update> ord_set;
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);
int MAXsteps = 10 * 2 + 2;
ll Log = 11;
queue< pair<ll, int> > bfs; // node, distance
bfs.push(mkp(A,0));
unordered_set<ll> vis;
vis.insert(A);
while(true){
ll u = bfs.front().first;
int 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+1 < MAXsteps){
vis.insert(left);
bfs.push(mkp(left, d + 1));
}
if (right < (ll)(1LL << Log) && vis.find(right) == vis.end() && d+1 < MAXsteps){
vis.insert(right);
bfs.push(mkp(right, d + 1));
}
if (L < (ll)(1LL << Log) && vis.find(L) == vis.end() && d + 1 < 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 + 1));
}
}
if (R < (ll)(1LL << Log) && vis.find(R) == vis.end() && d+1 < 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 + 1));
}
}
if (U < (ll)(1LL << Log) && vis.find(U) == vis.end() && d+1 < MAXsteps && U > 0LL){
vis.insert(U);
bfs.push(mkp(U, d + 1));
}
}
}
int main(){
Bolivia_is_nice;
int t = 1; //cin>>t;
while(t--)
solve();
return 0;
}
/*
~/.emacs
*/
Compilation message
board.cpp: In function 'll locationof(std::string, ll)':
board.cpp:28:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | for (int i=0; i<s.size(); i++){
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
876 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
2 ms |
748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2 ms |
876 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
748 KB |
Execution killed with signal 6 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2 ms |
748 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2 ms |
748 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
3 ms |
1516 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
3 ms |
1508 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
3 ms |
1260 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |