본문 바로가기
알고리즘/알고리즘

[C++]백준 2116번 : 주사위 쌓기

by 두둠칫 2020. 11. 29.

www.acmicpc.net/problem/2116

 

2116번: 주사위 쌓기

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

www.acmicpc.net

1. 접근&풀이

첫번째 주사위의 밑면(혹은 윗면)에 따라 쌓이는 다음 주사위의 밑면(혹은 윗면)이 결정되는 것을 이용한다.

 

입력은 주사위의 특성을 바로 활용할 수 있도록 주어지지 않으므로 특성을 이용할 수 있도록 입력받는 것이 포인트인 것 같다.

(i면의 반대면은 (i+3)%6)

#include <iostream>
#include <algorithm>

using namespace std;

int N, d[10000][6], ans = 0;


int main(){

	ios::sync_with_stdio(0);
	cin.tie(0);

	cin >> N;
	for (int i = 0; i < N; i++){
		cin >> d[i][0] >> d[i][1] >> d[i][2] >> d[i][4] >> d[i][5] >> d[i][3];
	}

	for (int i = 0; i < 6; i++){ // first bottom side
		int bot = i, cnt = 0, res = 0;

		while (cnt < N){
			// add maximum value except bottom side & upper side
			int val = 0;
			for (int j = 0; j < 6; j++){
				if ((j != bot && j != (bot + 3) % 6) && val < d[cnt][j])
					val = d[cnt][j];
			}
			res += val;

			// find next dice's bottom
			if (cnt < N - 1){
				int upp = d[cnt][(bot + 3) % 6];

				for (int j = 0; j < 6; j++){
					if (upp == d[cnt + 1][j]){
						bot = j;
						break;
					}
				}
			}
			cnt++;
		}

		ans = max(ans, res);
	}

	cout << ans;

	return 0;
}