-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain17140_이차원배열과연산.java
170 lines (145 loc) · 3.58 KB
/
Main17140_이차원배열과연산.java
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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;
//2019-08-01
public class Main17140_이차원배열과연산 {
private static int r;
private static int c;
private static int k;
private static int[][] map;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
r = Integer.parseInt(st.nextToken());
c = Integer.parseInt(st.nextToken());
k = Integer.parseInt(st.nextToken());
map = new int[3][3];
for (int i = 0; i < 3; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < 3; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
}
} /// input
int time = 0;
while (true) {
if(map.length>r-1 && map[0].length>c-1 && map[r-1][c-1]==k) break;
int row = map.length;
int column = map[0].length;
time++;
//System.out.println(time);
if(time>100) {
time = -1;
break;
}
if (row >= column) {
rCalc();
} else {
cCalc();
}
}
System.out.println(time);
}
public static void rCalc() {
int[][] check = new int[map.length][101];
// 모든 행에 대해서 정렬 수행
int nextC = 0;
for (int i = 0; i < map.length; i++) {
int tmpC = 0;
for (int j = 0; j < map[i].length; j++) {
if (map[i][j] == 0)
continue;
check[i][map[i][j]]++;
if (check[i][map[i][j]] == 1) { // 새로 더해지는 경우만.
tmpC++;
}
}
nextC = nextC < tmpC ? tmpC : nextC;
}
nextC = nextC*2 > 99 ? 99 : nextC*2;
map = new int[map.length][nextC];
List<value> list = new ArrayList<>();
for (int i = 0; i < map.length; i++) {
list.clear();
for (int j = 1; j < 101; j++) {
if (check[i][j] == 0)
continue;
list.add(new value(j, check[i][j]));
}
Collections.sort(list);
int start;
if(list.size()>50) {
start = list.size()-50;
} else {
start = 0;
}
int x=0;
for(int idx = start ; idx<list.size(); idx++) {
value v = list.get(idx);
map[i][x++] = v.num;
map[i][x++] = v.cnt;
if(x>99) break;
}
}
}
public static void cCalc() {
int[][] check = new int[101][map[0].length];
// 모든 열에 대해서 정렬 수행
int nextR = 0;
for (int j = 0; j < map[0].length; j++) {
int tmpR = 0;
for (int i = 0; i < map.length; i++) {
if (map[i][j] == 0)
continue;
check[map[i][j]][j]++;
if (check[map[i][j]][j] == 1) { // 새로 더해지는 경우만.
tmpR++;
}
}
nextR = nextR < tmpR ? tmpR : nextR;
}
nextR = nextR*2 > 99 ? 99 : nextR*2;
map = new int[nextR][map[0].length];
List<value> list = new ArrayList<>();
for (int j = 0; j < map[0].length; j++) {
list.clear();
for (int i = 1; i < 101; i++) {
if (check[i][j] == 0)
continue;
list.add(new value(i, check[i][j]));
}
Collections.sort(list);
int start;
if(list.size()>50) {
start = list.size()-50;
} else {
start = 0;
}
int x = 0;
for(int idx = start ; idx<list.size(); idx++) {
value v = list.get(idx);
map[x++][j] = v.num;
map[x++][j] = v.cnt;
if(x>99) break;
}
}
}
static class value implements Comparable<value> {
int num;
int cnt;
public value(int num, int cnt) {
super();
this.num = num;
this.cnt = cnt;
}
@Override
public int compareTo(value o) {
if (this.cnt == o.cnt)
return this.num - o.num; // 내림차순
else
return this.cnt - o.cnt; // 내림차순
}
}
}