• lightOJ 1047 Neighbor House (DP)


    lightOJ 1047   Neighbor House (DP)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730#problem/C

    题目:

    Description

    The people of Mohammadpur have decided to paint each of their houses red, green, or blue. They've also decided that no two neighboring houses will be painted the same color. The neighbors of house i are houses i-1 and i+1. The first and last houses are not neighbors.

    You will be given the information of houses. Each house will contain three integers "R G B" (quotes for clarity only), where R, G and B are the costs of painting the corresponding house red, green, and blue, respectively. Return the minimal total cost required to perform the work.

    Input

    Input starts with an integer T (≤ 100), denoting the number of test cases.

    Each case begins with a blank line and an integer n (1 ≤ n ≤ 20) denoting the number of houses. Each of the next n lines will contain 3 integers "R G B". These integers will lie in the range [1, 1000].

    Output

    For each case of input you have to print the case number and the minimal cost.

    Sample Input

    2

    4

    13 23 12

    77 36 64

    44 89 76

    31 78 45

    3

    26 40 83

    49 60 57

    13 89 99

    Sample Output

    Case 1: 137

    Case 2: 96

    Hint

    Use simple DP

    题意:

    N个邻居用红、绿、蓝三种颜色中的一种涂房子,相邻的房子颜色不能一样,每种颜色花费不同,求将N个用户全部涂完的总花费最小是什么。

    分析:

    动态规划,和数字三角形类似。先求每户人家涂颜色的最小花费,每次涂下一个房子时不能与上一个房子颜色相同

     状态转移方程:
          dp[i][j] = min(dp[i - 1][2], dp[i - 1][3]) + p[i][j];
          dp[i][j] = min(dp[i - 1][1], dp[i - 1][3]) + p[i][j];
          dp[i][j] = min(dp[i - 1][2], dp[i - 1][1]) + p[i][j];

    代码:

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<cstring>  //memset函数头文件
     5 using namespace std;
     6 
     7 int p[25][5];
     8 int dp[25][25]; //第i户人家涂第j种颜色的最小花费
     9 
    10 int min(int a,int b)//输出最小值
    11 {
    12     return a>b?b:a;
    13 }
    14 
    15 int main()
    16 {
    17     int t;
    18     int n;
    19     int m=1;
    20     scanf("%d",&t);//t组案例
    21     while(t--)
    22     {
    23         memset(dp,0,sizeof(dp));
    24         scanf("%d",&n);  //n户人家
    25         for(int i=1;i<=n;i++)
    26             for(int j=1;j<=3;j++)
    27                 scanf("%d",&p[i][j]);//第i户人家涂第j种颜色的花费
    28             for(int i=1;i<=n;i++)
    29                 for(int j=1;j<=3;j++)
    30                 {
    31                     if (j == 1)//涂第一种颜色
    32                         dp[i][j] = min(dp[i - 1][2], dp[i - 1][3]) + p[i][j];
    33                     if (j == 2)//涂第二种颜色
    34                         dp[i][j] = min(dp[i - 1][1], dp[i - 1][3]) + p[i][j];
    35                     if (j == 3)//涂第三种颜色
    36                         dp[i][j] = min(dp[i - 1][2], dp[i - 1][1]) + p[i][j];
    37                 }
    38                 printf("Case %d: %d
    ",m++,min(dp[n][1],min(dp[n][2],dp[n][3])));
    39     }
    40     return 0;
    41 }
  • 相关阅读:
    Android中使用WebView, WebChromeClient和WebViewClient加载网页
    Android清除本地数据缓存代码案例
    暴雪hash算法
    C++ 指向类的指针
    Qt VS Tools 的Qt Option add 不上qt版本的问题
    Dom4j完整教程详解
    java中charAt()方法的使用
    Linux环境下C++调试的三板斧
    (转载)Markdown进阶(更改字体、颜色、大小,设置文字背景色,调整图片大小设置居中)
    关于回调函数的理解
  • 原文地址:https://www.cnblogs.com/ttmj865/p/4735075.html
Copyright © 2020-2023  润新知