본문 바로가기

ProgramSoliving

백준 : 17140

반응형

https://www.acmicpc.net/problem/17140

 

17140번: 이차원 배열과 연산

첫째 줄에 r, c, k가 주어진다. (1 ≤ r, c, k ≤ 100) 둘째 줄부터 3개의 줄에 배열 A에 들어있는 수가 주어진다. 배열 A에 들어있는 수는 100보다 작거나 같은 자연수이다.

www.acmicpc.net

 

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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import java.io.*;
import java.util.*;
 
 
public class Main {
    static int r, c, k;
    static int[][] arr;
    static int cnt = 0;
 
    public static class A implements Comparable<A> {
 
        int v;
        int cnt;
 
        A(int v, int cnt) {
            this.v = v;
            this.cnt = cnt;
        }
 
        @Override
        public int compareTo(A o) {
            // TODO Auto-generated method stub
            if (this.cnt > o.cnt) {
                return 1;
            } else if (this.cnt == o.cnt) {
                if (this.v > o.v) {
                    return 1;
                } else {
                    return -1;
                }
            } else {
                return -1;
            }
        }
    }
 
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer st;
 
        st = new StringTokenizer(br.readLine());
 
        r = Integer.parseInt(st.nextToken());
        c = Integer.parseInt(st.nextToken());
        k = Integer.parseInt(st.nextToken());
        arr = new int[100][100];
 
        for (int i = 0; i < 3; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j = 0; j < 3; j++) {
                arr[i][j] = Integer.parseInt(st.nextToken());
            }
        }
 
        int R = 3;
        int C = 3;
 
        while (true) {
//            System.out.println("R , C : " + R + " " + C);
            if (cnt > 100) {
                bw.write(-1 + "");
                bw.flush();
                break;
            } else if (arr[r - 1][c - 1== k && r <= R && c <= C) {
                bw.write(cnt + "");
                bw.flush();
                break;
            }
            //
 
            if (R >= C) {
                // 행검사
                int tmp = 0;
                for (int i = 0; i < R; i++) {
                    ArrayList<A> myArray = new ArrayList<A>();
                    for (int j = 0; j < C; j++) {
                        // Array에 몇번째에 있는지
                        if (arr[i][j] == 0)
                            continue;
                        int look = -1;
                        for (int k = 0; k < myArray.size(); k++) {
                            if (myArray.get(k).v == arr[i][j]) {
                                look = k;
                            }
                        }
                        if (look == -1) {
                            myArray.add(new A(arr[i][j], 1));
                        } else {
                            myArray.set(look, new A(arr[i][j], myArray.get(look).cnt + 1));
                        }
                    }
                    // 정렬하면됨 카운트 기준 오름차순
                    Collections.sort(myArray);
//                    System.out.println(i + 1 + "행정렬");
//                    for (int myNum = 0; myNum < myArray.size(); myNum++) {
//                        System.out.printf("%d %d ", myArray.get(myNum).v, myArray.get(myNum).cnt);
//                    }
//                    System.out.println();
 
                    // 초기화
 
                    for (int j = 0; j < C; j++) {
                        arr[i][j] = 0;
                    }
 
                    int set = 0;
                    for (int myNum = 0; myNum < myArray.size(); myNum++) {
                        arr[i][set++= myArray.get(myNum).v;
                        arr[i][set++= myArray.get(myNum).cnt;
                    }
 
                    if (tmp < set) {
                        tmp = set;
                    }
 
                }
                C = tmp;
            } else {
                // 열검사
                int tmp = 0;
                for (int j = 0; j < C; j++) {
                    ArrayList<A> myArray = new ArrayList<A>();
                    for (int i = 0; i < R; i++) {
                        // Array에 몇번째에 있는지
                        if (arr[i][j] == 0)
                            continue;
                        int look = -1;
                        for (int k = 0; k < myArray.size(); k++) {
                            if (myArray.get(k).v == arr[i][j]) {
                                look = k;
                            }
                        }
                        if (look == -1) {
                            myArray.add(new A(arr[i][j], 1));
                        } else {
                            myArray.set(look, new A(arr[i][j], myArray.get(look).cnt + 1));
                        }
                    }
                    // 정렬하면됨 카운트 기준 오름차순
                    Collections.sort(myArray);
 
                    for (int i = 0; i < R; i++) {
                        arr[i][j] = 0;
                    }
 
                    int set = 0;
//                    System.out.println(j + 1 + "열정렬");
//                    for (int myNum = 0; myNum < myArray.size(); myNum++) {
//                        System.out.printf("%d %d ", myArray.get(myNum).v, myArray.get(myNum).cnt);
//                    }
//                    System.out.println();
                    for (int myNum = 0; myNum < myArray.size(); myNum++) {
                        arr[set++][j] = myArray.get(myNum).v;
                        arr[set++][j] = myArray.get(myNum).cnt;
                    }
                    if (set > tmp) {
                        tmp = set;
                    }
 
                }
                R = tmp;
                //
            }
 
            cnt++;
        }
 
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
반응형

'ProgramSoliving' 카테고리의 다른 글

백준 : 12100  (0) 2020.01.29
백준 : 16235  (0) 2020.01.28
백준 : 15683*  (0) 2020.01.21
백준 : 13460*  (0) 2020.01.18
백준 : 15685 *  (0) 2020.01.15