Artifact Content
Not logged in

Artifact ac1374745693c0580f23b72ff7ad8c1cebd5ce39


#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>
#ifdef __GNUC__
#include <ext/hash_map>
#define unordered_map __gnu_cxx::hash_map
#else
#include <unordered_map>
#endif
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class AlphabetPaths { public:
	int H, W;
	long long count(vector <string> maze)
	{
		H = maze.size();
		W = maze[0].size();
		for(int y=0; y<H; ++y)
		for(int x=0; x<W; ++x)
			if( maze[y][x] != '.' )
				maze[y][x] = char(string("ABCDEFZHIKLMNOPQRSTVX").find(maze[y][x]));

		LL total = 0;
		for(int y=0; y<H; ++y)
		for(int x=0; x<W; ++x)
			if( maze[y][x] != '.' ) {
				int mid = maze[y][x];

				unordered_map<int, int> reachable;
				travel(1, 1<<mid, y, x, maze, reachable);

				for(unordered_map<int,int>::iterator it=reachable.begin(); it!=reachable.end(); ++it)
				{
					unordered_map<int,int>::iterator kt = reachable.find((((1<<21)-1)^it->first) | (1<<mid));
					if( kt != reachable.end() )
						total += LL(it->second) * kt->second;
				}
			}
		return total;
	}

	void travel(int n, int mask, int y, int x, const vector<string>& maze,
	            unordered_map<int,int>& reachable)
	{
		if( n == 11 ) {
			reachable[mask]++;
			return;
		}

		static const int dy[] = {-1,+1,0,0};
		static const int dx[] = {0,0,-1,+1};
		for(int i=0; i<4; ++i) {
			int yy = y+dy[i];
			int xx = x+dx[i];
			if( 0<=yy && yy<H && 0<=xx && xx<W && maze[yy][xx]!='.' ) {
				int v = maze[yy][xx];
				if( !(mask & (1<<v)) )
					travel(n+1, mask|(1<<v), yy, xx, maze, reachable);
			}
		}
	}
};

// 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 long long& Expected, const long long& 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(_, AlphabetPaths().count(maze));}
int main(){

CASE(0)
	string maze_[] = {"ABCDEFZHIXKLMNOPQRST",
 "...................V"};
	  vector <string> maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_)); 
	long long _ = 2LL; 
END
CASE(1)
	string maze_[] = {".................VT.",
 "....................",
 "ABCDEFZHIXKLMNOPQRS.",
 "..................S.",
 ".................VT."};
	  vector <string> maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_)); 
	long long _ = 0LL; 
END
CASE(2)
	string maze_[] = {"TBCDE.PQRSA", 
 "FZHIXKLMNOV"};
	  vector <string> maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_)); 
	long long _ = 50LL; 
END
CASE(3)
	string maze_[] = {"ABCDEF.",
 "V....Z.",
 "T....H.",
 "S....I.",
 "R....X.",
 "KLMNOPQ"};
	  vector <string> maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_)); 
	long long _ = 4LL; 
END
CASE(4)
	string maze_[] = {
		"ABCDEFZHIXKLMNOPQRSTV",
		"HIXKLMNOPQRSTVABCDEFZ",
		"OPQRSTVABCDEFZHIXKLMN",
		"ABCDEFZHIXKLMNOPQRSTV",
		"HIXKLMNOPQRSTVABCDEFZ",
		"OPQRSTVABCDEFZHIXKLMN",
		"ABCDEFZHIXKLMNOPQRSTV",
		"HIXKLMNOPQRSTVABCDEFZ",
		"OPQRSTVABCDEFZHIXKLMN",
		"ABCDEFZHIXKLMNOPQRSTV",
		"HIXKLMNOPQRSTVABCDEFZ",
		"OPQRSTVABCDEFZHIXKLMN",
		"ABCDEFZHIXKLMNOPQRSTV",
		"HIXKLMNOPQRSTVABCDEFZ",
		"OPQRSTVABCDEFZHIXKLMN",
		"ABCDEFZHIXKLMNOPQRSTV",
		"HIXKLMNOPQRSTVABCDEFZ",
		"OPQRSTVABCDEFZHIXKLMN",
		"ABCDEFZHIXKLMNOPQRSTV",
		"HIXKLMNOPQRSTVABCDEFZ",
		"OPQRSTVABCDEFZHIXKLMN",
	};
	  vector <string> maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_)); 
	long long _ = 6119782LL; 
END
CASE(5)
	string maze_[] = {"ABCDEFZHIKLMNOPQRSTVX", "FZHIKLMNOPQRSTVXABCDE", "LMNOPQRSTVXABCDEFZHIK", "QRSTVXABCDEFZHIKLMNOP", "XABCDEFZHIKLMNOPQRSTV", "EFZHIKLMNOPQRSTVXABCD", "KLMNOPQRSTVXABCDEFZHI", "PQRSTVXABCDEFZHIKLMNO", "VXABCDEFZHIKLMNOPQRST", "DEFZHIKLMNOPQRSTVXABC", "IKLMNOPQRSTVXABCDEFZH", "OPQRSTVXABCDEFZHIKLMN", "TVXABCDEFZHIKLMNOPQRS", "CDEFZHIKLMNOPQRSTVXAB", "HIKLMNOPQRSTVXABCDEFZ", "NOPQRSTVXABCDEFZHIKLM", "STVXABCDEFZHIKLMNOPQR", "BCDEFZHIKLMNOPQRSTVXA", "ZHIKLMNOPQRSTVXABCDEF", "MNOPQRSTVXABCDEFZHIKL", "RSTVXABCDEFZHIKLMNOPQ"};
	  vector <string> maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_)); 
	long long _ = 10530238LL; 
END
}
// END CUT HERE