CS/알고리즘_KAKAO BLIND RECRUITMENT

2020 KAKAO BLIND RECRUITMENT : 기둥과 보 설치

Jedy_Kim 2021. 9. 28. 10:26
728x90

https://programmers.co.kr/learn/courses/30/lessons/60061

 

코딩테스트 연습 - 기둥과 보 설치

5 [[1,0,0,1],[1,1,1,1],[2,1,0,1],[2,2,1,1],[5,0,0,1],[5,1,0,1],[4,2,1,1],[3,2,1,1]] [[1,0,0],[1,1,1],[2,1,0],[2,2,1],[3,2,1],[4,2,1],[5,0,0],[5,1,0]] 5 [[0,0,0,1],[2,0,0,1],[4,0,0,1],[0,1,1,1],[1,1,1,1],[2,1,1,1],[3,1,1,1],[2,0,0,0],[1,1,1,0],[2,2,0,1]] [[

programmers.co.kr

// 코드

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
class Solution {
    
    boolean[][] Pillar; // 기둥
    boolean[][] Bar;    // 바
    
    boolean checkPillar(int x, int y) {
        // 기둥을 바닥이나 기둥위에 세우는 경우
        if(y == 0 || Pillar[x][y-1]) return true;
        // 보위에 세우는 경우
        if(x>0 && Bar[x-1][y] || Bar[x][y]) return true;
        // 이외에는 세울 수 없음
        return false;
    }
    
    boolean checkBar(int x, int y) {
        // 기둥이 있는경우
        if(Pillar[x][y-1|| Pillar[x+1][y-1]) return true;
        // 양옆에 보가 있는 경우
        if(x>0 && Bar[x-1][y] && Bar[x+1][y]) return true;
        // 이외의 경우에는 보를 설치할 수 없다.
        return false;        
    }
    
    boolean canDelete(int x, int y) {
        for(int i=Math.max(x-10); i<= x+1; i++) {
            for(int j=y; j<= y+1; j++) {
                if(Pillar[i][j] && !checkPillar(i, j)) {
                    return false;
                }
                if(Bar[i][j] && !checkBar(i, j)) {
                    return false;
                }
            }
        } 
        return true;
    }
    
    public int[][] solution(int n, int[][] build_frame) {
        
        Pillar = new boolean[n+2][n+2];
        Bar    = new boolean[n+2][n+2];
        int cnt = 0;
        
        for(int[] build : build_frame) {
            int x = build[0], y = build[1];
            int type = build[2], cmd = build[3];
            // 기둥
            if(type == 0) {
                if(cmd == 1) { // 설치
                    if(checkPillar(x, y)) {
                        Pillar[x][y] = true;
                        cnt++;
                    }
                } else { // 삭제
                    Pillar[x][y] = false;
                    if(!canDelete(x, y)) {
                        Pillar[x][y] = true;
                    } else {
                        cnt--;
                    }
                    
                }
            } else { // 보
                if(cmd == 1) { // 설치
                    if(checkBar(x, y)) {
                        Bar[x][y] = true;
                        cnt++;
                    }
                } else { // 삭제
                    Bar[x][y] = false;
                    if(!canDelete(x, y)) {
                        Bar[x][y] = true;
                    } else {
                        cnt--;
                    }
                }
            }
            
        }
        
        
        int[][] answer = new int[cnt][3];        
        cnt = 0;
        
        for(int x=0; x<=n; x++) {
            for(int y=0; y<=n; y++) {
                if(Pillar[x][y]) {
                    answer[cnt][0= x;
                    answer[cnt][1= y;
                    answer[cnt++][2= 0;
                }
                
                if(Bar[x][y]) {
                    answer[cnt][0= x;
                    answer[cnt][1= y;
                    answer[cnt++][2= 1;
                }
            }
        }
        
        return answer;
    }
}
cs

// 코드

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
class Solution {
    // 기둥과 바의 위치를 별도로 관리해준다.
    boolean[][] Pillar; // 기둥
    boolean[][] Bar;    // 바
    
    boolean checkPillar(int x, int y) {
        // 기둥을 바닥이나 기둥위에 세우는 경우
        if(y == 0 || Pillar[x][y-1]) return true;
        // 기둥을 보위에 세우는 경우
        if(x>0 && Bar[x-1][y] || Bar[x][y]) return true;
        // 이외에는 세울 수 없음        
        return false;
    }
    
    boolean checkBar(int x, int y) {
        // 기둥이 있는 경우
        if(Pillar[x][y-1|| Pillar[x+1][y-1]) return true;
        // 양면에 보가 있는 경우
        if(x>0 && Bar[x-1][y] && Bar[x+1][y]) return true;
        // 이외에는 설치할 수 없음
        return false;
    }
    
    // 기둥, 보를 삭젝할 수 있는지 여부를 체크하는 메서드
    boolean canDelete(int x, int y) {
        for(int i=Math.max(x-10); i<=x+1; i++) {
            for(int j=y; j<=y+1; j++) {                
                if(Pillar[i][j] && !checkPillar(i, j)) return false;
                if(Bar[i][j] && !checkBar(i, j)) return false;
            }
        }
        return true;
    }
    
    public int[][] solution(int n, int[][] build_frame) {
        // n = 5 인 경우
        // |0|1|2|3|4|5|
        // 즉 배열안의 값을 기둥혹은 보의 값으로 넣을 것이기에 n+2로 해줘야된다.
        Pillar  = new boolean[n+2][n+2];
        Bar     = new boolean[n+2][n+2];
        int cnt = 0;
        
        for(int[] build : build_frame) {
            int x    = build[0], y   = build[1]; // x, y는 기둥, 보의 교차점 [가로좌표, 세로좌표]
            int type = build[2], cmd = build[3]; // type : 0은 기둥 / 1은 보, cmd : 구조물 설치(1) 혹은 삭제(0)
            // 기둥
            if(type == 0) {
                // 설치
                if(cmd == 1) {
                    // 기둥 설치 가능여부를 체크한다.
                    if(checkPillar(x, y)) {
                        Pillar[x][y] = true;
                        cnt++;
                    }
                }
                // 삭제
                else {
                    // 우선 지워본다.
                    Pillar[x][y] = false;
                    // 삭제할 수 없는 경우 다시 되돌린다.
                    if(!canDelete(x, y)) Pillar[x][y] = true;
                    // 삭제 가능한 경우
                    else cnt--;
                }                    
            }
            // 보
            else {
                // 설치
                if(cmd == 1) {
                    // 바 설치 가능여부를 체크한다.
                    if(checkBar(x, y)) {
                        Bar[x][y] = true;
                        cnt ++;
                    }                    
                }
                // 삭제
                else {
                    // 우선 지워본다.
                    Bar[x][y] = false;
                    // 삭제할 수 없는 경우 다시 되돌린다.
                    if(!canDelete(x, y)) Bar[x][y] = true;
                    // 삭제 가능한 경우
                    else cnt--;
                }
            }
        }
        
        
        int[][] answer = new int[cnt][3];
        cnt = 0;
        for(int x=0; x<=n; x++) {
            for(int y=0; y<=n; y++) {
                if(Pillar[x][y]) {
                    answer[cnt][0]   = x;
                    answer[cnt][1]   = y;
                    answer[cnt++][2= 0;
                }
                
                if(Bar[x][y]) {
                    answer[cnt][0]   = x;
                    answer[cnt][1]   = y;
                    answer[cnt++][2= 1;
                }
            }
        }
        
        return answer;
    }
}
cs

 

반응형