Submission #320520

# Submission time Handle Problem Language Result Execution time Memory
320520 2020-11-09T03:26:37 Z monus1042 Board (CEOI13_board) C++17
0 / 100
200 ms 180028 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);

	ll MAXsteps = 50 * 2 + 2;
	ll Log = 60;
	queue< pair<ll, ll> > bfs; // node, distance
	bfs.push(mkp(A,0));
	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

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 Execution timed out 1097 ms 180028 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 306 ms 49212 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1055 ms 162384 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1103 ms 139744 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1104 ms 164468 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 8 ms 1644 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 620 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 -
# 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 -
# Verdict Execution time Memory Grader output
1 Execution timed out 1086 ms 128460 KB Time limit exceeded
2 Halted 0 ms 0 KB -