땅지원
땅지원's Personal blog
땅지원
전체 방문자
오늘
어제
  • 전체 (353)
    • Frontend (2)
      • React (2)
    • Backend (90)
      • Java (16)
      • Python (19)
      • Spring (23)
      • Database (21)
      • Troubleshooting (8)
    • DevOps (27)
      • ELK (13)
    • CS (40)
    • OS (2)
      • Linux (2)
    • Algorithm (95)
      • concept (18)
      • Algorithm Problem (77)
    • 인공지능 (25)
      • 인공지능 (12)
      • 연구노트 (13)
    • 수업정리 (35)
      • 임베디드 시스템 (10)
      • 데이터통신 (17)
      • Linux (8)
    • 한국정보통신학회 (5)
      • 학술대회 (4)
      • 논문지 (1)
    • 수상기록 (8)
      • 수상기록 (6)
      • 특허 (2)
    • 삼성 청년 SW 아카데미 (6)
    • 42seoul (12)
    • Toy project (3)
    • 땅's 낙서장 (2)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

  • 20.11.6 BB21플러스 온라인 학술대회
  • 20.10.30 한국정보통신학회 온라인 학술대회

인기 글

태그

  • E
  • D
  • I
  • 이것이 리눅스다 with Rocky Linux9
  • ㅗ

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
땅지원

땅지원's Personal blog

Algorithm/Algorithm Problem

SWEA 1952 수영장(DFS, DP)

2022. 9. 28. 16:17
 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

수영장을 어떠한 기준으로 끊어야할지 모르기 때문에 모든 경우를 봐줘야한다.

 

DFS의 구조를 트리로 그리면 이해가 쉬워진다.

 

기간은 1년이기 때문에 1년에 해당하는 부분은 상수(final)일 것이고

1일, 1개월,  3개월 단위로 어떤 순서로 할지 모든 경우를 탐색한 다음 비용을 갱신해주면서 답을 찾는다

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Solution{
	static int[] price, data;
	static int res;
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int T = Integer.parseInt(br.readLine());
		
		for (int i = 1; i < T+1; i++) {
			price = new int[4];
			data = new int[13];
			
			String[] input = br.readLine().split(" ");
			for (int j = 0; j < 4; j++) {
				price[j] = Integer.parseInt(input[j]);
			}
			
			input = br.readLine().split(" ");
			for (int j = 1; j < 13; j++) {
				data[j] = Integer.parseInt(input[j-1]);
			}
			res = price[3];

			
			dfs(1,0);
			
			System.out.println("#"+i+" "+res);
			
			
			
		}
				
	}
	
	public static void dfs(int depth, int sum) {
		if (depth >= 13) {
			res = Math.min(res, sum);
			return;
		}
		
		if (data[depth] == 0)
			dfs(depth+1,sum);
		else {
			dfs(depth+1, sum + (data[depth] * price[0]));
			dfs(depth+1, sum + price[1]);
			dfs(depth+3, sum + price[2]);
		}
		
		
		
	}
}

 

DP로 푼 풀이

더보기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Solution {
	static int[] price, data, dp;
	static int res;
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int T = Integer.parseInt(br.readLine());
		
		for (int i = 1; i < T+1; i++) {
			price = new int[4];
			data = new int[13];
			dp = new int[13];

			String[] input = br.readLine().split(" ");
			for (int j = 0; j < 4; j++) {
				price[j] = Integer.parseInt(input[j]);
			}
			
			input = br.readLine().split(" ");
			for (int j = 1; j < 13; j++) {
				data[j] = Integer.parseInt(input[j-1]);
			}
			res = price[3];
			
			
			
			for (int j = 1; j < 13; j++) {
				int day = data[j] * price[0] + dp[j-1];
				int month_1 = price[1] + dp[j-1];
				dp[j] = Math.min(day, month_1);
				
				if (j>=3) {
					int month_3 = price[2] + dp[j-3];
					dp[j] = Math.min(dp[j], month_3);
				}
			}
			
			System.out.println("#"+i+" "+Math.min(dp[12], price[3]));
			
			
			
		}
				
	}
	

}

 

'Algorithm > Algorithm Problem' 카테고리의 다른 글

백준 17143 낚시왕 (구현, 시뮬레이션)  (1) 2022.10.05
SWEA 1953 탈주범 검거(BFS, 구현)  (1) 2022.09.30
백준 19238 스타트 택시(BFS, 구현)  (0) 2022.09.20
백준 2623 음악프로그램(위상정렬, DFS, BFS)  (0) 2022.09.16
백준 3967 매직 스타(DFS)  (0) 2022.09.16
    'Algorithm/Algorithm Problem' 카테고리의 다른 글
    • 백준 17143 낚시왕 (구현, 시뮬레이션)
    • SWEA 1953 탈주범 검거(BFS, 구현)
    • 백준 19238 스타트 택시(BFS, 구현)
    • 백준 2623 음악프로그램(위상정렬, DFS, BFS)
    땅지원
    땅지원
    신입 개발자의 우당탕탕 기술 블로그

    티스토리툴바