2adfb7dfc0 2013-04-20 kinaba: #include <iostream> 2adfb7dfc0 2013-04-20 kinaba: #include <sstream> 2adfb7dfc0 2013-04-20 kinaba: #include <iomanip> 2adfb7dfc0 2013-04-20 kinaba: #include <vector> 2adfb7dfc0 2013-04-20 kinaba: #include <string> 2adfb7dfc0 2013-04-20 kinaba: #include <map> 2adfb7dfc0 2013-04-20 kinaba: #include <set> 2adfb7dfc0 2013-04-20 kinaba: #include <algorithm> 2adfb7dfc0 2013-04-20 kinaba: #include <numeric> 2adfb7dfc0 2013-04-20 kinaba: #include <iterator> 2adfb7dfc0 2013-04-20 kinaba: #include <functional> 2adfb7dfc0 2013-04-20 kinaba: #include <complex> 2adfb7dfc0 2013-04-20 kinaba: #include <queue> 2adfb7dfc0 2013-04-20 kinaba: #include <stack> 2adfb7dfc0 2013-04-20 kinaba: #include <cmath> 2adfb7dfc0 2013-04-20 kinaba: #include <cassert> 2adfb7dfc0 2013-04-20 kinaba: using namespace std; 2adfb7dfc0 2013-04-20 kinaba: typedef long long LL; 2adfb7dfc0 2013-04-20 kinaba: typedef long double LD; 2adfb7dfc0 2013-04-20 kinaba: typedef complex<LD> CMP; 2adfb7dfc0 2013-04-20 kinaba: 2adfb7dfc0 2013-04-20 kinaba: class ArcadeManao { public: 2adfb7dfc0 2013-04-20 kinaba: int shortestLadder(vector <string> level, int coinRow, int coinColumn) 2adfb7dfc0 2013-04-20 kinaba: { 2adfb7dfc0 2013-04-20 kinaba: for(int L=0; L<=level.size(); ++L) 2adfb7dfc0 2013-04-20 kinaba: if(can(level, coinRow-1, coinColumn-1, L)) 2adfb7dfc0 2013-04-20 kinaba: return L; 2adfb7dfc0 2013-04-20 kinaba: return level.size(); 2adfb7dfc0 2013-04-20 kinaba: } 2adfb7dfc0 2013-04-20 kinaba: 2adfb7dfc0 2013-04-20 kinaba: bool can(vector <string> level, int Cy, int Cx, int L) 2adfb7dfc0 2013-04-20 kinaba: { 2adfb7dfc0 2013-04-20 kinaba: int H = level.size(); 2adfb7dfc0 2013-04-20 kinaba: int W = level[0].size(); 2adfb7dfc0 2013-04-20 kinaba: typedef pair<int,int> point; 2adfb7dfc0 2013-04-20 kinaba: set<point> V; 2adfb7dfc0 2013-04-20 kinaba: queue<point> Q; 2adfb7dfc0 2013-04-20 kinaba: Q.push(point(H-1, 0)); 2adfb7dfc0 2013-04-20 kinaba: V.insert(Q.front()); 2adfb7dfc0 2013-04-20 kinaba: while(!Q.empty()) { 2adfb7dfc0 2013-04-20 kinaba: point p = Q.front(); 2adfb7dfc0 2013-04-20 kinaba: int y=p.first, x=p.second; 2adfb7dfc0 2013-04-20 kinaba: Q.pop(); 2adfb7dfc0 2013-04-20 kinaba: if(y==Cy && x==Cx) 2adfb7dfc0 2013-04-20 kinaba: return true; 2adfb7dfc0 2013-04-20 kinaba: vector<point> cand; 2adfb7dfc0 2013-04-20 kinaba: cand.push_back(point(y,x-1)); 2adfb7dfc0 2013-04-20 kinaba: cand.push_back(point(y,x+1)); 2adfb7dfc0 2013-04-20 kinaba: for(int dy=-L; dy<=+L; ++dy) 2adfb7dfc0 2013-04-20 kinaba: cand.push_back(point(y+dy, x)); 2adfb7dfc0 2013-04-20 kinaba: 2adfb7dfc0 2013-04-20 kinaba: for(int i=0; i<cand.size(); ++i) { 2adfb7dfc0 2013-04-20 kinaba: int yy = cand[i].first; 2adfb7dfc0 2013-04-20 kinaba: int xx = cand[i].second; 2adfb7dfc0 2013-04-20 kinaba: point pp(yy,xx); 2adfb7dfc0 2013-04-20 kinaba: if(0<=yy&&yy<H&&0<=xx&&xx<W&&level[y][x]=='X'&&!V.count(pp)) { 2adfb7dfc0 2013-04-20 kinaba: Q.push(pp); 2adfb7dfc0 2013-04-20 kinaba: V.insert(pp); 2adfb7dfc0 2013-04-20 kinaba: } 2adfb7dfc0 2013-04-20 kinaba: } 2adfb7dfc0 2013-04-20 kinaba: } 2adfb7dfc0 2013-04-20 kinaba: return false; 2adfb7dfc0 2013-04-20 kinaba: } 2adfb7dfc0 2013-04-20 kinaba: }; 2adfb7dfc0 2013-04-20 kinaba: 2adfb7dfc0 2013-04-20 kinaba: // BEGIN CUT HERE 2adfb7dfc0 2013-04-20 kinaba: #include <ctime> 2adfb7dfc0 2013-04-20 kinaba: double start_time; string timer() 2adfb7dfc0 2013-04-20 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 2adfb7dfc0 2013-04-20 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 2adfb7dfc0 2013-04-20 kinaba: { os << "{ "; 2adfb7dfc0 2013-04-20 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 2adfb7dfc0 2013-04-20 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 2adfb7dfc0 2013-04-20 kinaba: void verify_case(const int& Expected, const int& Received) { 2adfb7dfc0 2013-04-20 kinaba: bool ok = (Expected == Received); 2adfb7dfc0 2013-04-20 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 2adfb7dfc0 2013-04-20 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 2adfb7dfc0 2013-04-20 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 2adfb7dfc0 2013-04-20 kinaba: #define END verify_case(_, ArcadeManao().shortestLadder(level, coinRow, coinColumn));} 2adfb7dfc0 2013-04-20 kinaba: int main(){ 2adfb7dfc0 2013-04-20 kinaba: 2adfb7dfc0 2013-04-20 kinaba: CASE(0) 2adfb7dfc0 2013-04-20 kinaba: string level_[] = {"XXXX....", 2adfb7dfc0 2013-04-20 kinaba: "...X.XXX", 2adfb7dfc0 2013-04-20 kinaba: "XXX..X..", 2adfb7dfc0 2013-04-20 kinaba: "......X.", 2adfb7dfc0 2013-04-20 kinaba: "XXXXXXXX"}; 2adfb7dfc0 2013-04-20 kinaba: vector <string> level(level_, level_+sizeof(level_)/sizeof(*level_)); 2adfb7dfc0 2013-04-20 kinaba: int coinRow = 2; 2adfb7dfc0 2013-04-20 kinaba: int coinColumn = 4; 2adfb7dfc0 2013-04-20 kinaba: int _ = 2; 2adfb7dfc0 2013-04-20 kinaba: END 2adfb7dfc0 2013-04-20 kinaba: CASE(1) 2adfb7dfc0 2013-04-20 kinaba: string level_[] = {"XXXX", 2adfb7dfc0 2013-04-20 kinaba: "...X", 2adfb7dfc0 2013-04-20 kinaba: "XXXX"}; 2adfb7dfc0 2013-04-20 kinaba: vector <string> level(level_, level_+sizeof(level_)/sizeof(*level_)); 2adfb7dfc0 2013-04-20 kinaba: int coinRow = 1; 2adfb7dfc0 2013-04-20 kinaba: int coinColumn = 1; 2adfb7dfc0 2013-04-20 kinaba: int _ = 1; 2adfb7dfc0 2013-04-20 kinaba: END 2adfb7dfc0 2013-04-20 kinaba: CASE(2) 2adfb7dfc0 2013-04-20 kinaba: string level_[] = {"..X..", 2adfb7dfc0 2013-04-20 kinaba: ".X.X.", 2adfb7dfc0 2013-04-20 kinaba: "X...X", 2adfb7dfc0 2013-04-20 kinaba: ".X.X.", 2adfb7dfc0 2013-04-20 kinaba: "..X..", 2adfb7dfc0 2013-04-20 kinaba: "XXXXX"}; 2adfb7dfc0 2013-04-20 kinaba: vector <string> level(level_, level_+sizeof(level_)/sizeof(*level_)); 2adfb7dfc0 2013-04-20 kinaba: int coinRow = 1; 2adfb7dfc0 2013-04-20 kinaba: int coinColumn = 3; 2adfb7dfc0 2013-04-20 kinaba: int _ = 4; 2adfb7dfc0 2013-04-20 kinaba: END 2adfb7dfc0 2013-04-20 kinaba: CASE(3) 2adfb7dfc0 2013-04-20 kinaba: string level_[] = {"X"}; 2adfb7dfc0 2013-04-20 kinaba: vector <string> level(level_, level_+sizeof(level_)/sizeof(*level_)); 2adfb7dfc0 2013-04-20 kinaba: int coinRow = 1; 2adfb7dfc0 2013-04-20 kinaba: int coinColumn = 1; 2adfb7dfc0 2013-04-20 kinaba: int _ = 0; 2adfb7dfc0 2013-04-20 kinaba: END 2adfb7dfc0 2013-04-20 kinaba: CASE(4) 2adfb7dfc0 2013-04-20 kinaba: string level_[] = {"XXXXXXXXXX", 2adfb7dfc0 2013-04-20 kinaba: "...X......", 2adfb7dfc0 2013-04-20 kinaba: "XXX.......", 2adfb7dfc0 2013-04-20 kinaba: "X.....XXXX", 2adfb7dfc0 2013-04-20 kinaba: "..XXXXX..X", 2adfb7dfc0 2013-04-20 kinaba: ".........X", 2adfb7dfc0 2013-04-20 kinaba: ".........X", 2adfb7dfc0 2013-04-20 kinaba: "XXXXXXXXXX"}; 2adfb7dfc0 2013-04-20 kinaba: vector <string> level(level_, level_+sizeof(level_)/sizeof(*level_)); 2adfb7dfc0 2013-04-20 kinaba: int coinRow = 1; 2adfb7dfc0 2013-04-20 kinaba: int coinColumn = 1; 2adfb7dfc0 2013-04-20 kinaba: int _ = 2; 2adfb7dfc0 2013-04-20 kinaba: END 2adfb7dfc0 2013-04-20 kinaba: CASE(5) 2adfb7dfc0 2013-04-20 kinaba: string level_[] = { 2adfb7dfc0 2013-04-20 kinaba: "XXXXXXXXXXXXXXX", 2adfb7dfc0 2013-04-20 kinaba: "..............X", 2adfb7dfc0 2013-04-20 kinaba: "..............X", 2adfb7dfc0 2013-04-20 kinaba: "..............X", 2adfb7dfc0 2013-04-20 kinaba: "XXXXXXXXXXXXXXX", 2adfb7dfc0 2013-04-20 kinaba: }; 2adfb7dfc0 2013-04-20 kinaba: vector <string> level(level_, level_+sizeof(level_)/sizeof(*level_)); 2adfb7dfc0 2013-04-20 kinaba: int coinRow = 1; 2adfb7dfc0 2013-04-20 kinaba: int coinColumn = 1; 2adfb7dfc0 2013-04-20 kinaba: int _ = 1; 2adfb7dfc0 2013-04-20 kinaba: END 2adfb7dfc0 2013-04-20 kinaba: /* 2adfb7dfc0 2013-04-20 kinaba: CASE(6) 2adfb7dfc0 2013-04-20 kinaba: string level_[] = ; 2adfb7dfc0 2013-04-20 kinaba: vector <string> level(level_, level_+sizeof(level_)/sizeof(*level_)); 2adfb7dfc0 2013-04-20 kinaba: int coinRow = ; 2adfb7dfc0 2013-04-20 kinaba: int coinColumn = ; 2adfb7dfc0 2013-04-20 kinaba: int _ = ; 2adfb7dfc0 2013-04-20 kinaba: END 2adfb7dfc0 2013-04-20 kinaba: */ 2adfb7dfc0 2013-04-20 kinaba: } 2adfb7dfc0 2013-04-20 kinaba: // END CUT HERE