CS/알고리즘_KAKAO BLIND RECRUITMENT

2018 KAKAO BLIND RECRUITMENT : [1차] 비밀지도

Jedy_Kim 2021. 6. 27. 19:44
728x90

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

 

코딩테스트 연습 - [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
// 0 : n*n 2차원 배열을 만든다.
// 1 : 10진수 값을 2진수로 변환하여 대입한다.
 
class Solution {
    public int[][] make_matrix(int[] arrs, int n){
        int n_cursor_row = 0;
        int n_cursor_col = n-1;
        int [][] matrix = new int[n][n];
        for(int arr : arrs) {            
            int temp_val = arr;
            while (temp_val >= 0) {  
                matrix[n_cursor_row][n_cursor_col] = temp_val % 2;  
                temp_val /= 2;
                n_cursor_col--
                if(n_cursor_col<0){  
                    break;
                }
            }     
            n_cursor_row++;
            n_cursor_col = n-1;
        }
        return matrix;
    }
    
    public String[] solution(int n, int[] arr1, int[] arr2) {
        String[] answer = {};
        
        int [][] matrix   = new int[n][n];
        int [][] matrix2  = new int[n][n];
        String [] matrix3 = new String[n];
        
        
        matrix  = make_matrix(arr1, n);
        matrix2 = make_matrix(arr2, n);
        
        for(int i = 0; i < n; i++) {
            String resStr = "";
            for(int j = 0; j < n; j++) {
                if(matrix[i][j] == 1 || matrix2[i][j] == 1) {
                    resStr += "#";
                } else if(matrix[i][j] == 0 && matrix2[i][j] == 0) {
                    resStr += " ";
                }
            } 
            matrix3[i] = resStr;            
        } 
        
        return matrix3;
    }
}
cs

 

반응형