File Annotation
Not logged in
3de32bb725 2011-11-12        kinaba: #include <iostream>
3de32bb725 2011-11-12        kinaba: #include <sstream>
3de32bb725 2011-11-12        kinaba: #include <iomanip>
3de32bb725 2011-11-12        kinaba: #include <vector>
3de32bb725 2011-11-12        kinaba: #include <string>
3de32bb725 2011-11-12        kinaba: #include <map>
3de32bb725 2011-11-12        kinaba: #include <set>
3de32bb725 2011-11-12        kinaba: #include <algorithm>
3de32bb725 2011-11-12        kinaba: #include <numeric>
3de32bb725 2011-11-12        kinaba: #include <iterator>
3de32bb725 2011-11-12        kinaba: #include <functional>
3de32bb725 2011-11-12        kinaba: #include <complex>
3de32bb725 2011-11-12        kinaba: #include <queue>
3de32bb725 2011-11-12        kinaba: #include <stack>
3de32bb725 2011-11-12        kinaba: #include <cmath>
3de32bb725 2011-11-12        kinaba: #include <cassert>
3de32bb725 2011-11-12        kinaba: #include <cstring>
3de32bb725 2011-11-12        kinaba: #ifdef __GNUC__
3de32bb725 2011-11-12        kinaba: #include <ext/hash_map>
3de32bb725 2011-11-12        kinaba: #define unordered_map __gnu_cxx::hash_map
3de32bb725 2011-11-12        kinaba: #else
3de32bb725 2011-11-12        kinaba: #include <unordered_map>
3de32bb725 2011-11-12        kinaba: #endif
3de32bb725 2011-11-12        kinaba: using namespace std;
3de32bb725 2011-11-12        kinaba: typedef long long LL;
3de32bb725 2011-11-12        kinaba: typedef complex<double> CMP;
3de32bb725 2011-11-12        kinaba: 
3de32bb725 2011-11-12        kinaba: class AlphabetPaths { public:
3de32bb725 2011-11-12        kinaba: 	int H, W;
3de32bb725 2011-11-12        kinaba: 	long long count(vector <string> maze)
3de32bb725 2011-11-12        kinaba: 	{
3de32bb725 2011-11-12        kinaba: 		H = maze.size();
3de32bb725 2011-11-12        kinaba: 		W = maze[0].size();
3de32bb725 2011-11-12        kinaba: 		for(int y=0; y<H; ++y)
3de32bb725 2011-11-12        kinaba: 		for(int x=0; x<W; ++x)
3de32bb725 2011-11-12        kinaba: 			if( maze[y][x] != '.' )
3de32bb725 2011-11-12        kinaba: 				maze[y][x] = char(string("ABCDEFZHIKLMNOPQRSTVX").find(maze[y][x]));
3de32bb725 2011-11-12        kinaba: 
3de32bb725 2011-11-12        kinaba: 		LL total = 0;
3de32bb725 2011-11-12        kinaba: 		for(int y=0; y<H; ++y)
3de32bb725 2011-11-12        kinaba: 		for(int x=0; x<W; ++x)
3de32bb725 2011-11-12        kinaba: 			if( maze[y][x] != '.' ) {
3de32bb725 2011-11-12        kinaba: 				int mid = maze[y][x];
3de32bb725 2011-11-12        kinaba: 
3de32bb725 2011-11-12        kinaba: 				unordered_map<int, int> reachable;
3de32bb725 2011-11-12        kinaba: 				travel(1, 1<<mid, y, x, maze, reachable);
3de32bb725 2011-11-12        kinaba: 
3de32bb725 2011-11-12        kinaba: 				for(unordered_map<int,int>::iterator it=reachable.begin(); it!=reachable.end(); ++it)
3de32bb725 2011-11-12        kinaba: 				{
3de32bb725 2011-11-12        kinaba: 					unordered_map<int,int>::iterator kt = reachable.find((((1<<21)-1)^it->first) | (1<<mid));
3de32bb725 2011-11-12        kinaba: 					if( kt != reachable.end() )
3de32bb725 2011-11-12        kinaba: 						total += LL(it->second) * kt->second;
3de32bb725 2011-11-12        kinaba: 				}
3de32bb725 2011-11-12        kinaba: 			}
3de32bb725 2011-11-12        kinaba: 		return total;
3de32bb725 2011-11-12        kinaba: 	}
3de32bb725 2011-11-12        kinaba: 
3de32bb725 2011-11-12        kinaba: 	void travel(int n, int mask, int y, int x, const vector<string>& maze,
3de32bb725 2011-11-12        kinaba: 	            unordered_map<int,int>& reachable)
3de32bb725 2011-11-12        kinaba: 	{
3de32bb725 2011-11-12        kinaba: 		if( n == 11 ) {
3de32bb725 2011-11-12        kinaba: 			reachable[mask]++;
3de32bb725 2011-11-12        kinaba: 			return;
3de32bb725 2011-11-12        kinaba: 		}
3de32bb725 2011-11-12        kinaba: 
3de32bb725 2011-11-12        kinaba: 		static const int dy[] = {-1,+1,0,0};
3de32bb725 2011-11-12        kinaba: 		static const int dx[] = {0,0,-1,+1};
3de32bb725 2011-11-12        kinaba: 		for(int i=0; i<4; ++i) {
3de32bb725 2011-11-12        kinaba: 			int yy = y+dy[i];
3de32bb725 2011-11-12        kinaba: 			int xx = x+dx[i];
3de32bb725 2011-11-12        kinaba: 			if( 0<=yy && yy<H && 0<=xx && xx<W && maze[yy][xx]!='.' ) {
3de32bb725 2011-11-12        kinaba: 				int v = maze[yy][xx];
3de32bb725 2011-11-12        kinaba: 				if( !(mask & (1<<v)) )
3de32bb725 2011-11-12        kinaba: 					travel(n+1, mask|(1<<v), yy, xx, maze, reachable);
3de32bb725 2011-11-12        kinaba: 			}
3de32bb725 2011-11-12        kinaba: 		}
3de32bb725 2011-11-12        kinaba: 	}
3de32bb725 2011-11-12        kinaba: };
3de32bb725 2011-11-12        kinaba: 
3de32bb725 2011-11-12        kinaba: // BEGIN CUT HERE
3de32bb725 2011-11-12        kinaba: #include <ctime>
3de32bb725 2011-11-12        kinaba: double start_time; string timer()
3de32bb725 2011-11-12        kinaba:  { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
3de32bb725 2011-11-12        kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
3de32bb725 2011-11-12        kinaba:  { os << "{ ";
3de32bb725 2011-11-12        kinaba:    for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
3de32bb725 2011-11-12        kinaba:    os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
3de32bb725 2011-11-12        kinaba: void verify_case(const long long& Expected, const long long& Received) {
3de32bb725 2011-11-12        kinaba:  bool ok = (Expected == Received);
3de32bb725 2011-11-12        kinaba:  if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
3de32bb725 2011-11-12        kinaba:  cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
3de32bb725 2011-11-12        kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
3de32bb725 2011-11-12        kinaba: #define END	 verify_case(_, AlphabetPaths().count(maze));}
3de32bb725 2011-11-12        kinaba: int main(){
3de32bb725 2011-11-12        kinaba: 
3de32bb725 2011-11-12        kinaba: CASE(0)
3de32bb725 2011-11-12        kinaba: 	string maze_[] = {"ABCDEFZHIXKLMNOPQRST",
3de32bb725 2011-11-12        kinaba:  "...................V"};
3de32bb725 2011-11-12        kinaba: 	  vector <string> maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_));
3de32bb725 2011-11-12        kinaba: 	long long _ = 2LL;
3de32bb725 2011-11-12        kinaba: END
3de32bb725 2011-11-12        kinaba: CASE(1)
3de32bb725 2011-11-12        kinaba: 	string maze_[] = {".................VT.",
3de32bb725 2011-11-12        kinaba:  "....................",
3de32bb725 2011-11-12        kinaba:  "ABCDEFZHIXKLMNOPQRS.",
3de32bb725 2011-11-12        kinaba:  "..................S.",
3de32bb725 2011-11-12        kinaba:  ".................VT."};
3de32bb725 2011-11-12        kinaba: 	  vector <string> maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_));
3de32bb725 2011-11-12        kinaba: 	long long _ = 0LL;
3de32bb725 2011-11-12        kinaba: END
3de32bb725 2011-11-12        kinaba: CASE(2)
3de32bb725 2011-11-12        kinaba: 	string maze_[] = {"TBCDE.PQRSA",
3de32bb725 2011-11-12        kinaba:  "FZHIXKLMNOV"};
3de32bb725 2011-11-12        kinaba: 	  vector <string> maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_));
3de32bb725 2011-11-12        kinaba: 	long long _ = 50LL;
3de32bb725 2011-11-12        kinaba: END
3de32bb725 2011-11-12        kinaba: CASE(3)
3de32bb725 2011-11-12        kinaba: 	string maze_[] = {"ABCDEF.",
3de32bb725 2011-11-12        kinaba:  "V....Z.",
3de32bb725 2011-11-12        kinaba:  "T....H.",
3de32bb725 2011-11-12        kinaba:  "S....I.",
3de32bb725 2011-11-12        kinaba:  "R....X.",
3de32bb725 2011-11-12        kinaba:  "KLMNOPQ"};
3de32bb725 2011-11-12        kinaba: 	  vector <string> maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_));
3de32bb725 2011-11-12        kinaba: 	long long _ = 4LL;
3de32bb725 2011-11-12        kinaba: END
3de32bb725 2011-11-12        kinaba: CASE(4)
3de32bb725 2011-11-12        kinaba: 	string maze_[] = {
3de32bb725 2011-11-12        kinaba: 		"ABCDEFZHIXKLMNOPQRSTV",
3de32bb725 2011-11-12        kinaba: 		"HIXKLMNOPQRSTVABCDEFZ",
3de32bb725 2011-11-12        kinaba: 		"OPQRSTVABCDEFZHIXKLMN",
3de32bb725 2011-11-12        kinaba: 		"ABCDEFZHIXKLMNOPQRSTV",
3de32bb725 2011-11-12        kinaba: 		"HIXKLMNOPQRSTVABCDEFZ",
3de32bb725 2011-11-12        kinaba: 		"OPQRSTVABCDEFZHIXKLMN",
3de32bb725 2011-11-12        kinaba: 		"ABCDEFZHIXKLMNOPQRSTV",
3de32bb725 2011-11-12        kinaba: 		"HIXKLMNOPQRSTVABCDEFZ",
3de32bb725 2011-11-12        kinaba: 		"OPQRSTVABCDEFZHIXKLMN",
3de32bb725 2011-11-12        kinaba: 		"ABCDEFZHIXKLMNOPQRSTV",
3de32bb725 2011-11-12        kinaba: 		"HIXKLMNOPQRSTVABCDEFZ",
3de32bb725 2011-11-12        kinaba: 		"OPQRSTVABCDEFZHIXKLMN",
3de32bb725 2011-11-12        kinaba: 		"ABCDEFZHIXKLMNOPQRSTV",
3de32bb725 2011-11-12        kinaba: 		"HIXKLMNOPQRSTVABCDEFZ",
3de32bb725 2011-11-12        kinaba: 		"OPQRSTVABCDEFZHIXKLMN",
3de32bb725 2011-11-12        kinaba: 		"ABCDEFZHIXKLMNOPQRSTV",
3de32bb725 2011-11-12        kinaba: 		"HIXKLMNOPQRSTVABCDEFZ",
3de32bb725 2011-11-12        kinaba: 		"OPQRSTVABCDEFZHIXKLMN",
3de32bb725 2011-11-12        kinaba: 		"ABCDEFZHIXKLMNOPQRSTV",
3de32bb725 2011-11-12        kinaba: 		"HIXKLMNOPQRSTVABCDEFZ",
3de32bb725 2011-11-12        kinaba: 		"OPQRSTVABCDEFZHIXKLMN",
3de32bb725 2011-11-12        kinaba: 	};
3de32bb725 2011-11-12        kinaba: 	  vector <string> maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_));
3de32bb725 2011-11-12        kinaba: 	long long _ = 6119782LL;
3de32bb725 2011-11-12        kinaba: END
3de32bb725 2011-11-12        kinaba: CASE(5)
3de32bb725 2011-11-12        kinaba: 	string maze_[] = {"ABCDEFZHIKLMNOPQRSTVX", "FZHIKLMNOPQRSTVXABCDE", "LMNOPQRSTVXABCDEFZHIK", "QRSTVXABCDEFZHIKLMNOP", "XABCDEFZHIKLMNOPQRSTV", "EFZHIKLMNOPQRSTVXABCD", "KLMNOPQRSTVXABCDEFZHI", "PQRSTVXABCDEFZHIKLMNO", "VXABCDEFZHIKLMNOPQRST", "DEFZHIKLMNOPQRSTVXABC", "IKLMNOPQRSTVXABCDEFZH", "OPQRSTVXABCDEFZHIKLMN", "TVXABCDEFZHIKLMNOPQRS", "CDEFZHIKLMNOPQRSTVXAB", "HIKLMNOPQRSTVXABCDEFZ", "NOPQRSTVXABCDEFZHIKLM", "STVXABCDEFZHIKLMNOPQR", "BCDEFZHIKLMNOPQRSTVXA", "ZHIKLMNOPQRSTVXABCDEF", "MNOPQRSTVXABCDEFZHIKL", "RSTVXABCDEFZHIKLMNOPQ"};
3de32bb725 2011-11-12        kinaba: 	  vector <string> maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_));
3de32bb725 2011-11-12        kinaba: 	long long _ = 10530238LL;
3de32bb725 2011-11-12        kinaba: END
3de32bb725 2011-11-12        kinaba: }
3de32bb725 2011-11-12        kinaba: // END CUT HERE