File Annotation
Not logged in
4fd800b3a8 2011-02-23        kinaba: #include <iostream>
4fd800b3a8 2011-02-23        kinaba: #include <sstream>
4fd800b3a8 2011-02-23        kinaba: #include <iomanip>
4fd800b3a8 2011-02-23        kinaba: #include <vector>
4fd800b3a8 2011-02-23        kinaba: #include <string>
4fd800b3a8 2011-02-23        kinaba: #include <map>
4fd800b3a8 2011-02-23        kinaba: #include <set>
4fd800b3a8 2011-02-23        kinaba: #include <algorithm>
4fd800b3a8 2011-02-23        kinaba: #include <numeric>
4fd800b3a8 2011-02-23        kinaba: #include <iterator>
4fd800b3a8 2011-02-23        kinaba: #include <complex>
4fd800b3a8 2011-02-23        kinaba: #include <queue>
4fd800b3a8 2011-02-23        kinaba: #include <stack>
4fd800b3a8 2011-02-23        kinaba: #include <cmath>
4fd800b3a8 2011-02-23        kinaba: #include <cassert>
4fd800b3a8 2011-02-23        kinaba: using namespace std;
4fd800b3a8 2011-02-23        kinaba: typedef long long LL;
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: typedef pair<int,int> vert;
4fd800b3a8 2011-02-23        kinaba: typedef int cost;
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: // dijkstra
4fd800b3a8 2011-02-23        kinaba: template<typename OutIt>
4fd800b3a8 2011-02-23        kinaba: void neighbors(cost e[50][50], int Y, int X, vert v, bool vis[50][50], OutIt out)
4fd800b3a8 2011-02-23        kinaba: {
4fd800b3a8 2011-02-23        kinaba: 	typedef pair<cost,vert> cedge;
4fd800b3a8 2011-02-23        kinaba: 	priority_queue< cedge, vector<cedge>, greater<cedge> > Q;
4fd800b3a8 2011-02-23        kinaba: 	Q.push( cedge(e[v.first][v.second],v) );
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: 	set<vert> visited_now;
4fd800b3a8 2011-02-23        kinaba: 	while( !Q.empty() )
4fd800b3a8 2011-02-23        kinaba: 	{
4fd800b3a8 2011-02-23        kinaba: 		cedge ce = Q.top(); Q.pop();
4fd800b3a8 2011-02-23        kinaba: 		cost cc = ce.first;
4fd800b3a8 2011-02-23        kinaba: 		vert cv = ce.second;
4fd800b3a8 2011-02-23        kinaba: 		if( visited_now.count(cv) )
4fd800b3a8 2011-02-23        kinaba: 			continue;
4fd800b3a8 2011-02-23        kinaba: 		visited_now.insert(cv);
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: 		if( !vis[cv.first][cv.second] ) {
4fd800b3a8 2011-02-23        kinaba: 			*out++ = cv;
4fd800b3a8 2011-02-23        kinaba: 			vis[cv.first][cv.second] = true;
4fd800b3a8 2011-02-23        kinaba: 		}
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: 		// for 4 neibs
4fd800b3a8 2011-02-23        kinaba: 		int dy[]={0,0,-1,+1};
4fd800b3a8 2011-02-23        kinaba: 		int dx[]={-1,+1,0,0};
4fd800b3a8 2011-02-23        kinaba: 		for(int i=0; i<4; ++i)
4fd800b3a8 2011-02-23        kinaba: 		{
4fd800b3a8 2011-02-23        kinaba: 			int yyy = cv.first  + dy[i];
4fd800b3a8 2011-02-23        kinaba: 			int xxx = cv.second + dx[i];
4fd800b3a8 2011-02-23        kinaba: 			if( 0<=yyy && yyy<Y && 0<=xxx && xxx<X )
4fd800b3a8 2011-02-23        kinaba: 			{
4fd800b3a8 2011-02-23        kinaba: 				int ccc = cc + e[yyy][xxx];
4fd800b3a8 2011-02-23        kinaba: 				vert vvv(yyy, xxx);
4fd800b3a8 2011-02-23        kinaba: 				if( ccc<=9*8*7*6*5*4*3*2*1 )
4fd800b3a8 2011-02-23        kinaba: 					Q.push( cedge(ccc, vvv) );
4fd800b3a8 2011-02-23        kinaba: 			}
4fd800b3a8 2011-02-23        kinaba: 		}
4fd800b3a8 2011-02-23        kinaba: 	}
4fd800b3a8 2011-02-23        kinaba: }
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: int bfs( cost e[50][50], int Y, int X, vert s, vert d )
4fd800b3a8 2011-02-23        kinaba: {
4fd800b3a8 2011-02-23        kinaba: 	vector<vert> Q(1, s);
4fd800b3a8 2011-02-23        kinaba: 	bool vis[50][50] = {}; vis[s.first][s.second] = true;
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: 	for(int step=0; !Q.empty(); ++step)
4fd800b3a8 2011-02-23        kinaba: 	{
4fd800b3a8 2011-02-23        kinaba: 		vector<vert> Q2;
4fd800b3a8 2011-02-23        kinaba: 		for(int i=0; i!=Q.size(); ++i)
4fd800b3a8 2011-02-23        kinaba: 		{
4fd800b3a8 2011-02-23        kinaba: 			if( Q[i] == d )
4fd800b3a8 2011-02-23        kinaba: 				return step;
4fd800b3a8 2011-02-23        kinaba: 			neighbors(e, Y, X, Q[i], vis, back_inserter(Q2));
4fd800b3a8 2011-02-23        kinaba: 		}
4fd800b3a8 2011-02-23        kinaba: 		Q.swap(Q2);
4fd800b3a8 2011-02-23        kinaba: 	}
4fd800b3a8 2011-02-23        kinaba: 	return -1;
4fd800b3a8 2011-02-23        kinaba: }
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: class ExpensiveTravel
4fd800b3a8 2011-02-23        kinaba: {
4fd800b3a8 2011-02-23        kinaba: public:
4fd800b3a8 2011-02-23        kinaba: 	int minTime(vector <string> m, int startRow, int startCol, int endRow, int endCol)
4fd800b3a8 2011-02-23        kinaba: 	{
4fd800b3a8 2011-02-23        kinaba: 		int Y = m.size(), X = m[0].size();
4fd800b3a8 2011-02-23        kinaba: 		int Sy = startRow-1, Sx = startCol-1;
4fd800b3a8 2011-02-23        kinaba: 		int Ey = endRow-1,   Ex = endCol-1;
4fd800b3a8 2011-02-23        kinaba: 		cost e[50][50];
4fd800b3a8 2011-02-23        kinaba: 		for(int y=0; y<Y; ++y)
4fd800b3a8 2011-02-23        kinaba: 		for(int x=0; x<X; ++x)
4fd800b3a8 2011-02-23        kinaba: 			e[y][x] = 9*8*7*6*5*4*3*2*1 / (m[y][x]-'0');
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: 		return bfs( e, Y, X, vert(Sy,Sx), vert(Ey,Ex) );
4fd800b3a8 2011-02-23        kinaba: 	}
4fd800b3a8 2011-02-23        kinaba: };
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: // BEGIN CUT HERE
4fd800b3a8 2011-02-23        kinaba: #include <ctime>
4fd800b3a8 2011-02-23        kinaba: double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: 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(); }
4fd800b3a8 2011-02-23        kinaba: 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;}
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: template<int N> struct Case_ { Case_(){start_time=clock();} };
4fd800b3a8 2011-02-23        kinaba: char Test_(...);
4fd800b3a8 2011-02-23        kinaba: int Test_(Case_<0>) {
4fd800b3a8 2011-02-23        kinaba: 	string m_[] = {"22334"};
4fd800b3a8 2011-02-23        kinaba: 	  vector <string> m(m_, m_+sizeof(m_)/sizeof(*m_));
4fd800b3a8 2011-02-23        kinaba: 	int startRow = 1;
4fd800b3a8 2011-02-23        kinaba: 	int startCol = 1;
4fd800b3a8 2011-02-23        kinaba: 	int endRow = 1;
4fd800b3a8 2011-02-23        kinaba: 	int endCol = 5;
4fd800b3a8 2011-02-23        kinaba: 	int RetVal = 3;
4fd800b3a8 2011-02-23        kinaba: 	return verify_case(RetVal, ExpensiveTravel().minTime(m, startRow, startCol, endRow, endCol)); }
4fd800b3a8 2011-02-23        kinaba: int Test_(Case_<1>) {
4fd800b3a8 2011-02-23        kinaba: 	string m_[] = {"55",
4fd800b3a8 2011-02-23        kinaba:  "52",
4fd800b3a8 2011-02-23        kinaba:  "55"};
4fd800b3a8 2011-02-23        kinaba: 	  vector <string> m(m_, m_+sizeof(m_)/sizeof(*m_));
4fd800b3a8 2011-02-23        kinaba: 	int startRow = 1;
4fd800b3a8 2011-02-23        kinaba: 	int startCol = 2;
4fd800b3a8 2011-02-23        kinaba: 	int endRow = 3;
4fd800b3a8 2011-02-23        kinaba: 	int endCol = 2;
4fd800b3a8 2011-02-23        kinaba: 	int RetVal = 1;
4fd800b3a8 2011-02-23        kinaba: 	return verify_case(RetVal, ExpensiveTravel().minTime(m, startRow, startCol, endRow, endCol)); }
4fd800b3a8 2011-02-23        kinaba: int Test_(Case_<2>) {
4fd800b3a8 2011-02-23        kinaba: 	string m_[] = {"251",
4fd800b3a8 2011-02-23        kinaba:  "212",
4fd800b3a8 2011-02-23        kinaba:  "122"};
4fd800b3a8 2011-02-23        kinaba: 	  vector <string> m(m_, m_+sizeof(m_)/sizeof(*m_));
4fd800b3a8 2011-02-23        kinaba: 	int startRow = 1;
4fd800b3a8 2011-02-23        kinaba: 	int startCol = 1;
4fd800b3a8 2011-02-23        kinaba: 	int endRow = 3;
4fd800b3a8 2011-02-23        kinaba: 	int endCol = 3;
4fd800b3a8 2011-02-23        kinaba: 	int RetVal = -1;
4fd800b3a8 2011-02-23        kinaba: 	return verify_case(RetVal, ExpensiveTravel().minTime(m, startRow, startCol, endRow, endCol)); }
4fd800b3a8 2011-02-23        kinaba: int Test_(Case_<3>) {
4fd800b3a8 2011-02-23        kinaba: 	string m_[] = {"452232",
4fd800b3a8 2011-02-23        kinaba:  "287979",
4fd800b3a8 2011-02-23        kinaba:  "219872",
4fd800b3a8 2011-02-23        kinaba:  "928234",
4fd800b3a8 2011-02-23        kinaba:  "767676"};
4fd800b3a8 2011-02-23        kinaba: 	  vector <string> m(m_, m_+sizeof(m_)/sizeof(*m_));
4fd800b3a8 2011-02-23        kinaba: 	int startRow = 1;
4fd800b3a8 2011-02-23        kinaba: 	int startCol = 6;
4fd800b3a8 2011-02-23        kinaba: 	int endRow = 3;
4fd800b3a8 2011-02-23        kinaba: 	int endCol = 1;
4fd800b3a8 2011-02-23        kinaba: 	int RetVal = 3;
4fd800b3a8 2011-02-23        kinaba: 	return verify_case(RetVal, ExpensiveTravel().minTime(m, startRow, startCol, endRow, endCol)); }
4fd800b3a8 2011-02-23        kinaba: int Test_(Case_<4>) {
4fd800b3a8 2011-02-23        kinaba: 	string m_[] = {"11"};
4fd800b3a8 2011-02-23        kinaba: 	  vector <string> m(m_, m_+sizeof(m_)/sizeof(*m_));
4fd800b3a8 2011-02-23        kinaba: 	int startRow = 1;
4fd800b3a8 2011-02-23        kinaba: 	int startCol = 1;
4fd800b3a8 2011-02-23        kinaba: 	int endRow = 1;
4fd800b3a8 2011-02-23        kinaba: 	int endCol = 2;
4fd800b3a8 2011-02-23        kinaba: 	int RetVal = -1;
4fd800b3a8 2011-02-23        kinaba: 	return verify_case(RetVal, ExpensiveTravel().minTime(m, startRow, startCol, endRow, endCol)); }
4fd800b3a8 2011-02-23        kinaba: int Test_(Case_<5>) {
4fd800b3a8 2011-02-23        kinaba: 	string m_[] = {"123456789987654321"};
4fd800b3a8 2011-02-23        kinaba: 	  vector <string> m(m_, m_+sizeof(m_)/sizeof(*m_));
4fd800b3a8 2011-02-23        kinaba: 	int startRow = 1;
4fd800b3a8 2011-02-23        kinaba: 	int startCol = 2;
4fd800b3a8 2011-02-23        kinaba: 	int endRow = 1;
4fd800b3a8 2011-02-23        kinaba: 	int endCol = 16;
4fd800b3a8 2011-02-23        kinaba: 	int RetVal = 5;
4fd800b3a8 2011-02-23        kinaba: 	return verify_case(RetVal, ExpensiveTravel().minTime(m, startRow, startCol, endRow, endCol)); }
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); }
4fd800b3a8 2011-02-23        kinaba: template<>      void Run_<-1>() {}
4fd800b3a8 2011-02-23        kinaba: int main() { Run_<0>(); }
4fd800b3a8 2011-02-23        kinaba: // END CUT HERE
4fd800b3a8 2011-02-23        kinaba: