비오는량에 따라 안전영역의 갯수가 달라지는데 그중 가장 큰 갯수를 구하면 되는 문제이다


먼저 배열을 입력받으면서 높이값중에 최대값을 max_height 변수에 저장하였다

0부터 max_height 까지 반복 {

visited 배열 값 전부다 false로 바꿔줌

2차원배열을 탐색하며 방문하지않고 안전영역이 아닌 지점이라면 {

dfs수행

count변수 증가

}

count값 중 최대값 max_count 변수에 유지

}

max_count 출력



구조는 이렇게 잡았다. 이렇게 설명할바엔 걍 소스코드만 복붙하는게 나을것같기도하지만 그래도 소스코드만 복붙은 성의 없어보일것같아서 설명하였다.


유의할점은 dfs를 호출할때 x,y 좌표값 뿐만 아니라 비가 온 양(제일 바깥반복문의 증가변수)를 넘겨주어

비의 높이보다 낮은 지점은 탐색을 하지 않도록 하여야 한다.


아래는 소스코드이다.



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
60
import java.util.Arrays;
import java.util.Scanner;
 
public class Main {
    
    static int n;
    static int[] dx = { 001-1 };
    static int[] dy = {1-100 };
    static boolean[][] visited;
    static int[][] map;
    
    public static void main(String[] args) {
 
        int max_height=0, count, max_count=0;
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        map = new int[n][n];
        
        for(int i=0; i<n; i++) {
            for(int j=0; j<n; j++) {
                int height = sc.nextInt();
                map[i][j] = height;
                max_height = Math.max(max_height, height);
            }
        }
        
        for(int i=0; i<max_height; i++) {
            visited = new boolean[n][n];
            count = 0;
            
            for(int j=0; j<n; j++) {
                for(int k=0; k<n; k++) {
                    if(visited[j][k]==false && map[j][k]>i) {
                        dfs(j, k, i);
                        count++;
                    }
                }
            }
            
            max_count = Math.max(count, max_count);
        }
        
        System.out.println(max_count);
    }
    
    static public void dfs(int y, int x, int flooding_height) {
        visited[y][x] = true;
        
        for(int i=0; i<4; i++) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            
            if(nx<0 || ny<0 || nx >= n || ny >=n) continue;
            if(visited[ny][nx]==true || map[ny][nx]<=flooding_height) continue;
            
            dfs(ny, nx, flooding_height);
        }
    }
    
}
cs





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

백준 6603번 로또  (0) 2019.01.14
백준 1987번 알파벳  (0) 2019.01.05
백준 11403번 경로 찾기  (0) 2019.01.04
백준 11724번 연결 요소의 개수  (0) 2019.01.02
백준 2583번 영역구하기  (0) 2018.12.31

+ Recent posts