백준 1149

Algorithm|2023. 8. 20. 17:02

예전에 DP를 제일 어려워했던 기억이 있어서 바로 DP 갈겨봤다.

아직 잘 못풀겠다. 그동안 연습을 안했으니 당연히 그대로 풀기 어려운거라 생각한다.

점화식을 찾아내서 그걸 코드로 바꾸는 부분에서 많이 헤멘다.

누군가의 점화식을 보고 풀었다.

그리고 동적 배열로 할당하니 

system.nullreferenceexception: object reference not set to an instance of an object.

라는 오류가 떠서 [][] 이모양 말고 [,] 으로 바꿔서 했다.

 

 

언젠간 잘하게 되겠지.

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

 

1149번: RGB거리

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

www.acmicpc.net

namespace RGB_Street
{
    class b1149
    {
        static void solution(int N, int[,] cost)
        {
            for (int i = 1; i < N; i++)
            {
                cost[i,0] += Math.Min(cost[i - 1,1], cost[i - 1,2]);
                cost[i,1] += Math.Min(cost[i - 1,0], cost[i - 1,2]);
                cost[i,2] += Math.Min(cost[i - 1,0], cost[i - 1,1]);
            }
            int min = Math.Min(cost[N-1,0], cost[N-1,1]);
            min = Math.Min(cost[N-1,2], min);

            Console.WriteLine(min);
        }
        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[] tmp = Console.ReadLine().Split(' ');
                for (int j = 0; j < 3; j++)
                {
                    cost[i,j] = int.Parse(tmp[j]);
                }
            }
            solution(N, cost);
        }
    }
}

'Algorithm' 카테고리의 다른 글

백준 9461  (0) 2023.08.20
백준 11866  (0) 2023.08.19
백준 12789  (0) 2023.08.19
백준 2798  (0) 2023.08.18
백준 2839  (0) 2023.08.18

댓글()