• 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 } 
  • 相关阅读:
    Flash Builder 使用
    解决谷歌地图偏移问题
    南京垃圾处理场分布图-益云地图
    在Oracle Spatial中增加Web Mercator投影坐标系
    学习和使用 Styled Layer Descriptor SLD样式文件
    jmeter安装教程
    Linux常见命令更新中...
    Python并发编程(线程队列,协程,Greenlet,Gevent)
    Python并发编程(线程,Threading模块,守护线程,gil锁,)
    Python并发编程(管道,数据共享,信号量,进程池)
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/7337701.html
Copyright © 2020-2023  润新知