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 <functional> 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: #include <cstring> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: typedef complex<double> CMP; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int memo[1024][30]; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class FourBlocks { public: 4fd800b3a8 2011-02-23 kinaba: int X, Y; 4fd800b3a8 2011-02-23 kinaba: vector<string> G; 4fd800b3a8 2011-02-23 kinaba: int maxScore(vector <string> grid) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: X = grid.size(); 4fd800b3a8 2011-02-23 kinaba: Y = grid[0].size(); 4fd800b3a8 2011-02-23 kinaba: G = grid; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // sentinels 4fd800b3a8 2011-02-23 kinaba: for(int x=0; x<X; ++x) 4fd800b3a8 2011-02-23 kinaba: G[x] += '0'; 4fd800b3a8 2011-02-23 kinaba: G.push_back( string(Y+1,'0') ); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int baseScore = 0; 4fd800b3a8 2011-02-23 kinaba: for(int x=0; x<X; ++x) 4fd800b3a8 2011-02-23 kinaba: for(int y=0; y<Y; ++y) 4fd800b3a8 2011-02-23 kinaba: baseScore += (G[x][y]=='1'); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // memoized search 4fd800b3a8 2011-02-23 kinaba: memset(memo, 0xff, sizeof(memo)); 4fd800b3a8 2011-02-23 kinaba: return baseScore + best(0); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int best(int y) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( Y <= y ) 4fd800b3a8 2011-02-23 kinaba: return 0; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int mask = 0; 4fd800b3a8 2011-02-23 kinaba: for(int x=0; x<X; ++x) 4fd800b3a8 2011-02-23 kinaba: mask |= ((G[x][y]=='.') << x); 4fd800b3a8 2011-02-23 kinaba: if( memo[mask][y] != -1 ) 4fd800b3a8 2011-02-23 kinaba: return memo[mask][y]; 4fd800b3a8 2011-02-23 kinaba: return memo[mask][y] = best(0, y); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int best(int x, int y) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( X <= x ) 4fd800b3a8 2011-02-23 kinaba: return best(y+1); 4fd800b3a8 2011-02-23 kinaba: int Max = 0; 4fd800b3a8 2011-02-23 kinaba: if( G[x][y]=='.' ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( G[x+1][y]=='.' && G[x][y+1]=='.' && G[x+1][y+1]=='.' ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: // can put 4 4fd800b3a8 2011-02-23 kinaba: G[x][y]=G[x+1][y]=G[x][y+1]=G[x+1][y+1] = '4'; 4fd800b3a8 2011-02-23 kinaba: Max = max(Max, 16+best(x+2, y)); 4fd800b3a8 2011-02-23 kinaba: G[x][y]=G[x+1][y]=G[x][y+1]=G[x+1][y+1] = '.'; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: // can put 1 4fd800b3a8 2011-02-23 kinaba: G[x][y] = '1'; 4fd800b3a8 2011-02-23 kinaba: Max = max(Max, 1+best(x+1, y)); 4fd800b3a8 2011-02-23 kinaba: G[x][y] = '.'; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: else 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: Max = max(Max, best(x+1, y)); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return Max; 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() 4fd800b3a8 2011-02-23 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 4fd800b3a8 2011-02-23 kinaba: { os << "{ "; 4fd800b3a8 2011-02-23 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 4fd800b3a8 2011-02-23 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 4fd800b3a8 2011-02-23 kinaba: void verify_case(const int& Expected, const int& Received) { 4fd800b3a8 2011-02-23 kinaba: bool ok = (Expected == Received); 4fd800b3a8 2011-02-23 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 4fd800b3a8 2011-02-23 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 4fd800b3a8 2011-02-23 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 4fd800b3a8 2011-02-23 kinaba: #define END verify_case(_, FourBlocks().maxScore(grid));} 4fd800b3a8 2011-02-23 kinaba: int main(){ 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: CASE(0) 4fd800b3a8 2011-02-23 kinaba: string grid_[] = {".....1..1..", 4fd800b3a8 2011-02-23 kinaba: "..1.....1.."}; 4fd800b3a8 2011-02-23 kinaba: vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 70; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(1) 4fd800b3a8 2011-02-23 kinaba: string grid_[] = {"...1.", 4fd800b3a8 2011-02-23 kinaba: ".....", 4fd800b3a8 2011-02-23 kinaba: ".1..1", 4fd800b3a8 2011-02-23 kinaba: ".....", 4fd800b3a8 2011-02-23 kinaba: "1...."}; 4fd800b3a8 2011-02-23 kinaba: vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 73; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(2) 4fd800b3a8 2011-02-23 kinaba: string grid_[] = {"...1.", 4fd800b3a8 2011-02-23 kinaba: ".1...", 4fd800b3a8 2011-02-23 kinaba: "..1.1", 4fd800b3a8 2011-02-23 kinaba: "1...."}; 4fd800b3a8 2011-02-23 kinaba: vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 20; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(3) 4fd800b3a8 2011-02-23 kinaba: string grid_[] = {".....1...", 4fd800b3a8 2011-02-23 kinaba: ".....1...", 4fd800b3a8 2011-02-23 kinaba: "111111111", 4fd800b3a8 2011-02-23 kinaba: ".....1...", 4fd800b3a8 2011-02-23 kinaba: ".....1..."}; 4fd800b3a8 2011-02-23 kinaba: vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 117; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(4) 4fd800b3a8 2011-02-23 kinaba: string grid_[] = {".........................", 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: ".........................", 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: vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 117; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(5) 4fd800b3a8 2011-02-23 kinaba: string grid_[] = { 4fd800b3a8 2011-02-23 kinaba: "1...", "....", "..11", 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 36; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(6) 4fd800b3a8 2011-02-23 kinaba: string grid_[] = { 4fd800b3a8 2011-02-23 kinaba: "1..", "...", "..1", "..1", 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 36; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE