↓実装練習 (passed system test in practice room)
(...) #define VS vector<string> #define VI vector<ll> #define VVI vector<vector<ll> > #define VVVI vector<vector<vector<ll> > > #define REP(i,n) for(int i=0,_n=(n);(i)<(int)_n;++i) #define RANGE(i,a,b) for(int i=(int)a,_b=(int)(b);(i)<_b;++i) #define MOD 1000000007LL int W, H; VS rot(const VS& g) { VS o(W, string(H, '?')); REP(y, H) REP(x, W) o[W-1-x][y] = g[y][x]; return o; } VVVI dp; ll f(int y, int splitx, int co, const VS& g) { if(y==H) return co; if(dp[y][splitx][co]!=-1) return dp[y][splitx][co]; ll ans = 0; REP(ns, splitx+1) { int ok=1; RANGE(x, 0, ns) if(g[y][x]=='W') ok=0; RANGE(x, ns, W) if(g[y][x]=='B') ok=0; if(!ok) continue; ans += f(y+1, ns, co || (0<y && ns < splitx), g); ans %= MOD; } return dp[y][splitx][co]=ans; } ll all_(char notc, VS& g) { ll ok=1; REP(y, H) REP(x, W) if(g[y][x]==notc) ok=0; return ok; } class TwoConvexShapes { public: int countWays(vector <string> g) { W=g[0].size(); H=g.size(); ll ans = all_('B', g)+all_('W', g); REP(loop, 4) { W=g[0].size(); H=g.size(); dp = VVVI(H, VVI(W+1, VI(2, -1))); ll r = f(0, W, 0, g); cout<<r<<endl; ans += r; ans %= MOD; g = rot(g); } return ans; } };