Artifact Content
Not logged in

Artifact 77edbded42c604a61e79215c4a4eabac1477e227


#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;

typedef pair<int,int> vert;
typedef int cost;

// dijkstra
template<typename OutIt>
void neighbors(cost e[50][50], int Y, int X, vert v, bool vis[50][50], OutIt out)
{
	typedef pair<cost,vert> cedge;
	priority_queue< cedge, vector<cedge>, greater<cedge> > Q;
	Q.push( cedge(e[v.first][v.second],v) );

	set<vert> visited_now;
	while( !Q.empty() )
	{
		cedge ce = Q.top(); Q.pop();
		cost cc = ce.first;
		vert cv = ce.second;
		if( visited_now.count(cv) )
			continue;
		visited_now.insert(cv);

		if( !vis[cv.first][cv.second] ) {
			*out++ = cv;
			vis[cv.first][cv.second] = true;
		}

		// for 4 neibs
		int dy[]={0,0,-1,+1};
		int dx[]={-1,+1,0,0};
		for(int i=0; i<4; ++i)
		{
			int yyy = cv.first  + dy[i];
			int xxx = cv.second + dx[i];
			if( 0<=yyy && yyy<Y && 0<=xxx && xxx<X )
			{
				int ccc = cc + e[yyy][xxx];
				vert vvv(yyy, xxx);
				if( ccc<=9*8*7*6*5*4*3*2*1 )
					Q.push( cedge(ccc, vvv) );
			}
		}
	}
}

int bfs( cost e[50][50], int Y, int X, vert s, vert d )
{
	vector<vert> Q(1, s);
	bool vis[50][50] = {}; vis[s.first][s.second] = true;

	for(int step=0; !Q.empty(); ++step)
	{
		vector<vert> Q2;
		for(int i=0; i!=Q.size(); ++i)
		{
			if( Q[i] == d )
				return step;
			neighbors(e, Y, X, Q[i], vis, back_inserter(Q2));
		}
		Q.swap(Q2);
	}
	return -1;
}

class ExpensiveTravel
{
public:
	int minTime(vector <string> m, int startRow, int startCol, int endRow, int endCol) 
	{
		int Y = m.size(), X = m[0].size();
		int Sy = startRow-1, Sx = startCol-1;
		int Ey = endRow-1,   Ex = endCol-1;
		cost e[50][50];
		for(int y=0; y<Y; ++y)
		for(int x=0; x<X; ++x)
			e[y][x] = 9*8*7*6*5*4*3*2*1 / (m[y][x]-'0');

		return bfs( e, Y, X, vert(Sy,Sx), vert(Ey,Ex) );
	}
};

// 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 m_[] = {"22334"};
	  vector <string> m(m_, m_+sizeof(m_)/sizeof(*m_)); 
	int startRow = 1; 
	int startCol = 1; 
	int endRow = 1; 
	int endCol = 5; 
	int RetVal = 3; 
	return verify_case(RetVal, ExpensiveTravel().minTime(m, startRow, startCol, endRow, endCol)); }
int Test_(Case_<1>) {
	string m_[] = {"55",
 "52",
 "55"};
	  vector <string> m(m_, m_+sizeof(m_)/sizeof(*m_)); 
	int startRow = 1; 
	int startCol = 2; 
	int endRow = 3; 
	int endCol = 2; 
	int RetVal = 1; 
	return verify_case(RetVal, ExpensiveTravel().minTime(m, startRow, startCol, endRow, endCol)); }
int Test_(Case_<2>) {
	string m_[] = {"251",
 "212",
 "122"};
	  vector <string> m(m_, m_+sizeof(m_)/sizeof(*m_)); 
	int startRow = 1; 
	int startCol = 1; 
	int endRow = 3; 
	int endCol = 3; 
	int RetVal = -1; 
	return verify_case(RetVal, ExpensiveTravel().minTime(m, startRow, startCol, endRow, endCol)); }
int Test_(Case_<3>) {
	string m_[] = {"452232",
 "287979",
 "219872",
 "928234",
 "767676"};
	  vector <string> m(m_, m_+sizeof(m_)/sizeof(*m_)); 
	int startRow = 1; 
	int startCol = 6; 
	int endRow = 3; 
	int endCol = 1; 
	int RetVal = 3; 
	return verify_case(RetVal, ExpensiveTravel().minTime(m, startRow, startCol, endRow, endCol)); }
int Test_(Case_<4>) {
	string m_[] = {"11"};
	  vector <string> m(m_, m_+sizeof(m_)/sizeof(*m_)); 
	int startRow = 1; 
	int startCol = 1; 
	int endRow = 1; 
	int endCol = 2; 
	int RetVal = -1; 
	return verify_case(RetVal, ExpensiveTravel().minTime(m, startRow, startCol, endRow, endCol)); }
int Test_(Case_<5>) {
	string m_[] = {"123456789987654321"};
	  vector <string> m(m_, m_+sizeof(m_)/sizeof(*m_)); 
	int startRow = 1; 
	int startCol = 2; 
	int endRow = 1; 
	int endCol = 16; 
	int RetVal = 5; 
	return verify_case(RetVal, ExpensiveTravel().minTime(m, startRow, startCol, endRow, endCol)); }

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