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
| #include<bits/stdc++.h> using namespace std; #define pii pair<int, int> #define pll pair<ll, ll> #define ll long long
inline void solve() { int n, m; cin >> n >> m; vector<vector<char>> mp(n+5, vector<char>(m+5)); vector<pii> a; for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) { cin >> mp[i][j]; if(mp[i][j] == '.') a.push_back({i, j}); } auto check = [&](int x, int y) { if(x<1 || y<1 || x>n || y>m) return true; if(mp[x][y] == 'O') return true; return false; };
int dx[5] = {0, 1, -1, 0, 0}; int dy[5] = {0, 0, 0, 1, -1}; set<pii> st; auto bfs = [&](int x, int y) { queue<pii> q; vector<pii> dxy; st.insert({x, y}); q.push({x, y}); int tot = 1; while(!q.empty()) { auto u = q.front(); int ux = u.first; int uy = u.second; q.pop(); for(int i=1; i<=4; i++) { int vx = ux + dx[i]; int vy = uy + dy[i]; if(check(vx, vy)) continue; if(st.count({vx, vy})) continue; st.insert({vx, vy}); tot ++; dxy.push_back({vx - x, vy - y}); q.push({vx, vy}); } } for(auto t : a) { if(t.first != x || t.second != y) { bool ok = 0; for(pii p : dxy) { if(check(t.first+p.first, t.second+p.second)) { ok = 1; break; } } if(!ok) return 0; } } return tot; };
int res = 0; for(auto t : a) if(!st.count({t.first, t.second})) { res += bfs(t.first, t.second); } cout << res << endl; } int main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int t; cin >> t; while(t--) solve(); return 0; }
|