|
| 1 | +#include<bits/stdc++.h> |
| 2 | +#include<bits/extc++.h> |
| 3 | +#define ms(x,v) memset((x),v,sizeof(x)) |
| 4 | +#define INF 0x3f3f3f3f |
| 5 | +using namespace std; |
| 6 | +// using namespace __gnu_pbds; |
| 7 | + |
| 8 | +const int MAXN = 100+10; |
| 9 | +typedef long long LL; |
| 10 | +typedef pair<int,bool> PIB; |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +void dfs(int u,bool flip,int &cnt,vector<bool>& col,vector<bool>&vis,const vector<vector<PIB>> & G){ |
| 15 | + if(vis[u]){ |
| 16 | + if(col[u] != flip) |
| 17 | + cnt = INF; |
| 18 | + return; |
| 19 | + } |
| 20 | + vis[u] = true; |
| 21 | + col[u] = flip; |
| 22 | + if(flip)cnt ++ ; |
| 23 | + for(const auto & e : G[u]) |
| 24 | + dfs(e.first,flip ^ e.second,cnt,col,vis,G); |
| 25 | +} |
| 26 | + |
| 27 | +void solve(){ |
| 28 | + int n; |
| 29 | + cin >> n; |
| 30 | + const int offset = 2 *n -2 + n; |
| 31 | + const int MAX_V = 4 *n - 2; |
| 32 | + vector<vector<PIB> > G(MAX_V); |
| 33 | + for(int i=0 ; i<n ; ++i){ |
| 34 | + string s; |
| 35 | + cin >> s; |
| 36 | + for(int j=0 ; j<n ; ++j) |
| 37 | + { |
| 38 | + int x = i + j; |
| 39 | + int y = i-j + offset; |
| 40 | + bool v = (s[j] == '.'); |
| 41 | + G[x].push_back(make_pair(y,v)); |
| 42 | + // cout << x << " " << y << " "<< v << "\n"; |
| 43 | + G[y].push_back(make_pair(x,v)); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + vector<bool> vis1(MAX_V,false),vis2(MAX_V,false),col(MAX_V,false); |
| 48 | + int ans =0; |
| 49 | + |
| 50 | + for(int i=0 ; i< MAX_V ; ++i){ |
| 51 | + if(vis1[i]) |
| 52 | + continue; |
| 53 | + int cnt1 = 0,cnt2 = 0; |
| 54 | + dfs(i,0,cnt1,col,vis1,G); |
| 55 | + dfs(i,1,cnt2,col,vis2,G); |
| 56 | + |
| 57 | + ans += min(cnt1,cnt2); |
| 58 | + } |
| 59 | + cout << ans << '\n'; |
| 60 | +} |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +int main(){ |
| 67 | + ios :: sync_with_stdio(0); |
| 68 | + cin.tie(0); |
| 69 | + cout.tie(0); |
| 70 | + std::cout.precision(10); |
| 71 | + std::cout.setf( std::ios::fixed, std:: ios::floatfield ); |
| 72 | + int T; |
| 73 | + cin >> T; |
| 74 | + // cout << T << '\n'; |
| 75 | + for(int tt =1 ; tt <=T ; ++tt) |
| 76 | + { |
| 77 | + cout << "Case #" << tt << ": "; |
| 78 | + solve(); |
| 79 | + } |
| 80 | + return 0; |
| 81 | +} |
0 commit comments