백준 1926 그림

백준 1926

#include<bits/stdc++.h>
using namespace std;
int n, m;
int arr[501][501];
int vis[501][501];
int mx, cnt;
int dx[4] = { -1,0,1,0 };
int dy[4] = { 0,-1,0,1 };
int bfs(int i,int j,int area) {
	queue<pair<int, int>> q;
	q.push({ i,j });
	vis[i][j] = 1;
	area = 1;
	while (!q.empty()) {
		pair<int, int> nxt = q.front();
		q.pop();
		for (int i = 0; i < 4; i++) {
			int nx = nxt.first + dx[i];
			int ny = nxt.second + dy[i];
			if (!vis[nx][ny] && arr[nx][ny]==1) {
				area++;
				q.push({ nx,ny });
				vis[nx][ny] = 1;
			}
		}
	}
	return cnt;
}
int main() {

	ios::sync_with_stdio(0); cin.tie(0);
	cin >> n >> m;

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> arr[i][j];
		}
	}

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (arr[i][j]==1 && !vis[i][j]) {
				cnt++;
				mx = max(mx, bfs(i, j,0));
			}
		}
	}
	cout << cnt << '\n'<< mx << '\n';
}

좋은 웹페이지 즐겨찾기