f1d5bbf0f5 2012-08-04 kinaba: #include <iostream> f1d5bbf0f5 2012-08-04 kinaba: #include <sstream> f1d5bbf0f5 2012-08-04 kinaba: #include <iomanip> f1d5bbf0f5 2012-08-04 kinaba: #include <vector> f1d5bbf0f5 2012-08-04 kinaba: #include <string> f1d5bbf0f5 2012-08-04 kinaba: #include <map> f1d5bbf0f5 2012-08-04 kinaba: #include <set> f1d5bbf0f5 2012-08-04 kinaba: #include <algorithm> f1d5bbf0f5 2012-08-04 kinaba: #include <numeric> f1d5bbf0f5 2012-08-04 kinaba: #include <iterator> f1d5bbf0f5 2012-08-04 kinaba: #include <functional> f1d5bbf0f5 2012-08-04 kinaba: #include <complex> f1d5bbf0f5 2012-08-04 kinaba: #include <queue> f1d5bbf0f5 2012-08-04 kinaba: #include <stack> f1d5bbf0f5 2012-08-04 kinaba: #include <cmath> f1d5bbf0f5 2012-08-04 kinaba: #include <cassert> f1d5bbf0f5 2012-08-04 kinaba: using namespace std; f1d5bbf0f5 2012-08-04 kinaba: typedef long long LL; f1d5bbf0f5 2012-08-04 kinaba: typedef long double LD; f1d5bbf0f5 2012-08-04 kinaba: typedef complex<LD> CMP; f1d5bbf0f5 2012-08-04 kinaba: f1d5bbf0f5 2012-08-04 kinaba: class RotatingBot { public: f1d5bbf0f5 2012-08-04 kinaba: int minArea(vector <int> moves) f1d5bbf0f5 2012-08-04 kinaba: { f1d5bbf0f5 2012-08-04 kinaba: if( moves.size() == 1 ) { f1d5bbf0f5 2012-08-04 kinaba: int w = moves[0]+1; f1d5bbf0f5 2012-08-04 kinaba: return w; f1d5bbf0f5 2012-08-04 kinaba: } f1d5bbf0f5 2012-08-04 kinaba: if( moves.size() == 2 ) { f1d5bbf0f5 2012-08-04 kinaba: int w = moves[0]+1; f1d5bbf0f5 2012-08-04 kinaba: int h = moves[1]+1; f1d5bbf0f5 2012-08-04 kinaba: return w*h; f1d5bbf0f5 2012-08-04 kinaba: } f1d5bbf0f5 2012-08-04 kinaba: if( moves.size() == 3 ) { f1d5bbf0f5 2012-08-04 kinaba: int w1 = moves[0]+1; f1d5bbf0f5 2012-08-04 kinaba: int h = moves[1]+1; f1d5bbf0f5 2012-08-04 kinaba: int w2 = moves[2]+1; f1d5bbf0f5 2012-08-04 kinaba: return max(w1,w2)*h; f1d5bbf0f5 2012-08-04 kinaba: } f1d5bbf0f5 2012-08-04 kinaba: if( moves.size() == 4 ) { f1d5bbf0f5 2012-08-04 kinaba: int w1 = moves[0]+1; f1d5bbf0f5 2012-08-04 kinaba: int h1 = moves[1]+1; f1d5bbf0f5 2012-08-04 kinaba: int w2 = moves[2]+1; f1d5bbf0f5 2012-08-04 kinaba: int h2 = moves[3]+1; f1d5bbf0f5 2012-08-04 kinaba: if( w1 > w2 ) f1d5bbf0f5 2012-08-04 kinaba: return -1; f1d5bbf0f5 2012-08-04 kinaba: if( w1 == w2 ) { f1d5bbf0f5 2012-08-04 kinaba: if( h1 <= h2 ) f1d5bbf0f5 2012-08-04 kinaba: return -1; f1d5bbf0f5 2012-08-04 kinaba: else f1d5bbf0f5 2012-08-04 kinaba: return w1*h1; f1d5bbf0f5 2012-08-04 kinaba: } f1d5bbf0f5 2012-08-04 kinaba: return w2*max(h1,h2); f1d5bbf0f5 2012-08-04 kinaba: } f1d5bbf0f5 2012-08-04 kinaba: f1d5bbf0f5 2012-08-04 kinaba: int w1 = moves[0]+1; f1d5bbf0f5 2012-08-04 kinaba: int h1 = moves[1]+1; f1d5bbf0f5 2012-08-04 kinaba: int w2 = moves[2]+1; f1d5bbf0f5 2012-08-04 kinaba: int h2 = moves[3]+1; f1d5bbf0f5 2012-08-04 kinaba: if( w1 > w2 ) f1d5bbf0f5 2012-08-04 kinaba: return -1; f1d5bbf0f5 2012-08-04 kinaba: if( w1 == w2 ) { f1d5bbf0f5 2012-08-04 kinaba: vector<int> rm = simulate(w1, h1, 0, 0); f1d5bbf0f5 2012-08-04 kinaba: if( match(rm, moves) ) f1d5bbf0f5 2012-08-04 kinaba: return w1*h1; f1d5bbf0f5 2012-08-04 kinaba: else f1d5bbf0f5 2012-08-04 kinaba: return -1; f1d5bbf0f5 2012-08-04 kinaba: } else { f1d5bbf0f5 2012-08-04 kinaba: if( h2<h1 ) f1d5bbf0f5 2012-08-04 kinaba: return -1; f1d5bbf0f5 2012-08-04 kinaba: vector<int> rm = simulate(w2, h2, w2-w1, h2-h1); f1d5bbf0f5 2012-08-04 kinaba: if( match(rm, moves) ) f1d5bbf0f5 2012-08-04 kinaba: return w2*h2; f1d5bbf0f5 2012-08-04 kinaba: else f1d5bbf0f5 2012-08-04 kinaba: return -1; f1d5bbf0f5 2012-08-04 kinaba: } f1d5bbf0f5 2012-08-04 kinaba: } f1d5bbf0f5 2012-08-04 kinaba: f1d5bbf0f5 2012-08-04 kinaba: vector<int> simulate(int w, int h, int x, int y) f1d5bbf0f5 2012-08-04 kinaba: { f1d5bbf0f5 2012-08-04 kinaba: vector<int> moves; f1d5bbf0f5 2012-08-04 kinaba: vector< vector<bool> > vis(h, vector<bool>(w)); f1d5bbf0f5 2012-08-04 kinaba: int dx[]={+1,0,-1,0}; f1d5bbf0f5 2012-08-04 kinaba: int dy[]={0,+1,0,-1}; f1d5bbf0f5 2012-08-04 kinaba: int d = 0; f1d5bbf0f5 2012-08-04 kinaba: vis[y][x] = true; f1d5bbf0f5 2012-08-04 kinaba: for(;;) { f1d5bbf0f5 2012-08-04 kinaba: int cnt = 0; f1d5bbf0f5 2012-08-04 kinaba: for(;;) { f1d5bbf0f5 2012-08-04 kinaba: int xx=x+dx[d]; f1d5bbf0f5 2012-08-04 kinaba: int yy=y+dy[d]; f1d5bbf0f5 2012-08-04 kinaba: if( yy<0||h<=yy||xx<0||w<=xx||vis[yy][xx] ) f1d5bbf0f5 2012-08-04 kinaba: break; f1d5bbf0f5 2012-08-04 kinaba: y=yy, x=xx; f1d5bbf0f5 2012-08-04 kinaba: vis[y][x] = true; f1d5bbf0f5 2012-08-04 kinaba: ++cnt; f1d5bbf0f5 2012-08-04 kinaba: } f1d5bbf0f5 2012-08-04 kinaba: d = (d+1)%4; f1d5bbf0f5 2012-08-04 kinaba: if( cnt == 0 ) f1d5bbf0f5 2012-08-04 kinaba: break; f1d5bbf0f5 2012-08-04 kinaba: moves.push_back(cnt); f1d5bbf0f5 2012-08-04 kinaba: } f1d5bbf0f5 2012-08-04 kinaba: return moves; f1d5bbf0f5 2012-08-04 kinaba: } f1d5bbf0f5 2012-08-04 kinaba: f1d5bbf0f5 2012-08-04 kinaba: bool match(const vector<int>& rm, const vector<int>& m) f1d5bbf0f5 2012-08-04 kinaba: { f1d5bbf0f5 2012-08-04 kinaba: if(rm.size() < m.size()) f1d5bbf0f5 2012-08-04 kinaba: return false; f1d5bbf0f5 2012-08-04 kinaba: for(int i=0; i+1<m.size(); ++i) f1d5bbf0f5 2012-08-04 kinaba: if(rm[i] != m[i]) f1d5bbf0f5 2012-08-04 kinaba: return false; f1d5bbf0f5 2012-08-04 kinaba: return rm[m.size()-1] >= m[m.size()-1]; f1d5bbf0f5 2012-08-04 kinaba: } f1d5bbf0f5 2012-08-04 kinaba: }; f1d5bbf0f5 2012-08-04 kinaba: f1d5bbf0f5 2012-08-04 kinaba: // BEGIN CUT HERE f1d5bbf0f5 2012-08-04 kinaba: #include <ctime> f1d5bbf0f5 2012-08-04 kinaba: double start_time; string timer() f1d5bbf0f5 2012-08-04 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } f1d5bbf0f5 2012-08-04 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) f1d5bbf0f5 2012-08-04 kinaba: { os << "{ "; f1d5bbf0f5 2012-08-04 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) f1d5bbf0f5 2012-08-04 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } f1d5bbf0f5 2012-08-04 kinaba: void verify_case(const int& Expected, const int& Received) { f1d5bbf0f5 2012-08-04 kinaba: bool ok = (Expected == Received); f1d5bbf0f5 2012-08-04 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; f1d5bbf0f5 2012-08-04 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } f1d5bbf0f5 2012-08-04 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); f1d5bbf0f5 2012-08-04 kinaba: #define END verify_case(_, RotatingBot().minArea(moves));} f1d5bbf0f5 2012-08-04 kinaba: int main(){ f1d5bbf0f5 2012-08-04 kinaba: f1d5bbf0f5 2012-08-04 kinaba: CASE(0) f1d5bbf0f5 2012-08-04 kinaba: int moves_[] = {15}; f1d5bbf0f5 2012-08-04 kinaba: vector <int> moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); f1d5bbf0f5 2012-08-04 kinaba: int _ = 16; f1d5bbf0f5 2012-08-04 kinaba: END f1d5bbf0f5 2012-08-04 kinaba: CASE(1) f1d5bbf0f5 2012-08-04 kinaba: int moves_[] = {3,10}; f1d5bbf0f5 2012-08-04 kinaba: vector <int> moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); f1d5bbf0f5 2012-08-04 kinaba: int _ = 44; f1d5bbf0f5 2012-08-04 kinaba: END f1d5bbf0f5 2012-08-04 kinaba: CASE(2) f1d5bbf0f5 2012-08-04 kinaba: int moves_[] = {1,1,1,1}; f1d5bbf0f5 2012-08-04 kinaba: vector <int> moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); f1d5bbf0f5 2012-08-04 kinaba: int _ = -1; f1d5bbf0f5 2012-08-04 kinaba: END f1d5bbf0f5 2012-08-04 kinaba: CASE(3) f1d5bbf0f5 2012-08-04 kinaba: int moves_[] = {9,5,11,10,11,4,10}; f1d5bbf0f5 2012-08-04 kinaba: vector <int> moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); f1d5bbf0f5 2012-08-04 kinaba: int _ = 132; f1d5bbf0f5 2012-08-04 kinaba: END f1d5bbf0f5 2012-08-04 kinaba: CASE(4) f1d5bbf0f5 2012-08-04 kinaba: int moves_[] = {12,1,27,14,27,12,26,11,25,10,24,9,23,8,22,7,21,6,20,5,19,4,18,3,17,2,16,1,15}; f1d5bbf0f5 2012-08-04 kinaba: vector <int> moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); f1d5bbf0f5 2012-08-04 kinaba: int _ = 420; f1d5bbf0f5 2012-08-04 kinaba: END f1d5bbf0f5 2012-08-04 kinaba: CASE(5) f1d5bbf0f5 2012-08-04 kinaba: int moves_[] = {8,6,6,1}; f1d5bbf0f5 2012-08-04 kinaba: vector <int> moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); f1d5bbf0f5 2012-08-04 kinaba: int _ = -1; f1d5bbf0f5 2012-08-04 kinaba: END f1d5bbf0f5 2012-08-04 kinaba: CASE(6) f1d5bbf0f5 2012-08-04 kinaba: int moves_[] = {8,6,6}; f1d5bbf0f5 2012-08-04 kinaba: vector <int> moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); f1d5bbf0f5 2012-08-04 kinaba: int _ = 63; f1d5bbf0f5 2012-08-04 kinaba: END f1d5bbf0f5 2012-08-04 kinaba: CASE(7) f1d5bbf0f5 2012-08-04 kinaba: int moves_[] = {5,4,5,3,3}; f1d5bbf0f5 2012-08-04 kinaba: vector <int> moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); f1d5bbf0f5 2012-08-04 kinaba: int _ = 30; f1d5bbf0f5 2012-08-04 kinaba: END f1d5bbf0f5 2012-08-04 kinaba: CASE(8) f1d5bbf0f5 2012-08-04 kinaba: int moves_[] = {2,2,2,1}; f1d5bbf0f5 2012-08-04 kinaba: vector <int> moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); f1d5bbf0f5 2012-08-04 kinaba: int _ = 9; f1d5bbf0f5 2012-08-04 kinaba: END f1d5bbf0f5 2012-08-04 kinaba: /* f1d5bbf0f5 2012-08-04 kinaba: CASE(9) f1d5bbf0f5 2012-08-04 kinaba: int moves_[] = ; f1d5bbf0f5 2012-08-04 kinaba: vector <int> moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); f1d5bbf0f5 2012-08-04 kinaba: int _ = ; f1d5bbf0f5 2012-08-04 kinaba: END f1d5bbf0f5 2012-08-04 kinaba: */ f1d5bbf0f5 2012-08-04 kinaba: } f1d5bbf0f5 2012-08-04 kinaba: // END CUT HERE