题解 Atcoder-past202010-g

题目传送门

这道题吗,感觉橙题差不多。

话不多说,回归正题。

这道题数据范围挺小的,所以就想到了 dfs。

我们可以用一个 a n s ans ans 变量存储,然后 dfs。如果能到达, a n s + 1 → a n s ans+1\to ans ans+1ans

然后就没有什么可解释的了。

代码:

// 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;
}

AC 记录证明

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值