#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<double> CMP;
static const int INF = 0x1fffffff;
int bitcnt(int x)
{
int c = 0;
for(; x; x>>=1)
c += x&1;
return c;
}
class YetAnotherBoardGame { public:
int g_best;
int minimumMoves(vector <string> board)
{
int H = board.size();
int W = board[0].size();
vector<int> b(H);
for(int y=0; y<H; ++y)
for(int x=0; x<W; ++x)
b[y] |= (board[y][x]=='W')<<x;
b.push_back(0); // sentinel
g_best = INF;
for(int type=1; type<=2; ++type)
for(int mask=0; mask<(1<<W); ++mask)
{
int cur = b[0];
int next = b[1];
flip(cur, next, mask, type, W);
do_try(cur, next, b, 1, H, W, (type==1 ? mask : 0), (type==2 ? mask : 0), bitcnt(mask));
}
return g_best==INF ? -1 : g_best;
}
void flip(int& cur, int& next, int mask, int type, int W)
{
int pat = (type==1 ? 5 : 7);
for(int f=0; (1<<f)<=mask; ++f)
if((1<<f) & mask)
cur ^= (pat<<f>>1)&((1<<W)-1);
next ^= mask;
}
void do_try(int prev, int cur, const vector<int>& b, int y, int H, int W, int must_t1, int must_t2, int acc)
{
if(y==H) {
int score = (prev==0 ? 0 : INF) + acc;
g_best = min(g_best, score);
return;
}
int mask = prev;
int score = bitcnt(mask) + acc;
if(score >= g_best)
return;
if(!(mask&must_t2)) { // try type 1
int curr = cur;
int next = b[y+1];
flip(curr, next, mask, 1, W);
do_try(curr, next, b, y+1, H, W, must_t1|mask, must_t2, score);
}
if(!(mask&must_t1)) { // try type 2
int curr = cur;
int next = b[y+1];
flip(curr, next, mask, 2, W);
do_try(curr, next, b, y+1, H, W, must_t1, must_t2|mask, score);
}
}
};
// 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(_, YetAnotherBoardGame().minimumMoves(board));}
int main(){
CASE(0)
string board_[] = {"BBBBBBBBB",
"BBWBBBBBB",
"BWWWBBBBB",
"BBWBBBWBB",
"BBBBBWBWB",
"BBBBBBWBB"};
vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_));
int _ = 2;
END
CASE(1)
string board_[] = {"BBW",
"WWW",
"BWW"};
vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_));
int _ = 2;
END
CASE(2)
string board_[] = {"WBW",
"BBW",
"WBW"};
vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_));
int _ = 4;
END
CASE(3)
string board_[] = {"BBBB",
"WBWB",
"BBBB",
"BBBB"};
vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_));
int _ = -1;
END
CASE(4)
string board_[] = {"WWWWWBW",
"WBWBWBW",
"BBWBBWW"};
vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_));
int _ = 7;
END
CASE(5)
string board_[] = {"WWWWWWWWWW",
"WWWWWWWWWW",
"WWWWWWWWWW",
"WWWWWWWWWW",
"WWWWWWWWWW",
"WWWWWWWWWW",
"WWWWWWWWWW",
"WWWWWWWWWW",
"WWWWWWWWWW",
"WWWWWWWWWW"}
;
vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_));
int _ = 30;
END
/*
CASE(6)
string board_[] = ;
vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_));
int _ = ;
END
CASE(7)
string board_[] = ;
vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_));
int _ = ;
END
*/
}
// END CUT HERE