본문 바로가기
알고리즘/그래프

[java 백준] 골드 5/ 14502번 연구소

by Meaning_ 2022. 6. 28.
728x90
반응형

 

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
138
139
140
141
142
143
144
145
146
147
148
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
 
public class Main {
 
    public static int N;
    public static int M;
 
    public static int [][]arr;
    public static boolean visited[][];
    public static int [][]ans;
 
    public static int dirX[]={-1,1,0,0};
    public static int dirY[]={0,0,-1,1};
 
 
    public static int result=Integer.MIN_VALUE;
 
    public static class Node{
        int x,y;
        public Node(int x,int y){
            this.x=x;
            this.y=y;
        }
    }
 
 
    //바이러스 퍼뜨리기 (BFS)
 
    public static void BFS(){
        ans=new int[N][M];
        Queue<Node>queue=new LinkedList<Node>();
 
        //원본 배열 카피 (손실 안되게)
        for(int i=0;i<N;i++){
            for(int j=0;j<M;j++){
                ans[i][j]=arr[i][j];
            }
        }
 
        for(int i=0;i<N;i++){
            for(int j=0;j<M;j++){
                if(ans[i][j]==2){
                    queue.add(new Node(i,j));
                }
            }
        }
 
        while(!queue.isEmpty()){
 
            Node node=queue.poll();
            for(int s=0;s<4;s++){
                int nX=node.x+dirX[s];
                int nY=node.y+dirY[s];
 
                if(nX>=0&&nX<N&&nY>=0&&nY<M){
                    if(ans[nX][nY]==0){
 
                        queue.add(new Node(nX,nY));
                        ans[nX][nY]=2;
                    }
                }
            }
 
        }
 
        getScore(ans);
    }
 
    //안전영역의 크기를 계산
 
    public static void getScore(int[][]ans){
        int score=0;
        for(int i=0;i<N;i++){
            for(int j=0;j<M;j++){
                if(ans[i][j]==0){
                    score++;
                }
            }
        }
 
        result=Math.max(result,score);
 
    }
    
    //울타리 설치하면서 안전영역 계산
    public static void DFS(int depth){
 
        if(depth==3){
            BFS();
            return;
        }
 
        
        //울타리 넣어주기
        for(int i=0;i<N;i++){
            for(int j=0;j<M;j++){
                if(arr[i][j]==0){
                    arr[i][j]=1;
 
 
                    DFS(depth+1);
                    //백트래킹 느낌 
                    arr[i][j]=0;
 
 
                }
            }
        }
 
 
 
    }
 
 
    public static void main(String[]args) throws IOException {
 
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st=new StringTokenizer(br.readLine());
 
        N=Integer.parseInt(st.nextToken());
        M=Integer.parseInt(st.nextToken());
        arr=new int[N][M];
        visited=new boolean[N][M];
 
 
        for(int i=0;i<N;i++){
 
            st=new StringTokenizer(br.readLine());
            for(int j=0;j<M;j++){
 
                arr[i][j]=Integer.parseInt(st.nextToken());
 
            }
        }
 
        DFS(0);
        System.out.println(result);
 
 
 
    }
 
 
 
}
cs

 

문제를 풀어갈 때 생각의 절차

 

1. 울타리를 세워주기 위한 DFS 탐색

2. 울타리를 세워줬으면 BFS를 통해 바이러스 퍼뜨리기 -> 이때 울타리만 세워준 원본 배열에 손실이 가지 않게 copy배열 만들기

3. 안전영역 크기 계산

 

3개에 해당하는 메서드를 만들면 된다. 

 

문제 풀면서 실수한 부분

 

재귀가 깊어지지 않게 조건 조심, 매개변수 오타 조심.

 

728x90
반응형

댓글