[https://www.acmicpc.net/problem/17140](https://www.acmicpc.net/problem/17140
λ¬Έμ μ€λͺ
λ°°μ΄ A
: μ²μμ 3x3μΈ λ°°μ΄, μΈλ±μ€λ 1λΆν° μμ
1μ΄κ° μ§λ λλ§λ€ μ°μ°μ΄ μ μ©λ¨
π Rμ°μ° : λͺ¨λ νμ λν΄μ μ λ ¬ μν, νμ κ°μ >= μ΄μ κ°μ
π Cμ°μ° : λͺ¨λ μ΄μ λν΄μ μ λ ¬ μν, νμ κ°μ < μ΄μ κ°μ
κ°κ°μ μκ° λνλ νμμ μ€λ¦μ°¨μμΌλ‘, κ°λ€λ©΄ μ«μκ° μ»€μ§λ μμΌλ‘ μ λ ¬ν΄μ λ°°μ΄Aμ λ£κΈ°
ν λλ μ΄μ ν¬κΈ°κ° 100μ΄ λμ΄κ°λ©΄, μ²μ 100κ°λ§βΌοΈ
A[r][c] == kκ° λλ μ΅μ μκ°
100μ΄κ° μ§λλ kκ° λμ§ μμΌλ©΄ -1 μΆλ ₯
λ¬Έμ νμ΄
Map<num, cnt>
: μ«μλ€μ κ°―μPriorityQueue<pair>
: pair(μ, κ°―μ) μ μ₯
operating()
1) xLen >= yLen π R()
2) xLen < xLen π C()
μ½λ
import javax.swing.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class Main_BOJ_17140_μ΄μ°¨μ_λ°°μ΄κ³Ό_μ°μ° {
static class pair implements Comparable<pair>{
int num;
int cnt;
public pair(int num, int cnt) {
this.num = num;
this.cnt = cnt;
}
@Override
public int compareTo(pair o) {
int result = this.cnt - o.cnt;
if(result == 0)
result = this.num - o.num;
return result;
}
}
static int r, c, k;
static int[][] A = new int[101][101];
static int xLen = 3, yLen = 3;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer stringTokenizer = new StringTokenizer(br.readLine());
r = Integer.parseInt(stringTokenizer.nextToken());
c = Integer.parseInt(stringTokenizer.nextToken());
k = Integer.parseInt(stringTokenizer.nextToken());
for (int i = 1; i <= 3; i++) {
stringTokenizer = new StringTokenizer(br.readLine());
for (int j = 1; j <= 3; j++) {
A[i][j] = Integer.parseInt(stringTokenizer.nextToken());
}
}
System.out.println(solution());
}
private static int solution() {
for (int time = 0; time <= 100; time++) {
if(A[r][c] == k) return time;
operating();
}
return -1;
}
private static void operating() {
if (xLen >= yLen) {
for (int i = 1; i <= xLen; i++) R(i);
} else {
for (int i = 1; i <= yLen; i++) C(i);
}
}
private static void C(int key) {
PriorityQueue<pair> pq = new PriorityQueue<>();
Map<Integer, Integer> map = new HashMap<>();
for (int i = 1; i <= xLen; i++) {
if(A[i][key] == 0) continue;
map.compute(A[i][key], (num, cnt) -> cnt == null ? 1 : cnt + 1);
}
map.forEach((k, v) -> pq.add(new pair(k, v)));
int i = 1;
while (!pq.isEmpty()) {
pair p = pq.poll();
A[i++][key] = p.num;
A[i++][key] = p.cnt;
}
xLen = Math.max(xLen, i);
while (i <= 99) {
A[i++][key] = 0;
A[i++][key] = 0;
}
}
private static void R(int key) {
PriorityQueue<pair> pq = new PriorityQueue<>();
Map<Integer, Integer> map = new HashMap<>();
for (int i = 1; i <= yLen; i++) {
if(A[key][i] == 0) continue;
map.compute(A[key][i], (num, cnt) -> cnt == null ? 1 : cnt + 1);
}
map.forEach((k, v) -> pq.add(new pair(k, v)));
int i = 1;
while (!pq.isEmpty()) {
pair p = pq.poll();
A[key][i++] = p.num;
A[key][i++] = p.cnt;
}
yLen = Math.max(yLen, i);
while(i <= 99){
A[key][i++] = 0;
A[key][i++] = 0;
}
}
}
'μκ³ λ¦¬μ¦ > λ°±μ€' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[ BOJ / λ°±μ€ 19238 ] μ€ννΈ νμ ( μλ° / JAVA ) (0) | 2022.02.04 |
---|---|
[ λ°±μ€ / BOJ 17779 ] κ²λ¦¬λ©λλ§_2 ( μλ° / JAVA ) (0) | 2022.02.02 |
[ λ°±μ€ / BOJ 17135 ] μΊμ¬ λνμ€ ( μλ° / JAVA ) (0) | 2022.01.31 |
[ λ°±μ€ / BOJ 16236 ] μκΈ° μμ΄ ( μλ°/JAVA ) (0) | 2022.01.30 |
[ λ°±μ€ / BOJ 17144 ] λ―ΈμΈλ¨Όμ§ μλ ! ( JAVA ) (0) | 2022.01.30 |