티스토리 뷰

Algorithm

[백준] G5 2116 주사위 쌓기 (java)

코딩브론즈 2021. 1. 17. 23:58

www.acmicpc.net/problem/2116

 

2116번: 주사위 쌓기

첫줄에는 주사위의 개수가 입력된다. 그 다음 줄부터는 한 줄에 하나씩 주사위의 종류가 1번 주사위부터 주사위 번호 순서대로 입력된다. 주사위의 종류는 각 면에 적혀진 숫자가 그림1에 있는

www.acmicpc.net

 

풀이

 

1) 2차원 배열에 주사위 정보 저장 dice[N][6];

2) 경우의 수는 총 6가지이다. 첫번째 주사위의 각 면을 바닥으로 했을 경우를 다 따져보자.

3) A-F, B-D, C-E 끼리 마주보는것을 잘 인지해야한다.

4) 예제의 경우 첫 번째 주사위의 A가 밑면일 경우 F가 윗면. B, C, D, E 중에 최댓값을 구하여 더해준다.

4-1) 첫 번째 주사위의 윗면은 F 이므로 두 번째 주사위의 밑면은 D, 윗면은 B. A, C, E, F 중 최댓값을 더해준다.

4-2) N 번째 주사위 까지 반복하여 다 더한값을 저장한다.

5) 4번 과정을 B, C, D, E, F 면이 밑면일 경우도 구하여 최댓값을 갱신한다.

6) 출력

 

주의사항

 

1) 공간지각능력

 

package com.baekJoon;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.StringTokenizer;


public class BJ_G5_2116_주사위쌓기 {
	static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
	static StringTokenizer tokens;
	static int N, dice[][];
	public static void main(String[] args) throws NumberFormatException, IOException {
		input = new BufferedReader(new StringReader(src));
		N = Integer.parseInt(input.readLine());
		dice = new int[N][6];
		for(int r=0; r<N; r++) {
			tokens = new StringTokenizer(input.readLine());
			for(int c=0; c<6; c++) {
				dice[r][c] = Integer.parseInt(tokens.nextToken());
			}	
		}

		int max = 0;
		for(int t=0; t<6; t++) {
			int floor = dice[0][t];
			int ceiling = 0;
			int fIndex = t;
			int cIndex = 0;
			int sum = 0;
			if(fIndex == 0) {
				ceiling = dice[0][5];
				cIndex = 5;
			}else if (fIndex == 1) {
				ceiling = dice[0][3];
				cIndex = 3;
			}else if (fIndex == 2) {
				ceiling = dice[0][4];
				cIndex = 4;
			}else if (fIndex == 3) {
				ceiling = dice[0][1];
				cIndex = 1;
			}else if (fIndex == 4) {
				ceiling = dice[0][2];
				cIndex = 2;
			}else if (fIndex == 5) {
				ceiling = dice[0][0];
				cIndex = 0;
			}
			for(int c=0; c<6; c++) {
				if(c != fIndex && c!= cIndex) {
					if(dice[0][c] > sum) {
						sum = dice[0][c];
					}
				}
			}
			floor = ceiling;
			for(int r=1; r<N; r++) {
				for(int c=0; c<6; c++) {
					if(dice[r][c]==floor) {
						fIndex = c;
						break;
					}
				}
				if(fIndex == 0) {
					ceiling = dice[r][5];
					cIndex = 5;
				}else if (fIndex == 1) {
					ceiling = dice[r][3];
					cIndex = 3;
				}else if (fIndex == 2) {
					ceiling = dice[r][4];
					cIndex = 4;
				}else if (fIndex == 3) {
					ceiling = dice[r][1];
					cIndex = 1;
				}else if (fIndex == 4) {
					ceiling = dice[r][2];
					cIndex = 2;
				}else if (fIndex == 5) {
					ceiling = dice[r][0];
					cIndex = 0;
				}
				int val = 0;
				for(int c=0; c<6; c++) {
					if(c != fIndex && c!= cIndex) {
						if(dice[r][c] > val) {
							val = dice[r][c];
						}
					}
				}
				sum += val;
				floor = ceiling;
			}
			if(max < sum) {
				max = sum;
			}
		}
		System.out.println(max);
	}

	static String src =
			"5\r\n" + 
			"2 3 1 6 5 4\r\n" + 
			"3 1 2 4 6 5\r\n" + 
			"5 6 4 1 3 2\r\n" + 
			"1 3 6 2 4 5\r\n" + 
			"4 1 6 5 2 3";
}

 

 

풀이

 

 처음에는 Dice라는 클래스를 하나 생성해서 하면 더 쉬울것 같아서 해봤지만 더 어려워져서 2차원 배열로 돌아왔다.

실수를 한 부분은 A, B, C 3가지만 구하면 F, D, E는 값이 똑같을 줄 알았는데 생각이 짧았다. 숫자를 기준으로 해야지 알파벳을 기준으로 계산하는 문제가 아니었다.

생각보다 번거로운 문제였지만 두뇌회전에 좋은 문제였다.

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
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
글 보관함