문제 링크 : https://www.acmicpc.net/problem/2468



1. 입력을 받으며 높이의 최대값을 max_height에 저장하였다.


2. 0부터 max_height까지 반복하며

map배열을 훑으며 max_height보다 높이가 높고 방문하지 않은 지점이 있다면 그 좌표값을 넣어 dfs를 실행하였다.

dfs를 실행할때마다 count의 값을 올려주어 영역의 갯수를 구하였다.

반복이 한번 끝날때마다 영역의 갯수중 큰값을 max_count에 갱신해주었다.


3. max_count를 출력하였다.



아래는 소스코드이다.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <algorithm>
#include <string.h>
#define MAX 100
 
using namespace std;
 
int n;
int map[MAX][MAX];
bool visit[MAX][MAX];
int dy[] = { 00-11 };
int dx[] = { -1100 };
 
void dfs(int y, int x, int rain) {
    visit[y][x] = true;
 
    for (int i = 0; i < 4; i++) {
        int ny = y + dy[i];
        int nx = x + dx[i];
 
        if (ny < 0 || nx < 0 || ny >= n || nx >= n) continue;
        if (map[ny][nx] <= rain || visit[ny][nx]) continue;
 
        dfs(ny, nx, rain);
    }
}
 
int main()
{
    int max_height=0, count, max_count=0;
 
    cin >> n;
 
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int t;
            cin >> t;
            map[i][j] = t;
            max_height = max(max_height, t);
        }
    }
 
    for (int k = 0; k < max_height; k++) {
        count = 0;
        memset(visit, falsesizeof(visit));
 
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (map[i][j] > k && visit[i][j] == false) {
                    dfs(i, j, k);
                    count++;
                }
            }
        }
        max_count = max(max_count, count);
    }
 
    cout << max_count;
}
cs



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

백준 9663번 N-Queen  (0) 2019.01.28
백준 1325번 효율적인 해킹  (0) 2019.01.23
백준 1012번 유기농 배추  (0) 2019.01.21
백준 1966번 프린터 큐  (0) 2019.01.21
백준 15686번 치킨 배달  (0) 2019.01.17

+ Recent posts