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 题意:见中文题面 思路:该题是求二维的子矩阵的最大值与最小值的差值尽量小.所以可以考 ...
随机推荐
- JavaMail 发送邮件案例
#----------------这两个是构建session必须的字段---------- #smtp服务器 mail.smtp.host=smtp.exmail.qq.com #身份验证 mail. ...
- Ubuntu 查看文件以及磁盘空间大小管理
(1)查看文件大小 查看当前文件夹下所有文件大小(包括子文件夹) du -sh # du -h15M ./package16K ./.fontconfig4.0K . ...
- jquery获得option的值和对option进行操作 作者: 字体:[增加 减小] 类型:转载 时间:2013-12-13 我要评论
jquery获得option的值和对option进行操作 作者: 字体:[增加 减小] 类型:转载 时间:2013-12-13我要评论 本文为大家介绍下jquery获得option的值和对option ...
- go语言实战向导
版权声明:本文由魏佳原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/173 来源:腾云阁 https://www.qclou ...
- java 多线程7(线程的停止)
notify(): 是很温和的唤醒线程的方法,它不可以指定清除哪一个异常 interrupt(): 粗暴的方式,强制清除线程的等待状态,被清除的线程会接收到一个InterruptedException ...
- struts2 token 使用说明
使用token标签的时候,Struts2会建立一个GUID(全局唯一的字符串)放在session中,并且会成为一个hidden放在form中. token拦截器会判断客户端form提交的token和s ...
- java之如何实现调用启动一个可执行文件,exe
/* * 运行可执行文件:.exe * 当要执行一个本地机器上的可执行文件时, * 可以使用java.lang包中的Runtime类,首先使用Runtime类,首先 * 使用Runtime类声明一个对 ...
- 初学java之常用组件
import javax.swing.*; import java.awt.*; class Win extends JFrame { JTextField mytext; // 设置一个文本区 JB ...
- Spring配置文件解析--集合注入方法
<bean id="moreComplexObject" class="example.ComplexObject"> <property n ...
- 关闭“JDK自动更新”提示