892b20bec8 2015-04-28 kinaba: #include <iostream> 892b20bec8 2015-04-28 kinaba: #include <sstream> 892b20bec8 2015-04-28 kinaba: #include <iomanip> 892b20bec8 2015-04-28 kinaba: #include <vector> 892b20bec8 2015-04-28 kinaba: #include <string> 892b20bec8 2015-04-28 kinaba: #include <map> 892b20bec8 2015-04-28 kinaba: #include <set> 892b20bec8 2015-04-28 kinaba: #include <algorithm> 892b20bec8 2015-04-28 kinaba: #include <numeric> 892b20bec8 2015-04-28 kinaba: #include <iterator> 892b20bec8 2015-04-28 kinaba: #include <functional> 892b20bec8 2015-04-28 kinaba: #include <complex> 892b20bec8 2015-04-28 kinaba: #include <queue> 892b20bec8 2015-04-28 kinaba: #include <stack> 892b20bec8 2015-04-28 kinaba: #include <cmath> 892b20bec8 2015-04-28 kinaba: #include <cassert> 892b20bec8 2015-04-28 kinaba: #include <tuple> 892b20bec8 2015-04-28 kinaba: using namespace std; 892b20bec8 2015-04-28 kinaba: typedef long long LL; 892b20bec8 2015-04-28 kinaba: typedef complex<double> CMP; 892b20bec8 2015-04-28 kinaba: 892b20bec8 2015-04-28 kinaba: class BichromePainting { public: 892b20bec8 2015-04-28 kinaba: string isThatPossible(vector <string> board, int k) 892b20bec8 2015-04-28 kinaba: { 892b20bec8 2015-04-28 kinaba: return solve(board, k) ? "Possible" : "Impossible"; 892b20bec8 2015-04-28 kinaba: } 892b20bec8 2015-04-28 kinaba: 892b20bec8 2015-04-28 kinaba: bool solve(vector<string> board, int K) 892b20bec8 2015-04-28 kinaba: { 892b20bec8 2015-04-28 kinaba: while(!no_black(board)) 892b20bec8 2015-04-28 kinaba: if(!step_back(board, K)) 892b20bec8 2015-04-28 kinaba: return false; 892b20bec8 2015-04-28 kinaba: return true; 892b20bec8 2015-04-28 kinaba: } 892b20bec8 2015-04-28 kinaba: 892b20bec8 2015-04-28 kinaba: bool no_black(const vector<string>& board) 892b20bec8 2015-04-28 kinaba: { 892b20bec8 2015-04-28 kinaba: const int H = board.size(); 892b20bec8 2015-04-28 kinaba: const int W = board[0].size(); 892b20bec8 2015-04-28 kinaba: for(int y=0; y<H; ++y) 892b20bec8 2015-04-28 kinaba: for(int x=0; x<W; ++x) if(board[y][x] == 'B') 892b20bec8 2015-04-28 kinaba: return false; 892b20bec8 2015-04-28 kinaba: return true; 892b20bec8 2015-04-28 kinaba: } 892b20bec8 2015-04-28 kinaba: 892b20bec8 2015-04-28 kinaba: bool step_back(vector<string>& board, int K) { 892b20bec8 2015-04-28 kinaba: bool updated = false; 892b20bec8 2015-04-28 kinaba: 892b20bec8 2015-04-28 kinaba: const int H = board.size(); 892b20bec8 2015-04-28 kinaba: const int W = board[0].size(); 892b20bec8 2015-04-28 kinaba: for(int y=0; y+K<=H; ++y) 892b20bec8 2015-04-28 kinaba: for(int x=0; x+K<=W; ++x) { 892b20bec8 2015-04-28 kinaba: bool has_white = false; 892b20bec8 2015-04-28 kinaba: bool has_black = false; 892b20bec8 2015-04-28 kinaba: for(int dy=0; dy<K; ++dy) 892b20bec8 2015-04-28 kinaba: for(int dx=0; dx<K; ++dx) { 892b20bec8 2015-04-28 kinaba: if(board[y+dy][x+dx]=='W') has_white=true; 892b20bec8 2015-04-28 kinaba: if(board[y+dy][x+dx]=='B') has_black=true; 892b20bec8 2015-04-28 kinaba: } 892b20bec8 2015-04-28 kinaba: if(has_white && has_black) 892b20bec8 2015-04-28 kinaba: continue; 892b20bec8 2015-04-28 kinaba: if(has_white || has_black) { 892b20bec8 2015-04-28 kinaba: for(int dy=0; dy<K; ++dy) 892b20bec8 2015-04-28 kinaba: for(int dx=0; dx<K; ++dx) 892b20bec8 2015-04-28 kinaba: board[y+dy][x+dx] = '*'; 892b20bec8 2015-04-28 kinaba: updated = true; 892b20bec8 2015-04-28 kinaba: } 892b20bec8 2015-04-28 kinaba: } 892b20bec8 2015-04-28 kinaba: return updated; 892b20bec8 2015-04-28 kinaba: } 892b20bec8 2015-04-28 kinaba: }; 892b20bec8 2015-04-28 kinaba: 892b20bec8 2015-04-28 kinaba: // BEGIN CUT HERE 892b20bec8 2015-04-28 kinaba: #include <ctime> 892b20bec8 2015-04-28 kinaba: double start_time; string timer() 892b20bec8 2015-04-28 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 892b20bec8 2015-04-28 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 892b20bec8 2015-04-28 kinaba: { os << "{ "; 892b20bec8 2015-04-28 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 892b20bec8 2015-04-28 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 892b20bec8 2015-04-28 kinaba: void verify_case(const string& Expected, const string& Received) { 892b20bec8 2015-04-28 kinaba: bool ok = (Expected == Received); 892b20bec8 2015-04-28 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 892b20bec8 2015-04-28 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 892b20bec8 2015-04-28 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 892b20bec8 2015-04-28 kinaba: #define END verify_case(_, BichromePainting().isThatPossible(board, k));} 892b20bec8 2015-04-28 kinaba: int main(){ 892b20bec8 2015-04-28 kinaba: 892b20bec8 2015-04-28 kinaba: CASE(0) 892b20bec8 2015-04-28 kinaba: string board_[] = {"BBBW", 892b20bec8 2015-04-28 kinaba: "BWWW", 892b20bec8 2015-04-28 kinaba: "BWWW", 892b20bec8 2015-04-28 kinaba: "WWWW"}; 892b20bec8 2015-04-28 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 892b20bec8 2015-04-28 kinaba: int k = 3; 892b20bec8 2015-04-28 kinaba: string _ = "Possible"; 892b20bec8 2015-04-28 kinaba: END 892b20bec8 2015-04-28 kinaba: CASE(1) 892b20bec8 2015-04-28 kinaba: string board_[] = {"BW", 892b20bec8 2015-04-28 kinaba: "WB"} 892b20bec8 2015-04-28 kinaba: ; 892b20bec8 2015-04-28 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 892b20bec8 2015-04-28 kinaba: int k = 2; 892b20bec8 2015-04-28 kinaba: string _ = "Impossible"; 892b20bec8 2015-04-28 kinaba: END 892b20bec8 2015-04-28 kinaba: CASE(2) 892b20bec8 2015-04-28 kinaba: string board_[] = {"BWBWBB", 892b20bec8 2015-04-28 kinaba: "WBWBBB", 892b20bec8 2015-04-28 kinaba: "BWBWBB", 892b20bec8 2015-04-28 kinaba: "WBWBBB", 892b20bec8 2015-04-28 kinaba: "BBBBBB", 892b20bec8 2015-04-28 kinaba: "BBBBBB"} 892b20bec8 2015-04-28 kinaba: ; 892b20bec8 2015-04-28 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 892b20bec8 2015-04-28 kinaba: int k = 2; 892b20bec8 2015-04-28 kinaba: string _ = "Possible"; 892b20bec8 2015-04-28 kinaba: END 892b20bec8 2015-04-28 kinaba: CASE(3) 892b20bec8 2015-04-28 kinaba: string board_[] = {"BWBWBB", 892b20bec8 2015-04-28 kinaba: "WBWBWB", 892b20bec8 2015-04-28 kinaba: "BWBWBB", 892b20bec8 2015-04-28 kinaba: "WBWBWB", 892b20bec8 2015-04-28 kinaba: "BWBWBB", 892b20bec8 2015-04-28 kinaba: "BBBBBB"} 892b20bec8 2015-04-28 kinaba: ; 892b20bec8 2015-04-28 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 892b20bec8 2015-04-28 kinaba: int k = 2; 892b20bec8 2015-04-28 kinaba: string _ = "Impossible"; 892b20bec8 2015-04-28 kinaba: END 892b20bec8 2015-04-28 kinaba: CASE(4) 892b20bec8 2015-04-28 kinaba: string board_[] = {"BWBWBB", 892b20bec8 2015-04-28 kinaba: "WBWBWB", 892b20bec8 2015-04-28 kinaba: "BWBWBB", 892b20bec8 2015-04-28 kinaba: "WBWBWB", 892b20bec8 2015-04-28 kinaba: "BWBWBB", 892b20bec8 2015-04-28 kinaba: "BBBBBB"} 892b20bec8 2015-04-28 kinaba: ; 892b20bec8 2015-04-28 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 892b20bec8 2015-04-28 kinaba: int k = 1; 892b20bec8 2015-04-28 kinaba: string _ = "Possible"; 892b20bec8 2015-04-28 kinaba: END 892b20bec8 2015-04-28 kinaba: CASE(5) 892b20bec8 2015-04-28 kinaba: string board_[] = {"BB", 892b20bec8 2015-04-28 kinaba: "BB"}; 892b20bec8 2015-04-28 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 892b20bec8 2015-04-28 kinaba: int k = 2; 892b20bec8 2015-04-28 kinaba: string _ = "Possible"; 892b20bec8 2015-04-28 kinaba: END 892b20bec8 2015-04-28 kinaba: CASE(6) 892b20bec8 2015-04-28 kinaba: string board_[] = {"BBWW", "BBWW", "WBBW", "WWWW"}; 892b20bec8 2015-04-28 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 892b20bec8 2015-04-28 kinaba: int k = 2; 892b20bec8 2015-04-28 kinaba: string _ = "Possible"; 892b20bec8 2015-04-28 kinaba: END 892b20bec8 2015-04-28 kinaba: /* 892b20bec8 2015-04-28 kinaba: CASE(7) 892b20bec8 2015-04-28 kinaba: string board_[] = ; 892b20bec8 2015-04-28 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 892b20bec8 2015-04-28 kinaba: int k = ; 892b20bec8 2015-04-28 kinaba: string _ = ; 892b20bec8 2015-04-28 kinaba: END 892b20bec8 2015-04-28 kinaba: */ 892b20bec8 2015-04-28 kinaba: } 892b20bec8 2015-04-28 kinaba: // END CUT HERE