SMALL

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

 

1149번: RGB거리

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

www.acmicpc.net


  • 문제


  • 문제풀이

for문을 사용하여 math.min을 구해 최소거리를 구했다.

 


  • 코드 1
using System;

class RGBHouse
{
    static void Main(string[] args)
    {
        int n = int.Parse(Console.ReadLine());

        int[,] cost = new int[n, 3];
        for (int i = 0; i < n; i++)
        {
            string[] input = Console.ReadLine().Split();
            cost[i, 0] = int.Parse(input[0]);
            cost[i, 1] = int.Parse(input[1]);
            cost[i, 2] = int.Parse(input[2]);
        }

        int[,] ans = new int[n, 3];
        ans[0, 0] = cost[0, 0];
        ans[0, 1] = cost[0, 1];
        ans[0, 2] = cost[0, 2];

        for (int i = 1; i < n; i++)
        {
            ans[i, 0] = cost[i, 0] + Math.Min(ans[i - 1, 1], ans[i - 1, 2]);
            ans[i, 1] = cost[i, 1] + Math.Min(ans[i - 1, 0], ans[i - 1, 2]);
            ans[i, 2] = cost[i, 2] + Math.Min(ans[i - 1, 0], ans[i - 1, 1]);
        }

        int minCost = Math.Min(ans[n - 1, 0], Math.Min(ans[n - 1, 1], ans[n - 1, 2]));
        Console.WriteLine(minCost);
    }
}

 


  • 후기

다이나믹 프로그래밍은 항상 접근하기 어렵다. 많이 노력해야겠다.

 

LIST

+ Recent posts