这道题吗,感觉橙题差不多。
话不多说,回归正题。
这道题数据范围挺小的,所以就想到了 dfs。
我们可以用一个 a n s ans ans 变量存储,然后 dfs。如果能到达, a n s + 1 → a n s ans+1\to ans ans+1→ans。
然后就没有什么可解释的了。
代码:
// LUOGU_RID: 123663665
#include <bits/stdc++.h> //
#define FastIO ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define cint const int
#define rint register int
using namespace std;
using namespace std::literals;
using namespace std::string_literals;
using namespace std::chrono;
typedef long long ll;
typedef size_t ull;
int n, m;
char mp[105][105];
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};
bool vis[105][105];
int sum = 0, ans = 0;
int cnt = 0;
bool check(int x, int y) {
return x <= n && x >= 1 && y <= m && y >= 1 && !vis[x][y];
}
void dfs(int x, int y) {
for (int i = 0; i < 4; i++) {
int nx = dx[i] + x, ny = dy[i] + y;
if (mp[nx][ny] != '#' && check(nx, ny)) {
vis[nx][ny] = true;
dfs(nx, ny);
cnt++;
}
}
}
int main() {
// freopen(".out", "w", stdout);
FastIO;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> mp[i][j];
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (mp[i][j] != '#'){
sum++;
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (mp[i][j] == '#') {
cnt = 0;
memset(vis, 0, sizeof vis);
mp[i][j] = '.', vis[i][j] = true;
dfs(i, j);
if (cnt == sum) ans++;
mp[i][j] = '#';
}
}
}
cout << ans << endl;
return 0;
}