본문 바로가기
My Image
Algorithm/BackJoon

[삼성SW테스트] 백준 17140 이차원 배열과 연산(시뮬레이션)

by Lim-Ky 2019. 8. 8.
반응형

백준 시뮬레이션 문제

#17140- 이차원 배열과 연산

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

 

생각보다 간단한 축에 속하는 시뮬레이션 문제!

이 문제의 포인트는 

 

"한 행 또는 열에 있는 수를 정렬하려면, 각각의 수가 몇 번 나왔는지 알아야 한다. 그 다음, 수의 등장 횟수가 커지는 순으로, 그러한 것이 여러가지면 수가 커지는 순으로 정렬한다. 그 다음에는 배열 A에 정렬된 결과를 다시 넣어야 한다. 정렬된 결과를 배열에 넣을 때는, 수와 등장 횟수를 모두 넣으며, 순서는 수가 먼저이다."

 

이부분을 어떻게 비교하고 정렬을 할 것인가이다.

나는 compareTo를 커스터마이징해서, 비교하여 정렬을 하도록 소스를 짰다.

 

 

전체소스

import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Scanner;

public class BackJoon_17140 {

	static int rSize = 101;
	static int cSize = 101;
	static int[] countInfo = new int[101];
	static int[][] map = new int[rSize][cSize];
	static int r;
	static int c;
	static int k;
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		r = sc.nextInt();
		c = sc.nextInt();
	    k = sc.nextInt();
		
		rSize = 3;
		cSize = 3;
		for(int i=0; i<rSize; i++){
			for(int j=0; j<cSize; j++){
				map[i][j] = sc.nextInt(); 
			}
		}
		
		
		//while() 루프 돌면서
		//1.행/열 길이 체크
		//2.R연산, C연산
		int t;
		for(t=0; t<=100; t++){
			//map[r][c] == k 체크
			if(map[r-1][c-1] == k){
				break;
			}
			
			
			LinkedList<MyInfo> list = new LinkedList<>();
			
			//행 >= 열
			if(cSize >= rSize){
				//R연산
				int tmpRsize = 0;
				for(int i=0; i<cSize; i++){
					for(int j=0; j<rSize; j++){
						countInfo[map[i][j]]++;
						map[i][j] = 0;
					}
					for(int z=1; z<countInfo.length; z++){
						if(countInfo[z] != 0){
							list.add(new MyInfo(z, countInfo[z]));
						}
					}
					Collections.sort(list);
					
					int index = 0;	

					for(MyInfo m : list){
						map[i][index++] = m.number;
						map[i][index++] = m.count;
					}
					
					//행,열 길이 갱신 (R연산인 경우 열길이만 갱신)
					if(tmpRsize <= index){
						tmpRsize = index;
					}
					
					//초기화
					list.clear();
					Arrays.fill(countInfo, 0);
				}
				rSize = tmpRsize;
		
			}else{
				//operate = "[C연산]";
				//C연산
				int tmpCsize = 0;
				for(int i=0; i<rSize; i++){
					for(int j=0; j<cSize; j++){
						countInfo[map[j][i]]++;
						map[j][i] = 0;
					}
					for(int z=1; z<countInfo.length; z++){
						if(countInfo[z] != 0){
							list.add(new MyInfo(z, countInfo[z]));
						}
					}
					Collections.sort(list);
					
					int index = 0;
								
					for(MyInfo m : list){
						map[index++][i] = m.number;
						map[index++][i] = m.count;
					}
					
					//행,열 길이 갱신 (C연산인 경우 행길이만 갱신)
					if(tmpCsize <= index){
						tmpCsize = index;
					}
					
					//초기화
					list.clear();
					Arrays.fill(countInfo, 0);
				}
				cSize = tmpCsize;
			}
			//연산종료

		}
		
		if(t==101) t = -1;
		System.out.println(t);
		
		
	}

	static class MyInfo implements Comparable<MyInfo>{
		int number;
		int count;
		
		MyInfo(int number, int count){
			this.number = number;
			this.count = count;
		}
		
		@Override
		public int compareTo(MyInfo o) {
			if(this.count < o.count){
				return -1;
			}else if(this.count > o.count){
				return 1;	
			}else{
				//count가 동률인경우
				if(this.number < o.number){
					return -1;
				}else if(this.number > o.number){
					return 1;
				}
			}
			return 0;
		}
		
	}
}


 

질문은 댓글에 남겨주세요~

반응형

댓글