Article Image
Article Image
read

문제

1952. [모의 SW 역량테스트] 수영장

풀이

이 문제는 DFS 알고리즘을 이용해서 쉽게 풀 수 있는 문제이다.

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

class Solution
{
    static int[] costs;
    static int[] days;
    static int min;
    
	public static void main(String args[]) throws Exception
	{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int T = Integer.parseInt(br.readLine());

		for(int test_case = 1; test_case <= T; test_case++)
		{
            String[] input = br.readLine().split(" ");
            costs = new int[4];
            min = Integer.MAX_VALUE;
            
            for(int i=0; i<4; i++)
                costs[i] = Integer.parseInt(input[i]);
            
            input = br.readLine().split(" ");
            days = new int[12];
            for(int i=0; i<12; i++)
                days[i] = Integer.parseInt(input[i]);
            
            dfs(0, 0);
            System.out.println("#"+test_case+" "+Math.min(min, costs[3]));
		}
	}
    
    public static void dfs(int month, int cost) {
        if(month>=12) {
            min = Math.min(min, cost);
            return;
        }
        
        if(days[month]==0)
            dfs(month+1, cost);
        
        else {
            for(int i=0; i<3; i++) {
            	if(i==0)
            		dfs(month+1, cost+days[month]*costs[i]);
            	else if(i==1)
                	dfs(month+1, cost+costs[i]);
                
                else
                    dfs(month+3, cost+costs[i]);
        	}
        }
    }
}
Blog Logo

pss407


Published

Image

pss's GitHub 블로그

Welcome my blog!

Back to Overview