담배팔이소년

[BOJ] 02116 주사위 쌓기 본문

Algorithm

[BOJ] 02116 주사위 쌓기

bkkmw 2023. 9. 6. 23:52

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

 

2116번: 주사위 쌓기

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

www.acmicpc.net

 

문제 풀이

 

package boj;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class MainB_02116 {
	
	public static int[] opposite = new int[] {
			5, 3, 4, 1, 2, 0
	};

	public static void main(String[] args) throws Exception {
		System.setIn(new FileInputStream("input/boj/input_02116.txt"));
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		
		int N = Integer.parseInt(br.readLine());
		
		int[][] dices = new int[N][6];
		
		for(int i=0; i<N; i++) {
			st = new StringTokenizer(br.readLine(), " ");
			for(int j=0; j<6; j++) {
				dices[i][j] = Integer.parseInt(st.nextToken());
			}
		}
		
		int ans = solve(dices);
		System.out.println(ans);
	}
	
	public static int solve(int[][] dices) {
		int ret = 0;
		int N = dices.length;
		
		for(int j=1; j<7; j++) {
			int[] res = choose_max(dices[0], j);
			int score = res[0];
			for(int i=1; i<N; i++) {
				res = choose_max(dices[i], res[1]);
				score += res[0];
			}
			
			if(ret < score)
				ret = score;
		}
		
		
		return ret;
	}
	
	public static int[] choose_max(int[] dice, int bot) {
		int ret = 6;
		int top = 0;
		for(int i=0; i<6; i++) {
			if(dice[i] == bot) {
				top = dice[opposite[i]];
				break;
			}
		}
		
		if(top == 6 || bot == 6) {
			if(top ==5 || bot == 5)
				ret = 4;
			else
				ret = 5;
		}
		
		return new int[] {ret, top};
	}
}

'Algorithm' 카테고리의 다른 글

[BOJ] 01167 트리의 지름  (1) 2023.10.18
[BOJ] 01520 내리막 길  (0) 2023.09.06
[SWEA] 3238 이항계수 구하기  (0) 2023.07.31
[BOJ] 06198 옥상 정원 꾸미기  (0) 2023.07.19
[BOJ] 13459 구슬 탈출  (1) 2023.07.12
Comments