LightOJ 1047-Program 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 Bare 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
题目大意:给n座房子刷漆,每座房子可以刷红,绿,蓝色中的任何一种,粉刷每一种颜色都要花费一定的money,但是相邻两座房子的颜色不能相同,
要求输出最少的花费。用i表示1~n座房子,用j表示三种颜色的花费。
分析:先输入1~n的房子中每种颜色所需的花费p[i][j],接着从1~n先比较第一座房子中所需花费最少的并标记其颜色,这其中有三种情况,j=1,2,3,
分别表示第一,二,三种颜色,用一个二维数组dp[i][j]=min(dp[i][j-2],dp[i][j-3])+p[i][j](当j=1时),然后比较第二座房子所需的花费最少,判断其颜色是否与第一座相
同,不同则直接累加,否则比较出第二少的,再累加。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
int n;
int dp[25][5],p[25][5];
int main()
{
int t,i,j,kase=0;
scanf("%d",&t);
while(t--)
{
memset(dp,0,sizeof(dp));
scanf("%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=3;j++)
scanf("%d",&p[i][j]);
for(i=1;i<=n;i++)
for(j=1;j<=3;j++)
{
if(j==1)
dp[i][j]=min(dp[i-1][2],dp[i-1][3])+p[i][j];
if(j==2)
dp[i][j]=min(dp[i-1][1],dp[i-1][3])+p[i][j];
if(j==3)
dp[i][j]=min(dp[i-1][1],dp[i-1][2])+p[i][j];
}
printf("Case %d: %d\n",++kase,min(dp[n][1],min(dp[n][2],dp[n][3])));
}
return 0;
}
LightOJ 1047-Program C的更多相关文章
- lightOJ 1047 Neighbor House (DP)
lightOJ 1047 Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...
- Neighbor House LightOJ - 1047
Neighbor House LightOJ - 1047 #include<cstdio> #include<cstring> #include<algorithm&g ...
- LightOJ 1141 Program E
Description In this problem, you are given an integer number s. You can transform any integer number ...
- 周赛C题 LightOJ 1047 (DP)
C - C Time Limit:500MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Description Th ...
- hdu 1047 Integer Inquiry
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1047 Integer Inquiry Description One of the first use ...
- 解决方案--java执行cmd命令ProcessBuilder--出错Exception in thread "main" java.io.IOException: Cannot run program "dir d:\": CreateProcess error=2(xjl456852原创)
当我尝试在java中通过ProcessBuilder运行window的cmd命令时出现错误: public static void main(String [] args) throws IOExce ...
- LightOj:1030-Discovering Gold(期望dp模板)
传送门:http://www.lightoj.com/volume_showproblem.php?problem=1030 Discovering Gold Time Limit: 2 second ...
- [project euler] program 4
上一次接触 project euler 还是2011年的事情,做了前三道题,后来被第四题卡住了,前面几题的代码也没有保留下来. 今天试着暴力破解了一下,代码如下: (我大概是第 172,719 个解出 ...
- Solved: “Cannot execute a program. The command being executed was \roslyn\csc.exe”
When you publish your ASP.NET project to a hosting account such as GoDaddy, you may run into the iss ...
- BZOJ 1047 二维单调队列
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1047 题意:见中文题面 思路:该题是求二维的子矩阵的最大值与最小值的差值尽量小.所以可以考 ...
随机推荐
- [js] 非常好的面试题,学习了。。
原文链接:http://www.cnblogs.com/xxcanghai/p/5189353.html
- oracle中修改表名
<<<备忘>>> answer1: ALTER TABLE old_table_name RENAME TO new_table_name;(大写为系统命令) ...
- Webservice接口和Http接口
WebService又是一种高级应用,与之前学习的Struts.Spring.Hibernate等框架不同.WebService是面向服务的架构(SOA),看起来像是比SSH框架要大.那么它到底是做什 ...
- Linux上安装Mysql后除了本机其他机器不能访问的问题(zhuan)
http://blog.sina.com.cn/s/blog_a338027c0101esbs.html http://niutuku.com/tech/Mysql/237638.shtml http ...
- HTML CSS 常用英语单词
head 头部 body 身体 title 标题 table表格 list 列表 i ...
- 每日一笔记之2:QT之坐标系统:
以前一直多单片机开发,也没怎么使用过大的显示器,第一次学习,备忘: QT画图系统. 绘图,通过QPainter类实现. Qt的绘图系统对底层函数进行了良好的封装,使得在屏幕和设备的绘图功能可能使用相同 ...
- 深入理解JVM虚拟机-7虚拟机类加载机制
虚拟机把描述类的数据从Class文件夹加载到内存,并对数据进行小燕.转换解析和初始化,最终形成可以被虚拟机直接使用的java类型,这就是虚拟机的类加载机制. 下面所说的Class文件不是具体的某个文件 ...
- 大S《美容大王》内容80%都是没用的东西
揭20位明星真实人品 我在她大陆的企划公司工作过,见过她N次了,对人态度蛮不错的,而且很爱开玩笑,但感觉很假很抠门.只要一回公司她就黏着请她吃饭.她很主动很霸道,她和周渝民交往,原因之一就是周什么都听 ...
- VB6 GDI+ 入门教程[3] 笔、刷子、矩形、椭圆绘制
http://vistaswx.com/blog/article/category/tutorial/page/2 VB6 GDI+ 入门教程[3] 笔.刷子.矩形.椭圆绘制 2009 年 6 月 1 ...
- 原生js基础问题的一些备忘
1.在原生js里面 window.onload=function(){} 这个就相当于jquery中 $(document).ready(function(){}); 这样 2.getElemen ...