求卡常:
#include <bits/stdc++.h>
using namespace std;
int n, m;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
int pts;
long long ans = 0;
bool vis[105][105];
char mp[105][105];
pair<int, int> LftTop, RghBtm;
void bfs(int x, int y)
{
queue<pair<int, int>> q;
pts = 0;
LftTop = {INT_MAX, INT_MAX}, RghBtm = {INT_MIN, INT_MIN};
vis[x][y] = 1;
q.push({x, y});
while (!q.empty())
{
auto tp = q.front();
q.pop();
pts++;
LftTop = min(LftTop, tp);
RghBtm = max(RghBtm, tp);
for (int i = 0; i < 4; i++)
{
auto tx = tp.first + dx[i], ty = tp.second + dy[i];
if (tx < 0 || ty < 0 || tx >= n || ty >= m)
continue;
if (vis[tx][ty] || mp[tx][ty] != ' ')
continue;
vis[tx][ty] |= 1;
q.push({tx, ty});
}
}
}
int main()
{
cin.tie(&cout)->sync_with_stdio(0);
string tmp, sn, sm;
getline(cin, tmp);
istringstream iss(tmp + ' ');
getline(iss, sn, ' ');
getline(iss, sm, ' ');
n = stoi(sn), m = stoi(sm);
for (int i = 0; i < n; i++)
{
getline(cin, tmp);
for (int j = 0; j < m; j++)
{
mp[i][j] = tmp[j];
}
}
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (!vis[i][j] && mp[i][j] == ' ')
{
bfs(i, j);
if ((RghBtm.first - LftTop.first + 1) < 3 || (RghBtm.first - LftTop.first + 1) > 23 || (RghBtm.second - LftTop.second + 1) < 2 || (RghBtm.second - LftTop.second + 1) > 23)
continue;
if (pts == (RghBtm.first - LftTop.first + 1) * (RghBtm.second - LftTop.second + 1))
ans++;
}
cout << ans;
return 0;
}