반응형
문제
https://www.acmicpc.net/problem/2116
풀이
문제 이해하기가 살짝 어렵지만 어렵게 생각하지 말고 쉽게 생각하면 다음과 같다.
첫 번째 주사위를 놓는 방법에 따른 최대값 구하기!
처음 주사위를 놓는 방법은 딱 6개가 존재하기 때문에 각각 방법으로 놓아보고,
그 외 주사위들의 상,하를 이전 주사위와 맞춰주어야 한다.
맞춰주고, 상 하 값을 제외한 나머지 값들 중 가장 큰 값을 더해준다.
왜냐하면 주사위 회전이 가능하기 때문이다.
첫번째 주사위를 놓는 방법 (6가지) 중 가장 큰 값을 출력해주면 되는 문제
소스코드
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let dict: [Int: Int] = [0: 5, 1: 3, 2: 4, 3: 1, 4: 2, 5: 0] | |
let n = Int(readLine()!)! | |
var dices: [[Int]] = [] | |
for _ in 0..<n { dices.append(readLine()!.split { $0 == " " }.map { Int($0)! }) } | |
func selectedTop(s: Int) -> Int { | |
var top = dices[0][s] | |
var bottom = dices[0][dict[s]!] | |
var result = dices[0].filter { 0 != top &&0 != bottom }.max()! | |
for i in 1..<n { | |
let bottomIndex = dices[i].firstIndex(of: top)! | |
bottom = dices[i][bottomIndex] | |
top = dices[i][dict[bottomIndex]!] | |
result += dices[i].filter { 0 != bottom &&0 != top }.max()! | |
} | |
return result | |
} | |
var result = 0 | |
for i in 0..<6 { | |
result = max(result, selectedTop(s: i)) | |
} | |
print(result) |
후기
머리가 조금 아팠지만 재밌는 문제였다.
반응형
'TIL > 코테 스터디' 카테고리의 다른 글
99클럽 코테 스터디 27일차 TIL: LIS (0) | 2024.11.23 |
---|---|
99클럽 코테 스터디 26일차 TIL: 게임 (0) | 2024.11.22 |
99클럽 코테 스터디 24일차 TIL: 최대 힙 + 그리디 (0) | 2024.11.20 |
99클럽 코테 스터디 23일차 TIL: 완전탐색 + 백트래킹 (0) | 2024.11.19 |
99클럽 코테 스터디 22일차 TIL: 브루트포스 (1) | 2024.11.18 |