본문 바로가기
  • Let's study
PS/백준

[C++] 백준 2468번: 안전 영역

by 코딩고수이고파 2021. 9. 12.
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;

int arr[101][101];
bool visit[101][101];
int dx[] = { 1,-1,0,0 }, dy[] = { 0,0,1,-1 };
int n, height[101];

void dfs(int x, int y, int h) {
	visit[x][y] = true;

	for (int i = 0; i < 4; i++) {
		int nx = x + dx[i], ny = y + dy[i];
		if (nx >= 0 && nx < n && ny >= 0 && ny < n && !visit[nx][ny] && arr[nx][ny]>h) {
			dfs(nx, ny, h);
		}
	}
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);

	int MAX = 0;
	cin >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> arr[i][j];
			height[arr[i][j]] = 1;
			MAX = max(MAX, arr[i][j]);
		}
	}

	int res=1, cnt=0;
	for (int k = 1; k <= MAX; k++) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if (!visit[i][j] && arr[i][j] > k) {
					dfs(i, j, k);
					cnt++;
				}
			}
		}
		res = max(cnt, res);
		cnt = 0;
		memset(visit, false, sizeof(visit));
	}

	cout << res;
	return 0;
}

'PS > 백준' 카테고리의 다른 글

[C++] 백준 1931번: 회의실 배정  (0) 2021.11.01
[C++] 백준 2225번: 합분해  (0) 2021.09.12
[C++] 백준 20046번: Road Reconstruction  (0) 2021.09.09
[C++] 백준 6764번: Sounds fishy!  (0) 2021.03.13
[C++] 백준 10799번: 쇠막대기  (0) 2021.02.04

댓글