lightOJ 1047 Neighbor House (DP)
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)的更多相关文章
- LightOJ 1033 Generating Palindromes(dp)
LightOJ 1033 Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
- UVA11125 - Arrange Some Marbles(dp)
UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...
- 【POJ 3071】 Football(DP)
[POJ 3071] Football(DP) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4350 Accepted ...
- 初探动态规划(DP)
学习qzz的命名,来写一篇关于动态规划(dp)的入门博客. 动态规划应该算是一个入门oier的坑,动态规划的抽象即神奇之处,让很多萌新 萌比. 写这篇博客的目标,就是想要用一些容易理解的方式,讲解入门 ...
- Tour(dp)
Tour(dp) 给定平面上n(n<=1000)个点的坐标(按照x递增的顺序),各点x坐标不同,且均为正整数.请设计一条路线,从最左边的点出发,走到最右边的点后再返回,要求除了最左点和最右点之外 ...
- 2017百度之星资格赛 1003:度度熊与邪恶大魔王(DP)
.navbar-nav > li.active > a { background-image: none; background-color: #058; } .navbar-invers ...
- Leetcode之动态规划(DP)专题-详解983. 最低票价(Minimum Cost For Tickets)
Leetcode之动态规划(DP)专题-983. 最低票价(Minimum Cost For Tickets) 在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的 ...
- 最长公共子序列长度(dp)
/// 求两个字符串的最大公共子序列长度,最长公共子序列则并不要求连续,但要求前后顺序(dp) #include <bits/stdc++.h> using namespace std; ...
- Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings)
Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings) 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串. 具有不同开始位置或结束位置的子 ...
随机推荐
- ODI中的CDC
ODI中的CDC是通过一组所谓的日志知识模块(Journal Knowledge Module,JKM)实现的,在项目中加在了这些模块后,就可以在接口设计时选择全量数据,还是变化数据. ODI共提 ...
- 关注SSO
https://wiki.jasig.org/display/CASC/Configuring+the+Jasig+CAS+Client+for+Java+in+the+web.xml 其余的看osc ...
- 三种尺寸:手机SIM卡使用指南
毫无疑问目前卖的最火的手机非iPhone 5s莫属,相信仍有不少网友目前处于观望之中,由于iPhone 5s和iPhone 5c采用与iPhone相同的Nano-SIM卡,因此不少新用户在使用之前也徒 ...
- QCombobox设置下拉框的宽度
这几天写一个项目,里面用到qcombobox组件,其中下拉框含有129个子项,所以在点击的时候,一个下拉框就将整个电脑屏幕给占满了,很不好看并且在使用中会造成很大的苦恼.其实我就是想设置一个下拉框最大 ...
- HDU 5769 Substring(后缀数组)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5769 [题目大意] 在一个串中求出包含字母的子串个数, 只要存在一个字符不相等的子串即可视为不同的 ...
- [置顶] js模板方法的思路及实现
在js中如何实现设计模式中的模板方法? 思路的产生必然要求熟悉js,如何实现?就很简单了,都知道在js中如果定义两个相同名称的方法,前一个方法就会被后一个方法覆盖掉,使用此特点就可以实现模板方法. 例 ...
- SQL Server索引进阶:第一级,索引简介
这个并不是我翻译的,全文共有15篇,但我发现好多网站已经不全,所以自己整理. 原文地址: Stairway to SQL Server Indexes: Level 1, Introduction t ...
- 利用Apperance协议定义View的全局外观
假设要定义一个全局的bkColor用于背景颜色 1.@property(nonatomic,strong)UIColor *bkColor UI_APPEARANCE_SELECTOR; 2.在下面方 ...
- Hibernate - cascade-and -session_state
cascade属性: 默认为none,代表不级联. 级联是指操作主对象时,对关联的对象也做相同的操作. 可设为:delete, save-update, all, none ...-- 一般是对象只归 ...
- 几个linux 下C/C++集成开发环境推荐
链接地址:http://www.lupaworld.com/article-210675-1.html 摘要: 一.AnjutaAnjuta是一个多语言的IDE,它最大的特色是灵活,同时打开多个文件, ...