LightOJ 1047 Neighbor House (DP 数字三角形变形)

1047 - Neighbor House

PDF (English) Statistics Forum
Time Limit: 0.5 second(s) Memory Limit: 32 MB

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

You will be given the information of houses. Each house willcontain three integers"R G B" (quotes for clarity only),where R, G and B are the costs of painting the correspondinghouse red, green, and blue, respectively.
Return the minimal total costrequired 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 integern (1≤ n ≤ 20) denoting the number of houses. Each of the next nlines will contain 3 integers "R G B". These integers will liein the range[1, 1000].

Output

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

Sample Input

Output for 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


Case 1: 137

Case 2: 96

题目链接:http://lightoj.com/volume_showproblem.php?problem=1047

题目大意:相邻行不能取同一列的数字,从上往下路径最大权值和问题。

解题思路:只有三列,每步有两次选择,dp[i][j]表示到第a[i][j]步的最小数字累加和,起点处分三条路,最后选出和最小的一条路。

代码如下:

#include <cstdio>
#include <cstring>
int a[25][4],dp[25][4];
int min2(int a,int b)
{
    return a>b?b:a;
}
int min3(int a,int b,int c)
{
    if(a<=b&&a<=c)
        return a;
    if(b<=a&&b<=c)
        return b;
    if(c<=b&&c<=a)
        return c;
}
int main()
{
    int t,n,i,j,cnt=0;
    scanf("%d",&t);
    while(t--)
    {
        int ans;
        memset(dp,0,sizeof(dp));
        memset(a,0,sizeof(a));
        scanf("%d",&n);
        for(i=0;i<n;i++)
            for(j=0;j<3;j++)
                scanf("%d",&a[i][j]);
        dp[0][0]=a[0][0];
        dp[0][1]=a[0][1];
        dp[0][2]=a[0][2];
        for(i=1;i<n;i++)
        {
            dp[i][0]=a[i][0]+min2(dp[i-1][2],dp[i-1][1]);
            dp[i][1]=a[i][1]+min2(dp[i-1][2],dp[i-1][0]);
            dp[i][2]=a[i][2]+min2(dp[i-1][0],dp[i-1][1]);
        }
        ans=min3(dp[n-1][0],dp[n-1][1],dp[n-1][2]);
        printf("Case %d: %d\n",++cnt,ans);
    }
    return 0;
}
时间: 2024-07-29 04:03:12

LightOJ 1047 Neighbor House (DP 数字三角形变形)的相关文章

lightOJ 1047 Neighbor House (DP)

题目链接:lightOJ 1047 Neighbor House 题意:有N做房子,每个房子涂3种颜色各有一个花费,相邻的房子颜色不能一样,给N个房子涂颜色,问完成这个任务的最小花费. dp[i][j] 表示涂到第i个房子涂j颜色的最小花费. 状态转移方程:dp[i][k]=min(dp[i][k],dp[i-1][j]+p[i].c[k]); AC代码: #include <stdio.h> #include <string.h> #include <algorithm&g

4829 [DP]数字三角形升级版

4829 [DP]数字三角形升级版 时间限制: 1 s 空间限制: 16000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 从数字三角形的顶部(如图,第一行的5表示行数)到底部有很多条不同的路径.对于每条路径,把路径上面的数加起来可以得到一个和,且!!!!!!!!! ================================================================================== =================

LightOJ 1004 Monkey Banana Problem (DP 数字三角形)

1004 - Monkey Banana Problem PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB You are in the world of mathematics to solve the great "MonkeyBanana Problem". It states that, a monkey enters into a diamond shaped twodimen

LightOJ 1047 - Neighbor House 【DP】

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1047 题意:求(p[i][j])上下相邻的 j 不能相同的数塔的最小和. 解法:看代码! 代码: #include <stdio.h> #include <iostream> #include <string.h> #include <algorithm> #include <bitset> #include <math

HDU1176:免费馅饼(dp,数字三角形的应用)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1176 这题就是数字三角行的变形,可惜对于我这个渣渣来说就是没发现,区别是他可以保持在三个点,他左边的点,右边的点,还有原点, 从下往上处理.其他就没有什么好说的了,注意一下细节问题,我记得这题我白白贡献了几次WA. #include <iostream> #include <stdio.h> #include <string.h> #include <math.h>

HDU 1176(类似数字三角形的题,很经典,值得仔细理解的dp思维)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1176 免费馅饼 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 60927    Accepted Submission(s): 21380 Problem Description 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径

数字三角形 17 初入dp

Description 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 给出了一个数字三角形.从三角形的顶部到底部有很多条不同的路径.对于每条路径,把路径上面的数加起来可以得到一个和,你的任务就是找到最大的和. 注意:路径上的每一步只能从一个数走到下一层上和它最近的左边的那个数或者右边的那个数. Input 输入数据有多组,每组输入的是一行是一个整数N (1 < N <= 100),给出三角形的行数.下面的N行给出数字三角形.数字三角形上的数的范围都在0和100之间. Output

hihoCoder#1037 : 数字三角形(DP)

[题目链接]:click here~~ 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 问题描写叙述 小Hi和小Ho在经历了螃蟹先生的任务之后被奖励了一次出国旅游的机会,于是他们来到了大洋彼岸的美国.美国人民的生活很有意思,常常会有形形色色.奇奇怪怪的活动举办.这不,小Hi和小Ho刚刚下飞机,就赶上了当地的迷宫节活动. 迷宫节里展览出来的迷宫都特别的有意思.可是小Ho却相中了一个事实上并不怎么像迷宫的迷宫--由于这个迷宫的奖励很丰富~ 于是小Ho找到了小Hi,让小Hi帮助

算法训练 数字三角形(DP)

问题描述 (图3.1-1)示出了一个数字三角形. 请编一个程序计算从顶至底的某处的一条路 径,使该路径所经过的数字的总和最大. ●每一步可沿左斜线向下或右斜线向下走: ●1<三角形行数≤100: ●三角形中的数字为整数0,1,…99: . (图3.1-1) 输入格式 文件中首先读到的是三角形的行数. 接下来描述整个三角形 输出格式 最大总和(整数) 样例输入 573 88 1 02 7 4 44 5 2 6 5 样例输出 30 从下往上进行计算就好了,还可以用DP的方法来解 #include<