大佬们,救救孩子吧,快疯了
查看原帖
大佬们,救救孩子吧,快疯了
1308563
封禁用户楼主2024/3/24 10:11
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 505;
int n,m,cnt;
int a[N][N];
int dx[] = {1 , 0 , -1 , 0};
int dy[] = {0 , 1 , 0 , -1};
void dfs(int x,int y) {
	a[x][y] = 1;
	for(int i = 0; i <= 3; ++i) {
		int nx = x + dx[i];
		int ny = y + dy[i];
		if(nx <= n && nx >= 0 && ny >= 0 && ny <= m && a[nx][ny] != 1) {
			dfs(nx,ny);
		}
	}
	return;
}
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(nullptr);
	cin >> n >> m;
	for(int i = 1; i <= n; ++i) {
		for(int j = 1; j <= m; ++j) {
			char k;
			cin >> k;
			if(k == '*') {
				a[i][j] = 1;
			} else {
				a[i][j] = 0;
			}
		}
	}
	n++;m++;
	dfs(0,0);
	n--;m--;
	for(int i = 1; i <= n; ++i) {
		for(int j = 1; j <= m; ++j) {
			if(!a[i][j]) {
				cnt++;
				dfs(i,j);
			}
		}
	}
	cout << cnt;
	return 0;
}

2024/3/24 10:11
加载中...