[백준 14502] 연구소 (Java)
Algorithm/백준(BOJ)

[백준 14502] 연구소 (Java)

반응형






[백준 14502] 연구소 (Java)


출처 : 링크




삼성 코테 기출 문제다.


0은 이동가능한 값, 1은 벽, 2는 바이러스다.


벽으로 막혀있지 않고, 바이러스와 인접해있는 0인 곳들은 모두 감염이 된다.


처음 시작 전에, 값이 0인 map에서 3곳에 벽을 세울 수 있다.


따라서 map에 저장된 0의 수를 저장하고, 조합을 통해 벽을 모두 세운 뒤 dfs를 진행했다.


모두 진행한 뒤 가장 많은 안전 영역 수를 출력해주면 되는 문제다.




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
128
129
130
131
132
133
134
135
136
137
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
 
public class Main {
 
    static class Virus{
        int y;
        int x;
        
        Virus(int y, int x){
            this.y = y;
            this.x = x;
        }
    }
    
    static int N, M;
    static int[][] map;
    static int[][] copymap;
    static ArrayList<Virus> virus;
    static ArrayList<Virus> wall;
    
    static int[] dy = {0,0,-1,1};
    static int[] dx = {-1,1,0,0};
    static int[] set;
    static int max;
 
    public static void main(String[] args) throws Exception {
        
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        StringTokenizer st = new StringTokenizer(br.readLine(), " ");
        N = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());
        
        map = new int[N][M];
        virus = new ArrayList<>();
        wall = new ArrayList<>();
        
        for (int i = 0; i < N; i++) {
            st = new StringTokenizer(br.readLine(), " ");
            for (int j = 0; j < M; j++) {
                map[i][j] = Integer.parseInt(st.nextToken());
                if(map[i][j] == 2) {
                    virus.add(new Virus(i, j)); // 바이러스 저장
                }
                if(map[i][j] == 0) {
                    wall.add(new Virus(i,j));
                }
            }
        }
        
        set = new int[3];
        
        int n = wall.size();
        int k = set.length;
        
        max = Integer.MIN_VALUE;
        comb(set, 0, n, k, 0);
        
        System.out.println(max);
    }
    
    public static void dfs(Virus v) {
        int y = v.y;
        int x = v.x;
        
        for (int i = 0; i < 4; i++) {
            int ny = y + dy[i];
            int nx = x + dx[i];
            
            if(ny>=0 && nx>=0 && ny<&& nx<M) {
                if(copymap[ny][nx] == 0) {
                    copymap[ny][nx] = 2;
                    dfs(new Virus(ny,nx));
                }
            }
        }
    }
    
    public static void comb(int[] set, int size, int n, int k, int index) {
        
        if(k==0) {
            copymap = new int[N][M];
            copy();
            for (int i = 0; i < size; i++) {
                Virus v = wall.get(set[i]);
                copymap[v.y][v.x] = 1;
            }
            for (int i = 0; i < virus.size(); i++) {
                dfs(virus.get(i));
            }
            
            
            int cnt = 0;
            for (int i = 0; i < N; i++) {
                for (int j = 0; j < M; j++) {
                    if(copymap[i][j] == 0) cnt++;
                }
            }
            if(max < cnt) max = cnt;
            
            return;
        }
        else if(n==index) return;
        
        set[size] = index;
        comb(set, size+1, n, k-1, index+1);
        comb(set, size, n, k, index+1);
        
    }
    
    public static void checking(int y, int x) {
        
        for (int i = 0; i < 4; i++) {
            int ny = y + dy[i];
            int nx = x + dx[i];
            
            if(ny>=0 && nx>=0 && ny<&& nx<M) {
                
                if(map[ny][nx] == 0) {
                    wall.add(new Virus(ny,nx));
                }
            }
        }
    }
    
    public static void copy() {
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                copymap[i][j] = map[i][j];
            }
        }
    }
    
}
cs



반응형