zoukankan      html  css  js  c++  java
  • Neighbor House LightOJ

    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

    与数字三角形很相似的题,数据量也小。

     1 #define INF 1e8
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<iostream>
     5 #include<algorithm>
     6 using namespace std;
     7 
     8 int n;
     9 int map[25][4],dp[25][4];
    10 
    11 int solve(){
    12     dp[1][1]=map[1][1],dp[1][2]=map[1][2],dp[1][3]=map[1][3];
    13     for(int i=2;i<=n;i++){
    14         dp[i][1]=min(dp[i-1][2],dp[i-1][3])+map[i][1];
    15         dp[i][2]=min(dp[i-1][1],dp[i-1][3])+map[i][2];
    16         dp[i][3]=min(dp[i-1][1],dp[i-1][2])+map[i][3];
    17     }
    18     int ans=INF;
    19     for(int i=1;i<=3;i++) ans=min(ans,dp[n][i]);
    20     return ans; 
    21 }
    22 
    23 
    24 int main()
    25 {   int kase;
    26     cin>>kase;
    27     for(int t=1;t<=kase;t++){
    28         cin>>n;
    29         for(int i=1;i<=n;i++) for(int j=1;j<=3;j++) scanf("%d",&map[i][j]);    
    30         printf("Case %d: %d
    ",t,solve());
    31     }
    32     return 0;
    33 } 
  • 相关阅读:
    【Android】Camera 使用浅析
    【Android】Camera 使用浅析
    每日学习总结<二> 2015-9-1
    每日学习总结<一> 2015-8-31
    【原创】利用typeface实现不同字体的调用显示及String转换为Unicode
    Android 软件开发之如何使用Eclipse Debug调试程序详解及Eclipse常用快捷键(转)
    Flask学习之 会话控制
    Vue组件介绍及开发
    Flask学习之 会话控制
    Flask学习之 请求与响应
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/7337701.html
Copyright © 2011-2022 走看看