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
| using namespace std; #define MAX 1005 int n, m, ans; char s[MAX + 10]; vector<string> MAP; int vis[MAX * MAX + 10]; bool flag[MAX * MAX + 10];
int gao(int i, int j, int r, int c) { return i * m * n * m + j * n * m + r * m + c; }
void ungao(int msk, int &i, int &j, int &r, int &c) { i = msk / (m * n * m); j = msk / (n * m) % m; r = msk / m % n; c = msk % m; }
bool fall(int i, int j) { return i < 0 || j < 0 || i >= n || j >= m || MAP[i][j] == 'O'; }
short dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; void bfs(int S) { queue<int> q; q.push(S); vis[S] = S; flag[S] = false; while (!q.empty()) { int i, j, r, c; ungao(q.front(), i, j, r, c); q.pop(); for (int k = 0; k < 4; k++) { int ii = i + dir[k][0], jj = j + dir[k][1]; int rr = r + dir[k][0], cc = c + dir[k][1]; if (fall(ii, jj)) { continue; } if (fall(rr, cc)) { flag[S] = true; continue; } int nxt = gao(ii, jj, rr, cc); if (vis[nxt] >= 0) { continue; } q.push(nxt); vis[nxt] = S; } } }
int check(int i, int j) { for (int r = 0; r < n; r++) { for (int c = 0; c < m; c++) { if (MAP[r][c] == '.') { if (i == r && j == c) { continue; } int msk = gao(i, j, r, c); if (vis[msk] == -1) { bfs(msk); } if (!flag[vis[msk]]) { return 0; } } } } return 1; }
void solve() { cin >> n >> m; MAP.clear(); for (int i = 0; i < n; i++) { scanf("%s", s); MAP.push_back(string(s)); } memset(vis, -1, sizeof(int) * (n * m * n * m + 3)); ans = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (MAP[i][j] == '.') { ans += check(i, j); } } } cout << ans << endl;
}
signed main() { int t; cin >> t; while(t--) { solve(); } return 0; }
|