0b26cb3f9a 2012-05-29 kinaba: #include <iostream> 0b26cb3f9a 2012-05-29 kinaba: #include <sstream> 0b26cb3f9a 2012-05-29 kinaba: #include <iomanip> 0b26cb3f9a 2012-05-29 kinaba: #include <vector> 0b26cb3f9a 2012-05-29 kinaba: #include <string> 0b26cb3f9a 2012-05-29 kinaba: #include <map> 0b26cb3f9a 2012-05-29 kinaba: #include <set> 0b26cb3f9a 2012-05-29 kinaba: #include <algorithm> 0b26cb3f9a 2012-05-29 kinaba: #include <numeric> 0b26cb3f9a 2012-05-29 kinaba: #include <iterator> 0b26cb3f9a 2012-05-29 kinaba: #include <functional> 0b26cb3f9a 2012-05-29 kinaba: #include <complex> 0b26cb3f9a 2012-05-29 kinaba: #include <queue> 0b26cb3f9a 2012-05-29 kinaba: #include <stack> 0b26cb3f9a 2012-05-29 kinaba: #include <cmath> 0b26cb3f9a 2012-05-29 kinaba: #include <cassert> 0b26cb3f9a 2012-05-29 kinaba: using namespace std; 0b26cb3f9a 2012-05-29 kinaba: typedef long long LL; 0b26cb3f9a 2012-05-29 kinaba: typedef long double LD; 0b26cb3f9a 2012-05-29 kinaba: typedef complex<LD> CMP; 0b26cb3f9a 2012-05-29 kinaba: 0b26cb3f9a 2012-05-29 kinaba: class FlipGame { public: 0b26cb3f9a 2012-05-29 kinaba: int minOperations(vector <string> board) 0b26cb3f9a 2012-05-29 kinaba: { 0b26cb3f9a 2012-05-29 kinaba: int cnt = 0; 0b26cb3f9a 2012-05-29 kinaba: while( !all_zero(board) ) { 0b26cb3f9a 2012-05-29 kinaba: next(board); 0b26cb3f9a 2012-05-29 kinaba: ++cnt; 0b26cb3f9a 2012-05-29 kinaba: } 0b26cb3f9a 2012-05-29 kinaba: return cnt; 0b26cb3f9a 2012-05-29 kinaba: } 0b26cb3f9a 2012-05-29 kinaba: 0b26cb3f9a 2012-05-29 kinaba: bool all_zero(const vector <string>& board) 0b26cb3f9a 2012-05-29 kinaba: { 0b26cb3f9a 2012-05-29 kinaba: for(int i=0; i<board.size(); ++i) 0b26cb3f9a 2012-05-29 kinaba: if( count(board[i].begin(), board[i].end(), '0') != board[i].size() ) 0b26cb3f9a 2012-05-29 kinaba: return false; 0b26cb3f9a 2012-05-29 kinaba: return true; 0b26cb3f9a 2012-05-29 kinaba: } 0b26cb3f9a 2012-05-29 kinaba: 0b26cb3f9a 2012-05-29 kinaba: void next(vector<string>& board) 0b26cb3f9a 2012-05-29 kinaba: { 0b26cb3f9a 2012-05-29 kinaba: int X=0; 0b26cb3f9a 2012-05-29 kinaba: for(int y=0; y<board.size(); ++y) 0b26cb3f9a 2012-05-29 kinaba: { 0b26cb3f9a 2012-05-29 kinaba: int last1 = X-1; 0b26cb3f9a 2012-05-29 kinaba: for(int x=X; x<board[y].size(); ++x) 0b26cb3f9a 2012-05-29 kinaba: if( board[y][x] == '1' ) 0b26cb3f9a 2012-05-29 kinaba: last1 = x; 0b26cb3f9a 2012-05-29 kinaba: X = last1+1; 0b26cb3f9a 2012-05-29 kinaba: for(int x=0; x<X; ++x) 0b26cb3f9a 2012-05-29 kinaba: board[y][x] = char('0'+'1'-board[y][x]); 0b26cb3f9a 2012-05-29 kinaba: } 0b26cb3f9a 2012-05-29 kinaba: } 0b26cb3f9a 2012-05-29 kinaba: }; 0b26cb3f9a 2012-05-29 kinaba: 0b26cb3f9a 2012-05-29 kinaba: // BEGIN CUT HERE 0b26cb3f9a 2012-05-29 kinaba: #include <ctime> 0b26cb3f9a 2012-05-29 kinaba: double start_time; string timer() 0b26cb3f9a 2012-05-29 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 0b26cb3f9a 2012-05-29 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 0b26cb3f9a 2012-05-29 kinaba: { os << "{ "; 0b26cb3f9a 2012-05-29 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 0b26cb3f9a 2012-05-29 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 0b26cb3f9a 2012-05-29 kinaba: void verify_case(const int& Expected, const int& Received) { 0b26cb3f9a 2012-05-29 kinaba: bool ok = (Expected == Received); 0b26cb3f9a 2012-05-29 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 0b26cb3f9a 2012-05-29 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 0b26cb3f9a 2012-05-29 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 0b26cb3f9a 2012-05-29 kinaba: #define END verify_case(_, FlipGame().minOperations(board));} 0b26cb3f9a 2012-05-29 kinaba: int main(){ 0b26cb3f9a 2012-05-29 kinaba: 0b26cb3f9a 2012-05-29 kinaba: CASE(0) 0b26cb3f9a 2012-05-29 kinaba: string board_[] = {"1000", 0b26cb3f9a 2012-05-29 kinaba: "1110", 0b26cb3f9a 2012-05-29 kinaba: "1111"}; 0b26cb3f9a 2012-05-29 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 0b26cb3f9a 2012-05-29 kinaba: int _ = 1; 0b26cb3f9a 2012-05-29 kinaba: END 0b26cb3f9a 2012-05-29 kinaba: CASE(1) 0b26cb3f9a 2012-05-29 kinaba: string board_[] = {"1111", 0b26cb3f9a 2012-05-29 kinaba: "1111", 0b26cb3f9a 2012-05-29 kinaba: "1111"}; 0b26cb3f9a 2012-05-29 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 0b26cb3f9a 2012-05-29 kinaba: int _ = 1; 0b26cb3f9a 2012-05-29 kinaba: END 0b26cb3f9a 2012-05-29 kinaba: CASE(2) 0b26cb3f9a 2012-05-29 kinaba: string board_[] = {"00", 0b26cb3f9a 2012-05-29 kinaba: "00", 0b26cb3f9a 2012-05-29 kinaba: "00"}; 0b26cb3f9a 2012-05-29 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 0b26cb3f9a 2012-05-29 kinaba: int _ = 0; 0b26cb3f9a 2012-05-29 kinaba: END 0b26cb3f9a 2012-05-29 kinaba: CASE(3) 0b26cb3f9a 2012-05-29 kinaba: string board_[] = { 0b26cb3f9a 2012-05-29 kinaba: "00000000", 0b26cb3f9a 2012-05-29 kinaba: "00100000", 0b26cb3f9a 2012-05-29 kinaba: "01000000", 0b26cb3f9a 2012-05-29 kinaba: "00001000", 0b26cb3f9a 2012-05-29 kinaba: "00000000"}; 0b26cb3f9a 2012-05-29 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 0b26cb3f9a 2012-05-29 kinaba: int _ = 4; 0b26cb3f9a 2012-05-29 kinaba: END 0b26cb3f9a 2012-05-29 kinaba: CASE(4) 0b26cb3f9a 2012-05-29 kinaba: string board_[] = {"000000000000001100000000000000", 0b26cb3f9a 2012-05-29 kinaba: "000000000000011110000000000000", 0b26cb3f9a 2012-05-29 kinaba: "000000000000111111000000000000", 0b26cb3f9a 2012-05-29 kinaba: "000000000001111111100000000000", 0b26cb3f9a 2012-05-29 kinaba: "000000000011111111110000000000", 0b26cb3f9a 2012-05-29 kinaba: "000000000111111111111000000000", 0b26cb3f9a 2012-05-29 kinaba: "000000001100111111001100000000", 0b26cb3f9a 2012-05-29 kinaba: "000000011000011110000110000000", 0b26cb3f9a 2012-05-29 kinaba: "000000111100111111001111000000", 0b26cb3f9a 2012-05-29 kinaba: "000001111111111111111111100000", 0b26cb3f9a 2012-05-29 kinaba: "000011111111111111111111110000", 0b26cb3f9a 2012-05-29 kinaba: "000111111111000000111111111000", 0b26cb3f9a 2012-05-29 kinaba: "001111111111100001111111111100", 0b26cb3f9a 2012-05-29 kinaba: "011111111111110011111111111110", 0b26cb3f9a 2012-05-29 kinaba: "111111111111111111111111111111"}; 0b26cb3f9a 2012-05-29 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 0b26cb3f9a 2012-05-29 kinaba: int _ = 29; 0b26cb3f9a 2012-05-29 kinaba: END 0b26cb3f9a 2012-05-29 kinaba: /* 0b26cb3f9a 2012-05-29 kinaba: CASE(5) 0b26cb3f9a 2012-05-29 kinaba: string board_[] = ; 0b26cb3f9a 2012-05-29 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 0b26cb3f9a 2012-05-29 kinaba: int _ = ; 0b26cb3f9a 2012-05-29 kinaba: END 0b26cb3f9a 2012-05-29 kinaba: CASE(6) 0b26cb3f9a 2012-05-29 kinaba: string board_[] = ; 0b26cb3f9a 2012-05-29 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 0b26cb3f9a 2012-05-29 kinaba: int _ = ; 0b26cb3f9a 2012-05-29 kinaba: END 0b26cb3f9a 2012-05-29 kinaba: */ 0b26cb3f9a 2012-05-29 kinaba: } 0b26cb3f9a 2012-05-29 kinaba: // END CUT HERE