Algorithm

[BOJ] 14502. 연구소

프로그래민 2020. 5. 2. 18:21
반응형

구현할 것이 상당히 많은 문제였다.

기본적으로 벽이 갈수 있는 위치 3군대를 찾는 과정을 거치고 벽을 셋팅후 BFS하여 모든 0의 갯수를 세어주는 과정을 찾는 문제였다. 조합과 BFS를 연속하여 구현하기 때문에 빼먹을수 있는 부분이 많은 문제이므로 꼼꼼하게 함수로 나누어 코딩하는게 중요한 문제였다.

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
package boj;
 
 
public class Main_bj_14502_연구소 {
    static int[] di= {-1,0,1,0};
    static int[] dj= {0,1,0,-1};
    
    static int[][] originalMap;
    static boolean[][] visitForBFS;
    static int[][] map;
    static int R,C;
    
    static Pos[] result;
    static boolean[] visitForComb;
    
    static Queue<Pos> queue;
    static List<Pos> list;
    
    static class Pos{
        int i,j;
        Pos(int i,int j){
            this.i=i;this.j=j;
        }
    }
    
    static int answer;
    
    public static void main(String[] args) throws Exception  {
        BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = null;
        
        st=new StringTokenizer(br.readLine());
        R=Integer.parseInt(st.nextToken());
        C=Integer.parseInt(st.nextToken());
        
        list=new ArrayList<>();
        originalMap=new int[R][C];
        map=new int[R][C];
        
        for(int i=0;i<R;i++) {
            st=new StringTokenizer(br.readLine());
            for(int j=0;j<C;j++) {
                int num=Integer.parseInt(st.nextToken());
                originalMap[i][j]=num;
                if(num==0)
                    list.add(new Pos(i, j));
            }
        }
        
        visitForComb=new boolean[list.size()];
        result=new Pos[3];
        
        queue=new LinkedList<>();
        answer=0;
        comb(0,0);
        System.out.println(answer);
    }
    
    static void comb(int start,int depth) {
        if(depth==3) {
            check();
            
            int temp=0;
            for(int i=0;i<R;i++) {
                for(int j=0;j<C;j++) {
                    if(map[i][j]==0)
                        temp+=1;
                }
            }
            answer=Math.max(answer, temp);
            return;
        }
        
        for(int i=start;i<list.size();i++) {
            if(visitForComb[i]==false) {
                visitForComb[i]=true;
                result[depth]=list.get(i);
                comb(i,depth+1);
                visitForComb[i]=false;
            }
        }
    }
    
    static void check() {
        for(int i=0;i<R;i++) {
            for(int j=0;j<C;j++) {
                map[i][j]=originalMap[i][j];
            }
        }
        for(int index=0;index<3;index++) {
            map[result[index].i][result[index].j]=1;
        }
        
        
        bfs();
    }
    
    static void bfs() {
        visitForBFS=new boolean[R][C];
        
        for(int i=0;i<R;i++) {
            for(int j=0;j<C;j++) {
                if(map[i][j]==2 && visitForBFS[i][j]==false) {
                    queue.add(new Pos(i, j));
                    visitForBFS[i][j]=true;
                    
                    while(!queue.isEmpty()) {
                        Pos cur=queue.poll();
                        
                        for(int dir=0;dir<4;dir++) {
                            int nextI=cur.i+di[dir];
                            int nextJ=cur.j+dj[dir];
                            
                            if(nextI<0 || nextI>=|| nextJ<0 ||nextJ>=C)
                                continue;
                            
                            if(map[nextI][nextJ]==0 && visitForBFS[nextI][nextJ]==false) {
                                queue.offer(new Pos(nextI, nextJ));
                                visitForBFS[nextI][nextJ]=true;
                                map[nextI][nextJ]=2;
                            }
                        }
                    }
                }
            }
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
                                             
반응형

'Algorithm' 카테고리의 다른 글

[BOJ] 16998. Baaaaaaaaaduk2 (Easy)  (0) 2020.05.03
[Programmers] 징검다리 건너기  (0) 2020.05.03
[BOJ] 15686. 치킨 배달  (0) 2020.05.02
[SWEA] 4366. 정식이의 은행업무  (0) 2020.05.02
[SWEA] 1949. 등산로 조성  (0) 2020.05.02