본문 바로가기

Programmers/Level3

기둥과 보 설치

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
import java.util.*;
class Solution {
    private static boolean[][] arr0, arr1;
    private static int cnt = 0;
    public static boolean CheckFrame(int n, int[] frame, int i, int j, int type){
        // 기둥일 경우
        if(type == 0) {
            // 바닥, 기둥의 위, 보의 끝 부분일 경우만 수행
            if(i == 0 || i > 0 && arr0[i-1][j] || arr1[i][j] || j > 0 && arr1[i][j-1])
                return true;
        } else { // 보일 경우
            // 기둥의 위, 보의 양쪽 끝 부분이 연결된 경우만 수행
            if(i > 0 && arr0[i-1][j]|| i > 0 && j < n && arr0[i-1][j+1|| j > 0 && j < n && arr1[i][j-1&& arr1[i][j+1])
                return true;
        }
        return false;
    }
    
    public static boolean CheckRemove(int n, int[] frame, int i, int j, int type){
        // 범위 밖일 경우
        if(i > n || i < 0 || j > n || j < 0)
            return true;
        // 비어있는 경우
        if(type == 0){
            if(!arr0[i][j])
                return true;      
        } else {
            if(!arr1[i][j])
                return true;
        }
        if(CheckFrame(n, frame, i, j, type))
            return true;
        else
            return false;
    }
    
    public static void setFrame(int n, int[] frame){
        int i = frame[1]; // 행
        int j = frame[0]; // 열
        int type = frame[2];
        int action = frame[3];
        String pos = Integer.toString(i) + Integer.toString(j);
        // 범위 밖일 경우
        if(i > n || i < 0 || j > n || j < 0)
            return;
        
        // 프레임 설치
        if(action == 1){
            if(CheckFrame(n, frame, i, j, type)){
                if(type == 0)
                    arr0[i][j] = true;
                else
                    arr1[i][j] = true;
                cnt++;
            }
        } else { // 프레임 삭제
            if(type == 0)
                arr0[i][j] = false;
            else
                arr1[i][j] = false;
            cnt--;
            boolean flag = true;
 
            for(int l=0;l<=n;l++){
                for(int m=0;m<=n;m++){
                    // 기둥과 보 규칙 검사
                    if(!CheckRemove(n, frame, l, m, 0|| !CheckRemove(n, frame, l, m, 1)){
                        flag = false;
                        break;
                    }
                }
                if(!flag){
                    if(type == 0)
                        arr0[i][j] = true;
                    else
                        arr1[i][j] = true;
                    cnt++;
                    break;
                }
            } 
        }
    }
    
    public int[][] solution(int n, int[][] build_frame) {
        int[][] answer = {};
        arr0 = new boolean[n+1][n+1];
        arr1 = new boolean[n+1][n+1];
        for(int i=0;i<build_frame.length;i++){
            setFrame(n, build_frame[i]);            
        }
 
        answer = new int[cnt][3];
        int idx = 0;
        for(int i=0;i<=n;i++){
            for(int j=0;j<=n;j++){
                if(arr0[i][j]){
                    answer[idx][0= j;
                    answer[idx][1= i;
                    answer[idx][2= 0;
                    idx++;        
                }
                if(arr1[i][j]){
                    answer[idx][0= j;
                    answer[idx][1= i;
                    answer[idx][2= 1;
                    idx++;        
                }
            }
        }
        
        Arrays.sort(answer, new Comparator<int[]>(){
           @Override
            public int compare(int[] o1, int[] o2) {
                int a = o1[0- o2[0];
                if (a == 0)
                    a = o1[1- o2[1];
                if (a == 0)
                    return o1[2- o2[2];
                return a;
            }
        });
 
        return answer;
    }
}
cs

주어진 조건에 맞춰서 설치와 삭제를 수행하면 된다.

배열의 크기가 크지 않으므로 삭제 조건 확인 시 전체 배열을 검사하도록 하였다.

'Programmers > Level3' 카테고리의 다른 글

숫자 게임  (0) 2021.11.03
가장 긴 팰린드롬  (0) 2021.09.23
기지국 설치  (0) 2021.09.15
[카카오 인턴] 경주로 건설  (0) 2021.09.15
섬 연결하기  (0) 2021.09.14