본문 바로가기

Algorithm

백준 17140 이차원 배열과 연산 Java

삼성 SW 역량테스트 문제인 이차원 배열과 연산이다.

시뮬레이션 문제인데 나한텐 어려웠다..

요즘 조금복잡한 시뮬레이션 문제를 못풀어내고 있다.

 

이 문제는 생각해야되는 포인트가

0이 채워지는 부분과

정렬해야 되는 부분,

행과 열에 크기가 100이 넘어가지 않는 부분이다.

 

내가 캐치하지 못했던 점은

가장 큰 열 또는 가장 큰 행을 MAX라고 할 때

행의 길이가 row이고 열의 길이가 col인 배열을 R 연산하면

[row][MAX] 인 배열이 나오게 되고

 

C 연산하면

[MAX][col] 인 배열이 나오게 된다는 점이다.

이 포인트를 캐치했다면  0을 채우는 부분을 해결할 수 있다.

 

나오게되는 값을 채워놓으면 빈자리는 알아서 0이 남기 때문이다.

ArrayList를 사용하지않고 기본 배열을 사용하면 편하다.

 

정렬해야 되는 부분은 임의의 클래스를 사용해서 

Comparable을 구현해서 해결하면 되고

 

최대 길이가 100이 넘지 않는 부분은

R연산시에는 크기가 [row][MAX]로 나오기 때문에

row부분은 신경쓰지 않고 MAX부분만 100이 넘지 않도록 신경써주면 된다.

C연산도 마찬가지로 col은 신경쓰지 않고 MAX만 신경쓰면 된다.

 

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
import java.util.*;
import java.io.*;
 
public class Q17140 {
    static int r,c,k;
    static int[][] map;
    static int[] cnt;
    static List<Temp> list;
 
    public static void main(String[] args) throws IOException{
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(reader.readLine());
        r  = Integer.parseInt(st.nextToken())-1;
        c  = Integer.parseInt(st.nextToken())-1;
        k  = Integer.parseInt(st.nextToken());
 
        map = new int[3][3];
        for(int i=0; i<3; i++) {
            st = new StringTokenizer(reader.readLine());
            for(int j=0; j<3; j++) {
                map[i][j] = Integer.parseInt(st.nextToken());
            }
        }
        int time = -1;
 
        while(true) {
            time++;
 
            if(time > 100) {
                time = -1;
                break;
            }
            if(r < map.length && c < map[0].length) {
                if(map[r][c] == k)     break;
            }
 
            int row = map.length;
            int col = map[0].length;
 
            int[][] temp = new int[101][101];
 
            if(row >= col) {
                int max = Integer.MIN_VALUE;
                for(int i=0; i<row; i++) {
                    cnt = new int[101];
                    for(int j=0; j<col; j++) {
                        cnt[map[i][j]]++;
                    }
                    list = new ArrayList<>();
                    for(int j=1; j<=100; j++) {
                        if(cnt[j] >= 1) {
                            list.add(new Temp(j, cnt[j]));
                        }
                    }
                    Collections.sort(list);
                    int z = 0;
                    for(int j=0; j<list.size(); j++) {
                        temp[i][z++= list.get(j).num;
                        temp[i][z++= list.get(j).fre;
                    }
                    if(max < list.size()*2) max = list.size()*2;
                }
                if(max > 100) max = 100;
                map = new int[row][max];
                for(int i=0; i<map.length; i++) {
                    for(int j=0; j<map[i].length; j++) {
                        map[i][j] = temp[i][j];
                    }
                }
            }
            else {
                int max = Integer.MIN_VALUE;
                for(int i=0; i<col; i++) {
                    cnt = new int[101];
                    for(int j=0; j<row; j++) {
                        cnt[map[j][i]]++;
                    }
                    list = new ArrayList<>();
                    for(int j=1; j<=100; j++) {
                        if(cnt[j] >= 1) {
                            list.add(new Temp(j, cnt[j]));
                        }
                    }
                    Collections.sort(list);
                    int z = 0;
                    for(int j=0; j<list.size(); j++) {
                        temp[z++][i] = list.get(j).num;
                        temp[z++][i] = list.get(j).fre;
                    }
                    if(max < list.size()*2) max = list.size()*2;
                }
                if(max > 100) max = 100;
                map = new int[max][col];
                for(int i=0; i<map.length; i++) {
                    for(int j=0; j<map[i].length; j++) {
                        map[i][j] = temp[i][j];
                    }
                }
            }
        }
        System.out.println(time);
    }
}
class Temp implements Comparable<Temp>{
    int num;
    int fre;
 
    public Temp(int num, int fre) {
        this.num = num;
        this.fre = fre;
    }
 
    @Override
    public int compareTo(Temp o) {
        int r = this.fre - o.fre;
        if(r == 0) r =     this.num - o.num;
        return r;
    }
}
 
 

https://kim6394.tistory.com/208 

이분의 글을 참고하여 틀린부분을 수정하였다.

'Algorithm' 카테고리의 다른 글

백준 15683 감시 Java  (0) 2019.09.19
백준 2607 비슷한 단어 Java  (0) 2019.09.19
백준 15686 치킨 배달 Java  (0) 2019.09.13
백준 14500 테트로미노 Java  (0) 2019.09.11
2019 KAKAO 매칭 점수 Java  (0) 2019.09.06