f33265e95c 2014-09-05 kinaba: #include <iostream> f33265e95c 2014-09-05 kinaba: #include <sstream> f33265e95c 2014-09-05 kinaba: #include <iomanip> f33265e95c 2014-09-05 kinaba: #include <vector> f33265e95c 2014-09-05 kinaba: #include <string> f33265e95c 2014-09-05 kinaba: #include <map> f33265e95c 2014-09-05 kinaba: #include <set> f33265e95c 2014-09-05 kinaba: #include <algorithm> f33265e95c 2014-09-05 kinaba: #include <numeric> f33265e95c 2014-09-05 kinaba: #include <iterator> f33265e95c 2014-09-05 kinaba: #include <functional> f33265e95c 2014-09-05 kinaba: #include <complex> f33265e95c 2014-09-05 kinaba: #include <queue> f33265e95c 2014-09-05 kinaba: #include <stack> f33265e95c 2014-09-05 kinaba: #include <cmath> f33265e95c 2014-09-05 kinaba: #include <cassert> f33265e95c 2014-09-05 kinaba: #include <tuple> f33265e95c 2014-09-05 kinaba: using namespace std; f33265e95c 2014-09-05 kinaba: typedef long long LL; f33265e95c 2014-09-05 kinaba: typedef complex<double> CMP; f33265e95c 2014-09-05 kinaba: f33265e95c 2014-09-05 kinaba: class TaroJiroGrid { public: f33265e95c 2014-09-05 kinaba: int getNumber(vector <string> grid) f33265e95c 2014-09-05 kinaba: { f33265e95c 2014-09-05 kinaba: const int H = grid.size(); f33265e95c 2014-09-05 kinaba: const int W = grid[0].size(); f33265e95c 2014-09-05 kinaba: const string Change[2] = {string(W, 'B'), string(W, 'W')}; f33265e95c 2014-09-05 kinaba: f33265e95c 2014-09-05 kinaba: // 0 f33265e95c 2014-09-05 kinaba: if(ok(grid)) f33265e95c 2014-09-05 kinaba: return 0; f33265e95c 2014-09-05 kinaba: // 1 f33265e95c 2014-09-05 kinaba: for(int y1=0; y1<H; ++y1) for(int c1=0; c1<2; ++c1) { f33265e95c 2014-09-05 kinaba: auto t1 = grid[y1]; f33265e95c 2014-09-05 kinaba: grid[y1] = Change[c1]; f33265e95c 2014-09-05 kinaba: if(ok(grid)) return 1; f33265e95c 2014-09-05 kinaba: grid[y1] = t1; f33265e95c 2014-09-05 kinaba: } f33265e95c 2014-09-05 kinaba: return 2; f33265e95c 2014-09-05 kinaba: } f33265e95c 2014-09-05 kinaba: f33265e95c 2014-09-05 kinaba: bool ok(const vector<string>& g) f33265e95c 2014-09-05 kinaba: { f33265e95c 2014-09-05 kinaba: const int H = g.size(); f33265e95c 2014-09-05 kinaba: const int W = g[0].size(); f33265e95c 2014-09-05 kinaba: for(int x=0; x<W; ++x) { f33265e95c 2014-09-05 kinaba: int nb=0, nw=0; f33265e95c 2014-09-05 kinaba: for(int y=0; y<H; ++y) f33265e95c 2014-09-05 kinaba: { f33265e95c 2014-09-05 kinaba: if(g[y][x]=='B') nb++, nw=0; f33265e95c 2014-09-05 kinaba: else nb=0, nw++; f33265e95c 2014-09-05 kinaba: if(nw>H/2 || nb>H/2) f33265e95c 2014-09-05 kinaba: return false; f33265e95c 2014-09-05 kinaba: } f33265e95c 2014-09-05 kinaba: } f33265e95c 2014-09-05 kinaba: return true; f33265e95c 2014-09-05 kinaba: } f33265e95c 2014-09-05 kinaba: }; f33265e95c 2014-09-05 kinaba: f33265e95c 2014-09-05 kinaba: // BEGIN CUT HERE f33265e95c 2014-09-05 kinaba: #include <ctime> f33265e95c 2014-09-05 kinaba: double start_time; string timer() f33265e95c 2014-09-05 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } f33265e95c 2014-09-05 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) f33265e95c 2014-09-05 kinaba: { os << "{ "; f33265e95c 2014-09-05 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) f33265e95c 2014-09-05 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } f33265e95c 2014-09-05 kinaba: void verify_case(const int& Expected, const int& Received) { f33265e95c 2014-09-05 kinaba: bool ok = (Expected == Received); f33265e95c 2014-09-05 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; f33265e95c 2014-09-05 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } f33265e95c 2014-09-05 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); f33265e95c 2014-09-05 kinaba: #define END verify_case(_, TaroJiroGrid().getNumber(grid));} f33265e95c 2014-09-05 kinaba: int main(){ f33265e95c 2014-09-05 kinaba: f33265e95c 2014-09-05 kinaba: CASE(0) f33265e95c 2014-09-05 kinaba: string grid_[] = {"WB", f33265e95c 2014-09-05 kinaba: "BB"}; f33265e95c 2014-09-05 kinaba: vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); f33265e95c 2014-09-05 kinaba: int _ = 1; f33265e95c 2014-09-05 kinaba: END f33265e95c 2014-09-05 kinaba: CASE(1) f33265e95c 2014-09-05 kinaba: string grid_[] = {"WB", f33265e95c 2014-09-05 kinaba: "WW"}; f33265e95c 2014-09-05 kinaba: vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); f33265e95c 2014-09-05 kinaba: int _ = 1; f33265e95c 2014-09-05 kinaba: END f33265e95c 2014-09-05 kinaba: CASE(2) f33265e95c 2014-09-05 kinaba: string grid_[] = {"WB", f33265e95c 2014-09-05 kinaba: "WB"}; f33265e95c 2014-09-05 kinaba: vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); f33265e95c 2014-09-05 kinaba: int _ = 2; f33265e95c 2014-09-05 kinaba: END f33265e95c 2014-09-05 kinaba: CASE(3) f33265e95c 2014-09-05 kinaba: string grid_[] = {"WBBW", f33265e95c 2014-09-05 kinaba: "WBWB", f33265e95c 2014-09-05 kinaba: "WWBB", f33265e95c 2014-09-05 kinaba: "BWWW"}; f33265e95c 2014-09-05 kinaba: vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); f33265e95c 2014-09-05 kinaba: int _ = 2; f33265e95c 2014-09-05 kinaba: END f33265e95c 2014-09-05 kinaba: CASE(4) f33265e95c 2014-09-05 kinaba: string grid_[] = {"WBBWBB", f33265e95c 2014-09-05 kinaba: "BBWBBW", f33265e95c 2014-09-05 kinaba: "WWBWBW", f33265e95c 2014-09-05 kinaba: "BWWBBB", f33265e95c 2014-09-05 kinaba: "WBWBBW", f33265e95c 2014-09-05 kinaba: "WWWBWB"}; f33265e95c 2014-09-05 kinaba: vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); f33265e95c 2014-09-05 kinaba: int _ = 1; f33265e95c 2014-09-05 kinaba: END f33265e95c 2014-09-05 kinaba: /* f33265e95c 2014-09-05 kinaba: CASE(5) f33265e95c 2014-09-05 kinaba: string grid_[] = ; f33265e95c 2014-09-05 kinaba: vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); f33265e95c 2014-09-05 kinaba: int _ = ; f33265e95c 2014-09-05 kinaba: END f33265e95c 2014-09-05 kinaba: CASE(6) f33265e95c 2014-09-05 kinaba: string grid_[] = ; f33265e95c 2014-09-05 kinaba: vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); f33265e95c 2014-09-05 kinaba: int _ = ; f33265e95c 2014-09-05 kinaba: END f33265e95c 2014-09-05 kinaba: */ f33265e95c 2014-09-05 kinaba: } f33265e95c 2014-09-05 kinaba: // END CUT HERE