Artifact Content
Not logged in

Artifact 27ee55f46dcdfc29a2d385c91d48d942a01e2df4


#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>
#include <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

int bitcnt(int x)
{
	int c = 0;
	for(; x; x>>=1)
		c += x&1;
	return c;
}

class WolvesAndSheep { public:
	int getmin(vector <string> field)
	{
		int H = field.size();
		int W = field[0].size();
		int best = W*H;
		for(int mask=0; mask<(1<<(W-1)); ++mask)
		{
			vector<vector<pair<int,int>>> sw(H);
			for(int y=0; y<field.size(); ++y)
				for(int s=0,w=0,x=0; x<W; ++x)
				{
					if(field[y][x]=='S') s++;
					if(field[y][x]=='W') w++;
					if(s&&w)
						goto next;
					if((mask & (1<<x)) || x==W-1) {
						sw[y].emplace_back(s,w);
						s=w=0;
					}
				}
			{
				int score = bitcnt(mask);
				int WW = sw[0].size();
				vector<pair<int,int>> cur(WW);
				for(int y=0; y<sw.size(); ++y)
				{
					bool dame = false;
					for(int x=0; x<WW; ++x) {
						cur[x].first += sw[y][x].first;
						cur[x].second += sw[y][x].second;
						if(cur[x].first && cur[x].second)
							dame = true;
					}
					if(dame) {
						score ++;
						cur = sw[y];
					}
				}

				best = min(best, score);
			}
		next:;
		}
		return best;
	}
};

// 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(_, WolvesAndSheep().getmin(field));}
int main(){

CASE(0)
	string field_[] = {"W.WSS",
 "WW.S.",
 ".SSS.",
 "SSS.S",
 ".SS.S"};
	  vector <string> field(field_, field_+sizeof(field_)/sizeof(*field_)); 
	int _ = 2; 
END
CASE(1)
	string field_[] = {".....",
 ".....",
 "....."};
	  vector <string> field(field_, field_+sizeof(field_)/sizeof(*field_)); 
	int _ = 0; 
END
CASE(2)
	string field_[] = {".SS",
 "...",
 "S..",
 "W.W"};
	  vector <string> field(field_, field_+sizeof(field_)/sizeof(*field_)); 
	int _ = 1; 
END
CASE(3)
	string field_[] = {"WWWSWWSSWWW",
 "WWSWW.SSWWW",
 "WS.WSWWWWS."};
	  vector <string> field(field_, field_+sizeof(field_)/sizeof(*field_)); 
	int _ = 10; 
END
CASE(4)
	string field_[] = {".W.S.W.W",
 "W.W.S.W.",
 ".S.S.W.W",
 "S.S.S.W.",
 ".S.W.W.S",
 "S.S.W.W.",
 ".W.W.W.S",
 "W.W.S.S."};
	  vector <string> field(field_, field_+sizeof(field_)/sizeof(*field_)); 
	int _ = 7; 
END
CASE(5)
	string field_[] = {"W.SSWWSSSW.SS",
 ".SSSSW.SSWWWW",
 ".WWWWS.WSSWWS",
 "SS.WSS..W.WWS",
 "WSSS.SSWS.W.S",
 "WSS.WS...WWWS",
 "S.WW.S.SWWWSW",
 "WSSSS.SSW...S",
 "S.WWSW.WWSWSW",
 ".WSSS.WWSWWWS",
 "..SSSS.WWWSSS",
 "SSWSWWS.W.SSW",
 "S.WSWS..WSSS.",
 "WS....W..WSS."};
	  vector <string> field(field_, field_+sizeof(field_)/sizeof(*field_)); 
	int _ = 24; 
END
CASE(6)
	string field_[] = {"WW..SS",
 "WW..SS",
 "......",
 "......",
 "SS..WW",
 "SS..WW"};
	  vector <string> field(field_, field_+sizeof(field_)/sizeof(*field_)); 
	int _ = 2; 
END
/*
CASE(7)
	string field_[] = ;
	  vector <string> field(field_, field_+sizeof(field_)/sizeof(*field_)); 
	int _ = ; 
END
CASE(8)
	string field_[] = ;
	  vector <string> field(field_, field_+sizeof(field_)/sizeof(*field_)); 
	int _ = ; 
END
*/
}
// END CUT HERE