Artifact Content
Not logged in

Artifact 83e9a8df96eca9dfa5afe1e1491e75da6d3f0521


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef complex<LD> CMP;

class FlipGame { public:
	int minOperations(vector <string> board)
	{
		int cnt = 0;
		while( !all_zero(board) ) {
			next(board);
			++cnt;
		}
		return cnt;
	}

	bool all_zero(const vector <string>& board)
	{
		for(int i=0; i<board.size(); ++i)
			if( count(board[i].begin(), board[i].end(), '0') != board[i].size() )
				return false;
		return true;
	}

	void next(vector<string>& board)
	{
		int X=0;
		for(int y=0; y<board.size(); ++y)
		{
			int last1 = X-1;
			for(int x=X; x<board[y].size(); ++x)
				if( board[y][x] == '1' )
					last1 = x;
			X = last1+1;
			for(int x=0; x<X; ++x)
				board[y][x] = char('0'+'1'-board[y][x]);
		}
	}
};

// 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> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, FlipGame().minOperations(board));}
int main(){

CASE(0)
	string board_[] = {"1000",
 "1110",
 "1111"};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int _ = 1; 
END
CASE(1)
	string board_[] = {"1111",
 "1111",
 "1111"};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int _ = 1; 
END
CASE(2)
	string board_[] = {"00",
 "00",
 "00"};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int _ = 0; 
END
CASE(3)
	string board_[] = {
 "00000000",
 "00100000",
 "01000000",
 "00001000",
 "00000000"};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int _ = 4; 
END
CASE(4)
	string board_[] = {"000000000000001100000000000000",
 "000000000000011110000000000000",
 "000000000000111111000000000000",
 "000000000001111111100000000000",
 "000000000011111111110000000000",
 "000000000111111111111000000000",
 "000000001100111111001100000000",
 "000000011000011110000110000000",
 "000000111100111111001111000000",
 "000001111111111111111111100000",
 "000011111111111111111111110000",
 "000111111111000000111111111000",
 "001111111111100001111111111100",
 "011111111111110011111111111110",
 "111111111111111111111111111111"};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int _ = 29; 
END
/*
CASE(5)
	string board_[] = ;
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int _ = ; 
END
CASE(6)
	string board_[] = ;
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int _ = ; 
END
*/
}
// END CUT HERE