#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;
class ThirteenHard { public:
int calcTime(vector <string> city)
{
typedef int vert;
typedef int cost;
typedef pair<cost,vert> cedge;
priority_queue< cedge, vector<cedge>, greater<cedge> > Q;
Q.push( cedge(0,1*32+0) );
set<int> visited;
while( !Q.empty() )
{
cedge s = Q.top();
Q.pop();
if( visited.count(s.second) )
continue;
visited.insert(s.second);
int v = s.second%32;
int mask = s.second/32;
int c = s.first;
if( v == (int)city.size()-1 )
return c;
for(int i=0; i<city.size(); ++i)
{
char dd = city[v][i];
int d = ('a'<=dd && dd<='z' ? dd-'a'+27 : dd-'A'+1);
if( dd!='#' && d%13>0 )
{
int mask2 = mask << (d%13);
if( mask2 & (1<<13) )
continue;
mask2 = (mask2 & ((1<<13) - 1)) | (mask2 >> 13) | (1<<d%13);
int s2 = mask2*32 + i;
if( !visited.count(s2) )
Q.push( cedge(c+d, s2) );
}
}
}
return -1;
}
};
// 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> ostream& operator<<(ostream& os, const vector<T>& v)
{ os << "{ ";
for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
bool ok = (Expected == Received);
if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl;
cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END verify_case(_, ThirteenHard().calcTime(city));}
int main(){
CASE(0)
string city_[] = { "#AB##",
"###A#",
"###C#",
"####K",
"#####" };
vector <string> city(city_, city_+sizeof(city_)/sizeof(*city_));
int _ = 16;
END
CASE(1)
string city_[] = { "#Z",
"Z#" };
vector <string> city(city_, city_+sizeof(city_)/sizeof(*city_));
int _ = -1;
END
CASE(2)
string city_[] = { "Good#####",
"#Luck####",
"##and####",
"##Have###",
"####Fun##",
"#####in##",
"#####the#",
"CHALLENGE",
"##PHASE##" };
vector <string> city(city_, city_+sizeof(city_)/sizeof(*city_));
int _ = 137;
END
CASE(3)
string city_[] = { "###No#####",
"####Zaphod",
"#####Just#",
"######very",
"####very##",
"improbable",
"##########",
"##########",
"##########",
"##########" };
vector <string> city(city_, city_+sizeof(city_)/sizeof(*city_));
int _ = 103;
END
CASE(4)
string city_[] = { "#B#C##T#M",
"##K######",
"########A",
"####R####",
"#####U###",
"########C",
"#######H#",
"########S",
"#########" };
vector <string> city(city_, city_+sizeof(city_)/sizeof(*city_));
int _ = 47;
END
CASE(5)
string city_[] = {"#a","##"};
vector <string> city(city_, city_+sizeof(city_)/sizeof(*city_));
int _ = 27;
END
CASE(5)
string city_[] = {"#z","##"};
vector <string> city(city_, city_+sizeof(city_)/sizeof(*city_));
int _ = -1;
END
CASE(6)
string city_[] = {"abcdefghijklmnopqrstuvwxy","abcdefghijklmnopqrstuvwxy","abcdefghijklmnopqrstuvwxy","abcdefghijklmnopqrstuvwxy","abcdefghijklmnopqrstuvwxy",
"abcdefghijklmnopqrstuvwxy","abcdefghijklmnopqrstuvwxy","abcdefghijklmnopqrstuvwxy","abcdefghijklmnopqrstuvwxy","abcdefghijklmnopqrstuvwxy",
"abcdefghijklmnopqrstuvwxy","abcdefghijklmnopqrstuvwxy","abcdefghijklmnopqrstuvwxy","abcdefghijklmnopqrstuvwxy","abcdefghijklmnopqrstuvwxy",
"abcdefghijklmnopqrstuvwxy","abcdefghijklmnopqrstuvwxy","abcdefghijklmnopqrstuvwxy","abcdefghijklmnopqrstuvwxy","abcdefghijklmnopqrstuvwxy",
"abcdefghijklmnopqrstuvwxy","abcdefghijklmnopqrstuvwxy","abcdefghijklmnopqrstuvwxy","abcdefghijklmnopqrstuvwxy","abcdefghijklmnopqrstuvwxy"};
vector <string> city(city_, city_+sizeof(city_)/sizeof(*city_));
int _ = -999;
END
}
// END CUT HERE