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. 关于GitHub账号及文章选题

    课程:软件测试基础 姓名:胡东妮 学号:2014218028 github账号:hudongni1 文章选题:测试用例的自动生成  邮箱:dongnihu@tju.edu.cn

  2. python练习之list

    请用索引取出下面list的指定元素: # -*- coding: utf-8 -*- L = [ ['Apple', 'Google', 'Microsoft'], ['Java', 'Python' ...

  3. IOS 特定于设备的开发:处理基本方向

    UIDevice类使用内置的orientation属性获取设备的物理方向.IOS设备支持这个属性的7个可能的值. >UIDeviceOrientationUnknown:方向目前未知. > ...

  4. querySelector $() getElementBy区别

    参考 http://stackoverflow.com/questions/14377590/queryselector-and-queryselectorall-vs-getelementsbycl ...

  5. HTML5速查表

    HTML5速查表 标签 描述 版本 属性 <!--...--> 定义注释 4 / 5 none <!DOCTYPE> 定义文档类型 4 / 5 none <a> 定 ...

  6. JSP与Servlet的中文乱码处理

    注:百度来的,改了改... jsp页面的的头要设置好 <%@ page language="java" contentType="text/html; charse ...

  7. 领域驱动设计系列(2)浅析VO、DTO、DO、PO的概念、区别和用处

    PO:persistant object持久对象 最形象的理解就是一个PO就是数据库中的一条记录.好处是可以把一条记录作为一个对象处理,可以方便的转为其它对象. BO:business object业 ...

  8. 脑波设备mindwaveTGC接口示例

    TGC是一个后台应用程序,它负责和脑波设备建立连接,并获取数据,另一方面,它打开了一个端口在监听,让二次开发的应用程序,可以通过socket连接到这个TGC后台程序,获取脑波数据并展示,这种接口适合非 ...

  9. html网页编码问题

    之前碰到过一些html编码乱码问题,都理解的模模糊糊,问了别人解释的也是模模糊糊.近期要做前端这个问题研究了下仅仅须要两句话就能非常清楚的解释了(之前问的那些人是不是自己都没理解非常郁闷.) < ...

  10. JavaScript之字符串引号的使用技巧

    在JavaScript中可以随意使用引号,但是最好根据字符串包含的字符来选择. 1.如果字符串里面包含了单引号,那就把字符串放在双引号里面 var age = "this is 'pig'? ...