1. 문제
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
2. 접근방법
1월 ~ 12월 까지
일권으로 쓰는 경우 12
1월 ~ 12월 까지
월권으로 쓰는 경우 12
3개월 씩 끊어서
3개월 10개
1년
하나
경우의 수가 매우 적으니
찍어보면 경우의 수 18529개가 뜨는데 정확한 계산식은 다음에 추가 할게요
바로 브루트 포스 ㄱㄱ하면 풀리는 문제이다.
3. 자바 코드
package algo;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class P1952수영장 {
static int cost[] = new int[4];
static int[] year = new int[12];
static int min = 0;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
for (int tc = 1; tc <= T; tc++) {
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 0; i < 4; i++) {
cost[i] = Integer.parseInt(st.nextToken());
}
st = new StringTokenizer(br.readLine());
for (int i = 0; i < 12; i++) {
year[i] = Integer.parseInt(st.nextToken());
}
min = cost[3];
go(0,0);
System.out.println("#"+tc+" "+ min);
}
}
static void go(int mon,int price) {
if(mon>11) {
min = Math.min(min, price);
return;
}
for(int i = 0 ; i < 3 ; i++ ) {
if(i == 0) { //일 고르고 가기
price += year[mon] * cost[i];
go(mon+1,price);
price -= year[mon] * cost[i];
}else if(i == 1) { // 월 고르고 가기
price += cost[i];
go(mon+1,price);
price -= cost[i];
}else if(i == 2) { // 3개월 고르고 가기
price += cost[i];
go(mon+3,price);
price -= cost[i];
}
}
}
}
4. 마치며
dp 그리디 여러 방법이 있다 더 생각해보자
'알고리즘 문제풀이 > SWEA' 카테고리의 다른 글
[SWEA] 1868 파핑파핑 지뢰찾기 (0) | 2020.11.05 |
---|---|
[SWEA] 5643 키순서 (0) | 2020.11.04 |
[SWEA] 1949 등산로조성 (BFS로 풀어보기) (0) | 2020.11.03 |
[SWEA] 5656 벽돌깨기 (0) | 2020.11.03 |
[SWEA] 5644 무선충전 (0) | 2020.11.02 |