1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| #include<bits/stdc++.h> #define int long long using namespace std; #define endl '\n' const int N=1e5+10; typedef pair<int,int>pp; int ans=8; int dot[2][13][2]={{{2,2},{2,4},{2,6},{3,3},{3,5},{4,2},{4,4},{4,6},{5,3},{5,5},{6,2},{6,4},{6,6}},{{2,3},{2,5},{3,2},{3,4},{3,6},{4,3},{4,5},{5,2},{5,4},{5,6},{6,3},{6,5}}}; bool check(vector<vector<char> >&temp,int flag){ for(int k=0;k<13;++k){ int i=dot[flag][k][0],j=dot[flag][k][1]; if(temp[i][j]=='B'&&temp[i-1][j-1]=='B'&&temp[i-1][j+1]=='B'&&temp[i+1][j-1]=='B'&&temp[i+1][j+1]=='B') return false; } return true; } void dfs(vector<int>&num,vector<vector<char> >&temp,int flag,int step){ if(check(temp,flag)){ int cnt=0; for(int i=0;i<13;++i){ if(num[i])++cnt; } ans=min(ans,cnt); return; } if(step==13)return; char ch=temp[dot[flag][step][0]][dot[flag][step][1]]; if(ch=='W'){ dfs(num,temp,flag,step+1); }else { dfs(num,temp,flag,step+1); temp[dot[flag][step][0]][dot[flag][step][1]]='W'; num[step]=1; dfs(num,temp,flag,step+1); num[step]=0; temp[dot[flag][step][0]][dot[flag][step][1]]=ch; } } void solve() { ans=8; vector<vector<char> > vec(8,vector<char>(8)); for(int i=1;i<=7;++i){ string str; cin>>str; for(int j=1;j<=7;++j){ vec[i][j]=str[j-1]; } } vector<int>num(13,0); dfs(num,vec,0,0); int reans=ans; ans=8; for(int i=0;i<13;++i)num[i]=0; dfs(num,vec,1,0); reans+=ans; cout<<reans<<endl; } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int T=1; cin>>T; while(T--) solve(); return 0; }
|