-
[JAVA] 백준 17140 이차원 배열과 연산알고리즘 공부/문제 풀이 2021. 10. 16. 14:08
https://www.acmicpc.net/problem/17140
import java.util.*; public class Main { public static class Node implements Comparable<Node>{ int val; int cnt; public Node(int v, int c){ val = v; cnt = c; } public int compareTo(Node o){ if(cnt==o.cnt) return val-o.val; else return cnt - o.cnt; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int r = sc.nextInt()-1; int c = sc.nextInt()-1; int k = sc.nextInt(); int[][] A = new int[200][200]; for(int i=0;i<3;i++){ for(int j=0;j<3;j++) A[i][j] = sc.nextInt(); } int n = 3, m = 3; int time = -1; while(++time<=100){ if(A[r][c] == k) break; if(n>=m){ int maxN = 0; for(int i=0;i<n;i++){ int[] num = new int[101]; for(int j=0;j<m;j++){ num[A[i][j]]++; A[i][j] = 0; } PriorityQueue<Node> pq = new PriorityQueue<>(); for(int j=1;j<=100;j++){ if(num[j]!=0){ pq.add(new Node(j, num[j])); } } int idx = 0; while(!pq.isEmpty()){ Node node = pq.poll(); A[i][idx++] = node.val; A[i][idx++] = node.cnt; } if(maxN < idx) maxN = idx; } m = maxN; } else{ int maxM = 0; for(int i=0;i<m;i++){ int[] num = new int[101]; for(int j=0;j<n;j++){ num[A[j][i]]++; A[j][i] = 0; } PriorityQueue<Node> pq = new PriorityQueue<>(); for(int j=1;j<=100;j++){ if(num[j]!=0){ pq.add(new Node(j, num[j])); } } int idx = 0; while(!pq.isEmpty()){ Node node = pq.poll(); A[idx++][i] = node.val; A[idx++][i] = node.cnt; } if(maxM < idx) maxM = idx; } n = maxM; } } if(time==101) System.out.println(-1); else System.out.println(time); } }
'알고리즘 공부 > 문제 풀이' 카테고리의 다른 글
[JAVA] 백준 13334 철로 (0) 2021.10.22 [JAVA] 백준 17136 색종이 붙이기 (0) 2021.10.18 [JAVA] 백준 16438 원숭이 스포츠 (0) 2021.10.14 [JAVA] 백준 4095 최대 정사각형 (0) 2021.10.08 [JAVA] 백준 2638 치즈 (0) 2021.10.07