본문 바로가기
C#/백준 알고리즘

2022.02.06 [백준] C# RGB거리

by ian's coding 2022. 2. 6.
728x90
반응형

 

https://www.acmicpc.net/problem/1149

 

1149번: RGB거리

첫째 줄에 집의 수 N(2 ≤ N ≤ 1,000)이 주어진다. 둘째 줄부터 N개의 줄에는 각 집을 빨강, 초록, 파랑으로 칠하는 비용이 1번 집부터 한 줄에 하나씩 주어진다. 집을 칠하는 비용은 1,000보다 작거나

www.acmicpc.net

 


풀이

이 문제는 이전의 집과 같은 색으로 칠하지 않으면서 최소한의 비용으로 칠할 때 총 비용을 구하는 문제이다. 

모든 경우의 수를 따져 가면서 이미 거친 경우를 다시 계산하지 않기 위해 메모이제이션을 이용하여 각 경우의 수마다 배열에 저장하였다.

a가 0일 때, 첫 집을 칠할때의 각 색의 가격이 totalCost가 된다. 그 다음부터 각 컬러의 상황에서 이전 집을 칠할때의 최소 totalCost를 Math.Min으로 뽑아내고 현재 컬러에 대한 비용을 더했다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System;
using System.Text;
using System.IO;
 
class Program
{
    static int[,] colorCost;
    static int[,] totalCost;
    static void func(int a)
    {
        if (a == 0)
        {
            totalCost[00= colorCost[00];
            totalCost[01= colorCost[01];
            totalCost[02= colorCost[02];
        }
        else
        {
            totalCost[a, 0= Math.Min(totalCost[a - 11], totalCost[a - 12]) + colorCost[a, 0];
            totalCost[a, 1= Math.Min(totalCost[a - 10], totalCost[a - 12]) + colorCost[a, 1];
            totalCost[a, 2= Math.Min(totalCost[a - 10], totalCost[a - 11]) + colorCost[a, 2];
        }
    }
    static void Main()
    {
        StreamReader sr = new StreamReader(new BufferedStream(Console.OpenStandardInput()));
        StreamWriter sw = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()));
        StringBuilder sb = new StringBuilder();
        int n = int.Parse(sr.ReadLine());
        colorCost = new int[n, 3];
        totalCost = new int[n, 3];
        for(int i = 0; i < n; i++)
        {
            var num = Array.ConvertAll(sr.ReadLine().Split(), int.Parse);
            colorCost[i, 0= num[0];
            colorCost[i, 1= num[1];
            colorCost[i, 2= num[2];
            func(i);
        }
        sw.WriteLine(Math.Min(totalCost[n-10], Math.Min(totalCost[n-11], totalCost[n-12])));
        sr.Close();
        sw.Close(); 
    } 
}
cs

 

아래 코드는 totalCost없이 colorCost에 바로 이전의 비용과 자신의 비용을 더해주었다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System;
using System.Text;
using System.IO;
 
class Program
{
    static int[,] colorCost;
    static void func(int a)
    {
        
 
        colorCost[a, 0= Math.Min(colorCost[a - 11], colorCost[a - 12]) + colorCost[a, 0];
        colorCost[a, 1= Math.Min(colorCost[a - 10], colorCost[a - 12]) + colorCost[a, 1];
        colorCost[a, 2= Math.Min(colorCost[a - 10], colorCost[a - 11]) + colorCost[a, 2];
        
    }
    static void Main()
    {
        StreamReader sr = new StreamReader(new BufferedStream(Console.OpenStandardInput()));
        StreamWriter sw = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()));
        StringBuilder sb = new StringBuilder();
        int n = int.Parse(sr.ReadLine());
        colorCost = new int[n, 3];
        var num = Array.ConvertAll(sr.ReadLine().Split(), int.Parse);
        colorCost[00= num[0];
        colorCost[01= num[1];
        colorCost[02= num[2];
        for (int i = 1; i < n; i++)
        {
            var num1 = Array.ConvertAll(sr.ReadLine().Split(), int.Parse);
            colorCost[i, 0= num1[0];
            colorCost[i, 1= num1[1];
            colorCost[i, 2= num1[2];
            func(i);
        }
        sw.WriteLine(Math.Min(colorCost[n - 10], Math.Min(colorCost[n - 11], colorCost[n - 12])));
        sr.Close();
        sw.Close();
    }
}
cs

 

 

728x90
반응형

댓글