Artifact Content
Not logged in

Artifact 990eb2ca96ab96cb2c6b6ae9c3b38c5b879d6cdd


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
using namespace std;
typedef long long LL;

class BikeLock
{
public:
	map<pair<int,string>,int> memo;
	int minTurns(string current, string desired) 
	{
		current += "__";
		return rec(0, current.size()-2, current, desired);
	}

	int rec(int i, int N, string& s, const string& d)
	{
		if( i == N )
			return 0;
		if( s[i] == d[i] )
			return rec(i+1, N, s, d);
		pair<int,string> key( i, s.substr(i,3) );
		if( memo.count(key) )
			return memo[key];
		memo[key] = 9999;

		int ans = 9999;
		for(int w=1; w<=3; ++w)
			for(int r=-3; r<=3; ++r) if(r)
			{
				for(int j=0; j<w; ++j)
					s[i+j] = (s[i+j]-'0'+10+r)%10+'0';
				ans = min(ans, 1 + rec(i, N, s, d));
				for(int j=0; j<w; ++j)
					s[i+j] = (s[i+j]-'0'+10-r)%10+'0';
			}
		return memo[key] = ans;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }

template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
int verify_case(const int &Expected, const int &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;}

template<int N> struct Case_ { Case_(){start_time=clock();} };
char Test_(...);
int Test_(Case_<0>) {
	string current = "555"; 
	string desired = "464"; 
	int RetVal = 2; 
	return verify_case(RetVal, BikeLock().minTurns(current, desired)); }
int Test_(Case_<1>) {
	string current = "1234"; 
	string desired = "3456"; 
	int RetVal = 2; 
	return verify_case(RetVal, BikeLock().minTurns(current, desired)); }
int Test_(Case_<2>) {
	string current = "06012005"; 
	string desired = "06012005"; 
	int RetVal = 0; 
	return verify_case(RetVal, BikeLock().minTurns(current, desired)); }
int Test_(Case_<3>) {
	string current = "123456789"; 
	string desired = "567412490"; 
	int RetVal = 5; 
	return verify_case(RetVal, BikeLock().minTurns(current, desired)); }
int Test_(Case_<4>) {
	string current = "23887547676234543215443276347856987698648735634265"; 
	string desired = "14327805497625497814327632146531429785698765309822"; 
	int RetVal = 34; 
	return verify_case(RetVal, BikeLock().minTurns(current, desired)); }

template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); }
template<>      void Run_<-1>() {}
int main() { Run_<0>(); }
// END CUT HERE