티스토리 뷰
https://www.acmicpc.net/problem/17140
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.util.*;
public class P17140 {
static int r, c, k;
static int ans=-1;
static ArrayList<ArrayList<Integer>> board=new ArrayList<>(3);
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer(br.readLine());
r=Integer.parseInt(st.nextToken())-1;
c=Integer.parseInt(st.nextToken())-1;
k=Integer.parseInt(st.nextToken());
for (int i=0; i<3; i++){
st=new StringTokenizer(br.readLine());
board.add(new ArrayList<>());
for (int j=0; j<3; j++) {
board.get(i).add(Integer.parseInt(st.nextToken()));
}
}
for (int t=0; t<=100; ++t) {
int rowSize = board.size();
int colSize = board.get(0).size();
if (r < rowSize && c < colSize && board.get(r).get(c) == k) {
ans = t;
break;
}
if (rowSize >= colSize) {
RCalc();
} else {
CCalc();
}
/*
System.out.println();
for (int i=0; i<board.size(); i++){
System.out.println(board.get(i).toString());
}
*/
}
System.out.println(ans);
}
public static void RCalc(){ //행 단위
int maxSize=0;
for (int i=0; i<board.size(); i++){
int cnt=0;
ArrayList<Integer> temp=new ArrayList<>();
PriorityQueue<Infos> pq=new PriorityQueue<>();
Map<Integer, Integer> m=new HashMap<>();
for (int j=0; j<board.get(i).size(); j++){
int item=board.get(i).get(j);
if (item!=0) {
int count = m.containsKey(item) ? m.get(item) : 0;
m.put(item, count + 1);
}
}
for (Integer itg: m.keySet()){
pq.add(new Infos(itg, m.get(itg)));
}
while (!pq.isEmpty()){
cnt++;
if (cnt>100){
break;
}
Infos info=pq.poll();
temp.add(info.num); temp.add(info.cnt);
}
board.get(i).clear();
board.get(i).addAll(temp);
maxSize=board.get(i).size()>maxSize?board.get(i).size():maxSize;
}
for (int i=0; i<board.size(); i++){
int len=board.get(i).size();
if (len<maxSize){
for (int j=0; j<maxSize-len; j++){
board.get(i).add(0);
}
}
}
}
public static void CCalc(){
for (int i=0; i<board.get(0).size(); i++){
int cnt=0;
ArrayList<Integer> temp=new ArrayList<>();
PriorityQueue<Infos> pq=new PriorityQueue<>();
Map<Integer, Integer> m=new HashMap<>();
for (int j=0; j<board.size(); j++){
int item=board.get(j).get(i);
if (item!=0) {
int count = m.containsKey(item) ? m.get(item) : 0;
m.put(item, count + 1);
}
}
for (Integer itg: m.keySet()){
pq.add(new Infos(itg, m.get(itg)));
}
while (!pq.isEmpty()){
cnt++;
if (cnt>100){
break;
}
Infos info=pq.poll();
temp.add(info.num); temp.add(info.cnt);
}
if (board.size()<temp.size()){
int len=temp.size()-board.size();
for (int k=0; k<len; k++){
board.add(new ArrayList<>(Collections.nCopies(board.get(0).size(),0)));
}
}
for (int k=0; k<board.size(); k++){
if (k>=temp.size()){
board.get(k).set(i, 0);
}
else{
board.get(k).set(i, temp.get(k));
}
}
}
}
public static class Infos implements Comparable<Infos>{
int num;
int cnt;
public Infos(int num, int cnt){
this.num=num;
this.cnt=cnt;
}
@Override
public int compareTo(Infos i){
if (i.cnt==this.cnt){
return this.num>=i.num?1:-1;
}
return this.cnt>=i.cnt?1:-1;
}
}
}
풀이가 너무 더럽다..
https://kora1492.tistory.com/14
잘만 생각해보면 map을 쓸 필요, pq도 쓸 필요도 없다!
Infos 객체를 담는 arraylist 객체를 sort 하면 우선순위에 맞는 배열이 나온다.
그리고 board를 board[101][101] 의 2차원 배열로 생성한다.
수의 횟수를 셀때는 1부터 100까지 수가 있을 수 있다는 점을 이용해, cnt[101] 배열을 만들어 각 인덱스(수)에 맞는 수 갯수를 세주면 된다!!
'Problem Solving' 카테고리의 다른 글
[BOJ 17135] 캐슬 디펜스 (Java) (0) | 2022.01.19 |
---|---|
[BOJ 2661] 좋은 수열 (Java) (0) | 2022.01.18 |
[BOJ 1963] 소수 경로 (Java) (0) | 2022.01.13 |
[BOJ 2589] 보물섬 (JAVA) (0) | 2022.01.04 |
[프로그래머스] 블록 이동하기 (C++) (0) | 2021.12.16 |