Lined Notebook

[Gold 4] [Java] 치즈 (2638 번)

by HeshAlgo

Java Code

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package com.baekjoon.java;
 
import java.awt.Point;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
 
public class Main {
    static int N, M, answer;
    static int[][] map;
    static int[] dx = {-1010};
    static int[] dy = {010-1};
    
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] inputSize = br.readLine().split(" ");
        N = Integer.parseInt(inputSize[0]);
        M = Integer.parseInt(inputSize[1]);
        map = new int[N][M];
        
        for (int row = 0; row < N; row++) {
            String[] input = br.readLine().split(" ");
            for (int col = 0; col < M; col++) {
                map[row][col] = Integer.parseInt(input[col]);
            }
        }
        
        while (check()) {
            // 실내 공기 영역 분리
            air();
            
            // 공기와 접촉하는 곳 찾기
            contact();
            
            // 공기와 접촉한 곳 제거
            delete();
        }
        
        System.out.println(answer);
        
    }
 
    private static boolean check() {
        for (int row = 0; row < N; row++) {
            for (int col = 0; col < M; col++) {
                if (map[row][col] == 1) {
                    return true;
                }
            }
        }
        
        return false;
    }
 
    private static void delete() {
        for (int row = 0; row < N; row++) {
            for (int col = 0; col < M; col++) {
                // 공기와 2번이상 접촉했었던 곳 제거
                if (map[row][col] == 2) {
                    map[row][col] = 0;
                } 
                // 실내공기 영역 다시 초기화
                else if (map[row][col] == -1) {
                    map[row][col] = 0;
                }
            }
        }
        
        answer++;
    }
 
    private static void contact() {
        for (int row = 0; row < N; row++) {
            for (int col = 0; col < M; col++) {
                // 치즈 영역일 경우
                if (map[row][col] == 1) {
                    int cnt = 0;
                    
                    for (int i = 0; i < 4; i++) {
                        int nx = row + dx[i];
                        int ny = col + dy[i];
                        
                        if (0 <= nx && nx < N && 0 <= ny && ny < M) {
                            // 공기와 맞닿는 경우
                            if (map[nx][ny] == -1) {
                                cnt++;
                            }
                        }
                    }
                    // 공기와 맞닿는 곳이 2곳 이상일 경우
                    if (cnt >= 2) {
                        map[row][col] = 2;
                    }
                }
                
            }
        }
    }
 
    private static void air() {
        Queue<Point> q = new LinkedList<Point>();
        q.add(new Point(00));
        map[0][0= -1;
        
        while (!q.isEmpty()) {
            Point point = q.poll();
            int x = point.x;
            int y = point.y;
            
            for (int i = 0; i < 4; i++) {
                int nx = x + dx[i];
                int ny = y + dy[i];
                
                if (0 <= nx && nx < N && 0 <= ny && ny < M) {
                    if (map[nx][ny] == 0) {
                        map[nx][ny] = -1;
                        q.add(new Point(nx, ny));
                    }
                }
            }
            
        }
        
    }
 
}
cs

블로그의 정보

꾸준히 공부하는 개발 노트

HeshAlgo

활동하기