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];

代码:

 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring> //memset函数头文件
using namespace std; int p[][];
int dp[][]; //第i户人家涂第j种颜色的最小花费 int min(int a,int b)//输出最小值
{
return a>b?b:a;
} int main()
{
int t;
int n;
int m=;
scanf("%d",&t);//t组案例
while(t--)
{
memset(dp,,sizeof(dp));
scanf("%d",&n); //n户人家
for(int i=;i<=n;i++)
for(int j=;j<=;j++)
scanf("%d",&p[i][j]);//第i户人家涂第j种颜色的花费
for(int i=;i<=n;i++)
for(int j=;j<=;j++)
{
if (j == )//涂第一种颜色
dp[i][j] = min(dp[i - ][], dp[i - ][]) + p[i][j];
if (j == )//涂第二种颜色
dp[i][j] = min(dp[i - ][], dp[i - ][]) + p[i][j];
if (j == )//涂第三种颜色
dp[i][j] = min(dp[i - ][], dp[i - ][]) + p[i][j];
}
printf("Case %d: %d\n",m++,min(dp[n][],min(dp[n][],dp[n][])));
}
return ;
}

lightOJ 1047 Neighbor House (DP)的更多相关文章

  1. LightOJ 1033 Generating Palindromes(dp)

    LightOJ 1033  Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...

  2. UVA11125 - Arrange Some Marbles(dp)

    UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...

  3. 【POJ 3071】 Football(DP)

    [POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted ...

  4. 初探动态规划(DP)

    学习qzz的命名,来写一篇关于动态规划(dp)的入门博客. 动态规划应该算是一个入门oier的坑,动态规划的抽象即神奇之处,让很多萌新 萌比. 写这篇博客的目标,就是想要用一些容易理解的方式,讲解入门 ...

  5. Tour(dp)

    Tour(dp) 给定平面上n(n<=1000)个点的坐标(按照x递增的顺序),各点x坐标不同,且均为正整数.请设计一条路线,从最左边的点出发,走到最右边的点后再返回,要求除了最左点和最右点之外 ...

  6. 2017百度之星资格赛 1003:度度熊与邪恶大魔王(DP)

    .navbar-nav > li.active > a { background-image: none; background-color: #058; } .navbar-invers ...

  7. Leetcode之动态规划(DP)专题-详解983. 最低票价(Minimum Cost For Tickets)

    Leetcode之动态规划(DP)专题-983. 最低票价(Minimum Cost For Tickets) 在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的 ...

  8. 最长公共子序列长度(dp)

    /// 求两个字符串的最大公共子序列长度,最长公共子序列则并不要求连续,但要求前后顺序(dp) #include <bits/stdc++.h> using namespace std; ...

  9. Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings)

    Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings) 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串. 具有不同开始位置或结束位置的子 ...

随机推荐

  1. H3C交换配置PBR最佳实践

    简要说明 PBR算是比较常用的功能,需要我们去掌握一下 配置步骤简要 配置BFD 配置track 配置acl 配置policy-based-route 在接口上面应用policy-based-rout ...

  2. BOOL、sizeof

    BOOL使用前需要声明 #include <stdbool.h>(这个头文件定义了bool,true,false等宏) int a[5]; sizeof(a[5]),sizeof是关键字, ...

  3. 配置Raspbian 启用SPI I2C

    sudo vi /etc/modprobe.d/raspi-blacklist.conf 找到这两行 blacklist spi-bcm2708 blacklist i2c-bcm2708 注释掉 # ...

  4. 苹果iPhone不能判断红外发射管的好坏

    用手机来检测红外发射管好坏是目前比较常用的方法.实际操作比较简单,就是按照红外发射管的工作电压给发射管接上电源后,把手机的摄像头对着红外发射管就能看出好坏了.由于红外线是肉眼看不见的,如果不通过手机摄 ...

  5. GC(Garbagecollection)垃圾回收

    在前面向大家讲解FTL时,我们提到了GC的操作,所谓GC就是把一个闪存块里的‘有效’页数据复制到一个‘空白’块里,然后把这个块完全擦除.GC是SSD里的一个非常关键的操作,其效率对性能有决定性影响.闪 ...

  6. 用auto_ptr类模板帮助动态内存管理

    动态内存使用最多的是在C++应用程序的代码中.有过编程经验的程序员虽然都知道new操作符的使用一定要与delete匹配,在某些场合仍然可能有内存溢出.当异常被掷出时,程序的正常控制流程被改变,因此导致 ...

  7. 【Perl学习笔记】2. perl中的bless理解

    bless有两个参数:对象的引用.类的名称. 类的名称是一个字符串,代表了类的类型信息,这是理解bless的关键. 所谓bless就是把 类型信息 赋予 实例变量. 程序包括5个文件:person.p ...

  8. VS2010/MFC对话框:一般属性页对话框的创建及显示

    一般属性页对话框的创建及显示 本节将介绍一般属性页对话框的创建和显示. 实际上,一般属性页对话框的创建和显示过程和向导对话框是很类似的.鸡啄米将上一节中的向导对话框进行少量修改,使其成为一般属性页对话 ...

  9. HDU3415:Max Sum of Max-K-sub-sequence(单调队列)

    Problem Description Given a circle sequence A[1],A[2],A[3]......A[n]. Circle sequence means the left ...

  10. linux 知识整理1linux 常见的目录

    linux 系统现在也是搭配啦图形操作界面. 本人初次学习linux,不是为工作,我的工作是玩Asp.net 的.学习linux 也算是知识的储备吧. 学习linux必须知道一些基本的知识. 目录 用 ...