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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
#include <bits/stdc++.h> using namespace std; #define ll long long #define FO(x) {freopen(#x".in","r",stdin);freopen(#x".out","w",stdout);} #define pii pair<int,int> #define pll pair<ll,ll> #define mp make_pair
vector <vector<int> > ma; vector <vector<int> > vis; const int dx[4] = {0,0,-1,1}; const int dy[4] = {1,-1,0,0}; int n,m,col; vector <pii> ps; unordered_map <int,int> sta,viss,rt;
bool valid(int x,int y) { return x >= 0 && x < n && y >= 0 && y < m; }
void dfs1(int x,int y) { ps.push_back(mp(x,y)); vis[x][y] = col; for (int i = 0;i < 4;++i) { int nx = x + dx[i],ny = y + dy[i]; if (valid(nx,ny) && !ma[nx][ny]) { if (!vis[nx][ny]) { dfs1(nx,ny); } } } }
int zip(pii x,pii y) { int a,b,c,d;a = x.first,b = x.second;c = y.first,d = y.second; return d + c * m + b * n * m + a * m * n * m; }
pair<pii,pii> unzip(int x) { pii a,b; b.second = x % m; x /= m; b.first = x % n; x /= n; a.second = x % m; x /= m; a.first = x; return mp(a,b); }
void bfs(int stt) { queue <int> q; q.push(stt); rt[stt] = stt; while (!q.empty()) { int nowsta = q.front();q.pop(); pii s = unzip(nowsta).first,t = unzip(nowsta).second; int xx = s.first,yy = s.second; int xxx = t.first,yyy = t.second; bool flag = 0; for (int i = 0;i < 4;++i){ int nx = xx + dx[i],ny = yy + dy[i]; int nxx = xxx + dx[i],nyy = yyy + dy[i]; if (!valid(nx,ny) || ma[nx][ny]) continue; if (!valid(nxx,nyy) || ma[nxx][nyy]) { sta[stt] = sta[nowsta] = 1; continue ; } if (!rt[zip(mp(nx,ny),mp(nxx,nyy))])q.push(zip(mp(nx,ny),mp(nxx,nyy))),rt[zip(mp(nx,ny),mp(nxx,nyy))] = stt;
} } }
void solve() { cin >> n >> m; ma.clear();vis.clear();col = 0; sta.clear(); ma.resize(n); vis.resize(n); rt.clear(); int siz= 0; for (int i = 0;i < n;++i) { ma[i].resize(m); vis[i].resize(m); for (int j = 0;j < m;++j) { char c;cin >> c; if (c == 79) ma[i][j] = 1; else ma[i][j] = 0,++siz; } } int ans = 0;
for (int i = 0;i < n;++i) { for (int j = 0;j < m;++j) { if (!ma[i][j] && !vis[i][j]) { ps.clear(); ++col; dfs1(i,j); for (auto p1 : ps) { int cnt = 0; for (int xx = 0;xx < n;++xx) { for (int yy = 0;yy < m;++yy) { pii p2 = mp(xx,yy); if (p2 == p1 || ma[xx][yy]) continue; if (!rt[zip(p1,p2)]) bfs(zip(p1,p2));
if (sta[rt[zip(p1,p2)]]== 1) { ++cnt; } } } if (cnt == siz - 1) { ++ans; } } } } }
cout << ans << "\n"; }
signed main() { int T;cin >> T; while (T--) solve(); return 0; }
|