d4ec396a23 2014-04-21 kinaba: #include <iostream> d4ec396a23 2014-04-21 kinaba: #include <sstream> d4ec396a23 2014-04-21 kinaba: #include <iomanip> d4ec396a23 2014-04-21 kinaba: #include <vector> d4ec396a23 2014-04-21 kinaba: #include <string> d4ec396a23 2014-04-21 kinaba: #include <map> d4ec396a23 2014-04-21 kinaba: #include <set> d4ec396a23 2014-04-21 kinaba: #include <algorithm> d4ec396a23 2014-04-21 kinaba: #include <numeric> d4ec396a23 2014-04-21 kinaba: #include <iterator> d4ec396a23 2014-04-21 kinaba: #include <functional> d4ec396a23 2014-04-21 kinaba: #include <complex> d4ec396a23 2014-04-21 kinaba: #include <queue> d4ec396a23 2014-04-21 kinaba: #include <stack> d4ec396a23 2014-04-21 kinaba: #include <cmath> d4ec396a23 2014-04-21 kinaba: #include <cassert> d4ec396a23 2014-04-21 kinaba: #include <tuple> d4ec396a23 2014-04-21 kinaba: using namespace std; d4ec396a23 2014-04-21 kinaba: typedef long long LL; d4ec396a23 2014-04-21 kinaba: typedef complex<double> CMP; d4ec396a23 2014-04-21 kinaba: d4ec396a23 2014-04-21 kinaba: int bitcnt(int x) d4ec396a23 2014-04-21 kinaba: { d4ec396a23 2014-04-21 kinaba: int c = 0; d4ec396a23 2014-04-21 kinaba: for(; x; x>>=1) d4ec396a23 2014-04-21 kinaba: c += x&1; d4ec396a23 2014-04-21 kinaba: return c; d4ec396a23 2014-04-21 kinaba: } d4ec396a23 2014-04-21 kinaba: d4ec396a23 2014-04-21 kinaba: class WolvesAndSheep { public: d4ec396a23 2014-04-21 kinaba: int getmin(vector <string> field) d4ec396a23 2014-04-21 kinaba: { d4ec396a23 2014-04-21 kinaba: int H = field.size(); d4ec396a23 2014-04-21 kinaba: int W = field[0].size(); d4ec396a23 2014-04-21 kinaba: int best = W*H; d4ec396a23 2014-04-21 kinaba: for(int mask=0; mask<(1<<(W-1)); ++mask) d4ec396a23 2014-04-21 kinaba: { d4ec396a23 2014-04-21 kinaba: vector<vector<pair<int,int>>> sw(H); d4ec396a23 2014-04-21 kinaba: for(int y=0; y<field.size(); ++y) d4ec396a23 2014-04-21 kinaba: for(int s=0,w=0,x=0; x<W; ++x) d4ec396a23 2014-04-21 kinaba: { d4ec396a23 2014-04-21 kinaba: if(field[y][x]=='S') s++; d4ec396a23 2014-04-21 kinaba: if(field[y][x]=='W') w++; d4ec396a23 2014-04-21 kinaba: if(s&&w) d4ec396a23 2014-04-21 kinaba: goto next; d4ec396a23 2014-04-21 kinaba: if((mask & (1<<x)) || x==W-1) { d4ec396a23 2014-04-21 kinaba: sw[y].emplace_back(s,w); d4ec396a23 2014-04-21 kinaba: s=w=0; d4ec396a23 2014-04-21 kinaba: } d4ec396a23 2014-04-21 kinaba: } d4ec396a23 2014-04-21 kinaba: { d4ec396a23 2014-04-21 kinaba: int score = bitcnt(mask); d4ec396a23 2014-04-21 kinaba: int WW = sw[0].size(); d4ec396a23 2014-04-21 kinaba: vector<pair<int,int>> cur(WW); d4ec396a23 2014-04-21 kinaba: for(int y=0; y<sw.size(); ++y) d4ec396a23 2014-04-21 kinaba: { d4ec396a23 2014-04-21 kinaba: bool dame = false; d4ec396a23 2014-04-21 kinaba: for(int x=0; x<WW; ++x) { d4ec396a23 2014-04-21 kinaba: cur[x].first += sw[y][x].first; d4ec396a23 2014-04-21 kinaba: cur[x].second += sw[y][x].second; d4ec396a23 2014-04-21 kinaba: if(cur[x].first && cur[x].second) d4ec396a23 2014-04-21 kinaba: dame = true; d4ec396a23 2014-04-21 kinaba: } d4ec396a23 2014-04-21 kinaba: if(dame) { d4ec396a23 2014-04-21 kinaba: score ++; d4ec396a23 2014-04-21 kinaba: cur = sw[y]; d4ec396a23 2014-04-21 kinaba: } d4ec396a23 2014-04-21 kinaba: } d4ec396a23 2014-04-21 kinaba: d4ec396a23 2014-04-21 kinaba: best = min(best, score); d4ec396a23 2014-04-21 kinaba: } d4ec396a23 2014-04-21 kinaba: next:; d4ec396a23 2014-04-21 kinaba: } d4ec396a23 2014-04-21 kinaba: return best; d4ec396a23 2014-04-21 kinaba: } d4ec396a23 2014-04-21 kinaba: }; d4ec396a23 2014-04-21 kinaba: d4ec396a23 2014-04-21 kinaba: // BEGIN CUT HERE d4ec396a23 2014-04-21 kinaba: #include <ctime> d4ec396a23 2014-04-21 kinaba: double start_time; string timer() d4ec396a23 2014-04-21 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } d4ec396a23 2014-04-21 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) d4ec396a23 2014-04-21 kinaba: { os << "{ "; d4ec396a23 2014-04-21 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) d4ec396a23 2014-04-21 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } d4ec396a23 2014-04-21 kinaba: void verify_case(const int& Expected, const int& Received) { d4ec396a23 2014-04-21 kinaba: bool ok = (Expected == Received); d4ec396a23 2014-04-21 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; d4ec396a23 2014-04-21 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } d4ec396a23 2014-04-21 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); d4ec396a23 2014-04-21 kinaba: #define END verify_case(_, WolvesAndSheep().getmin(field));} d4ec396a23 2014-04-21 kinaba: int main(){ d4ec396a23 2014-04-21 kinaba: d4ec396a23 2014-04-21 kinaba: CASE(0) d4ec396a23 2014-04-21 kinaba: string field_[] = {"W.WSS", d4ec396a23 2014-04-21 kinaba: "WW.S.", d4ec396a23 2014-04-21 kinaba: ".SSS.", d4ec396a23 2014-04-21 kinaba: "SSS.S", d4ec396a23 2014-04-21 kinaba: ".SS.S"}; d4ec396a23 2014-04-21 kinaba: vector <string> field(field_, field_+sizeof(field_)/sizeof(*field_)); d4ec396a23 2014-04-21 kinaba: int _ = 2; d4ec396a23 2014-04-21 kinaba: END d4ec396a23 2014-04-21 kinaba: CASE(1) d4ec396a23 2014-04-21 kinaba: string field_[] = {".....", d4ec396a23 2014-04-21 kinaba: ".....", d4ec396a23 2014-04-21 kinaba: "....."}; d4ec396a23 2014-04-21 kinaba: vector <string> field(field_, field_+sizeof(field_)/sizeof(*field_)); d4ec396a23 2014-04-21 kinaba: int _ = 0; d4ec396a23 2014-04-21 kinaba: END d4ec396a23 2014-04-21 kinaba: CASE(2) d4ec396a23 2014-04-21 kinaba: string field_[] = {".SS", d4ec396a23 2014-04-21 kinaba: "...", d4ec396a23 2014-04-21 kinaba: "S..", d4ec396a23 2014-04-21 kinaba: "W.W"}; d4ec396a23 2014-04-21 kinaba: vector <string> field(field_, field_+sizeof(field_)/sizeof(*field_)); d4ec396a23 2014-04-21 kinaba: int _ = 1; d4ec396a23 2014-04-21 kinaba: END d4ec396a23 2014-04-21 kinaba: CASE(3) d4ec396a23 2014-04-21 kinaba: string field_[] = {"WWWSWWSSWWW", d4ec396a23 2014-04-21 kinaba: "WWSWW.SSWWW", d4ec396a23 2014-04-21 kinaba: "WS.WSWWWWS."}; d4ec396a23 2014-04-21 kinaba: vector <string> field(field_, field_+sizeof(field_)/sizeof(*field_)); d4ec396a23 2014-04-21 kinaba: int _ = 10; d4ec396a23 2014-04-21 kinaba: END d4ec396a23 2014-04-21 kinaba: CASE(4) d4ec396a23 2014-04-21 kinaba: string field_[] = {".W.S.W.W", d4ec396a23 2014-04-21 kinaba: "W.W.S.W.", d4ec396a23 2014-04-21 kinaba: ".S.S.W.W", d4ec396a23 2014-04-21 kinaba: "S.S.S.W.", d4ec396a23 2014-04-21 kinaba: ".S.W.W.S", d4ec396a23 2014-04-21 kinaba: "S.S.W.W.", d4ec396a23 2014-04-21 kinaba: ".W.W.W.S", d4ec396a23 2014-04-21 kinaba: "W.W.S.S."}; d4ec396a23 2014-04-21 kinaba: vector <string> field(field_, field_+sizeof(field_)/sizeof(*field_)); d4ec396a23 2014-04-21 kinaba: int _ = 7; d4ec396a23 2014-04-21 kinaba: END d4ec396a23 2014-04-21 kinaba: CASE(5) d4ec396a23 2014-04-21 kinaba: string field_[] = {"W.SSWWSSSW.SS", d4ec396a23 2014-04-21 kinaba: ".SSSSW.SSWWWW", d4ec396a23 2014-04-21 kinaba: ".WWWWS.WSSWWS", d4ec396a23 2014-04-21 kinaba: "SS.WSS..W.WWS", d4ec396a23 2014-04-21 kinaba: "WSSS.SSWS.W.S", d4ec396a23 2014-04-21 kinaba: "WSS.WS...WWWS", d4ec396a23 2014-04-21 kinaba: "S.WW.S.SWWWSW", d4ec396a23 2014-04-21 kinaba: "WSSSS.SSW...S", d4ec396a23 2014-04-21 kinaba: "S.WWSW.WWSWSW", d4ec396a23 2014-04-21 kinaba: ".WSSS.WWSWWWS", d4ec396a23 2014-04-21 kinaba: "..SSSS.WWWSSS", d4ec396a23 2014-04-21 kinaba: "SSWSWWS.W.SSW", d4ec396a23 2014-04-21 kinaba: "S.WSWS..WSSS.", d4ec396a23 2014-04-21 kinaba: "WS....W..WSS."}; d4ec396a23 2014-04-21 kinaba: vector <string> field(field_, field_+sizeof(field_)/sizeof(*field_)); d4ec396a23 2014-04-21 kinaba: int _ = 24; d4ec396a23 2014-04-21 kinaba: END d4ec396a23 2014-04-21 kinaba: CASE(6) d4ec396a23 2014-04-21 kinaba: string field_[] = {"WW..SS", d4ec396a23 2014-04-21 kinaba: "WW..SS", d4ec396a23 2014-04-21 kinaba: "......", d4ec396a23 2014-04-21 kinaba: "......", d4ec396a23 2014-04-21 kinaba: "SS..WW", d4ec396a23 2014-04-21 kinaba: "SS..WW"}; d4ec396a23 2014-04-21 kinaba: vector <string> field(field_, field_+sizeof(field_)/sizeof(*field_)); d4ec396a23 2014-04-21 kinaba: int _ = 2; d4ec396a23 2014-04-21 kinaba: END d4ec396a23 2014-04-21 kinaba: /* d4ec396a23 2014-04-21 kinaba: CASE(7) d4ec396a23 2014-04-21 kinaba: string field_[] = ; d4ec396a23 2014-04-21 kinaba: vector <string> field(field_, field_+sizeof(field_)/sizeof(*field_)); d4ec396a23 2014-04-21 kinaba: int _ = ; d4ec396a23 2014-04-21 kinaba: END d4ec396a23 2014-04-21 kinaba: CASE(8) d4ec396a23 2014-04-21 kinaba: string field_[] = ; d4ec396a23 2014-04-21 kinaba: vector <string> field(field_, field_+sizeof(field_)/sizeof(*field_)); d4ec396a23 2014-04-21 kinaba: int _ = ; d4ec396a23 2014-04-21 kinaba: END d4ec396a23 2014-04-21 kinaba: */ d4ec396a23 2014-04-21 kinaba: } d4ec396a23 2014-04-21 kinaba: // END CUT HERE