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 题意:见中文题面 思路:该题是求二维的子矩阵的最大值与最小值的差值尽量小.所以可以考 ...
随机推荐
- 转:Singleton模式
C++完美实现Singleton模式 转自:http://www.cppblog.com/dyj057/archive/2005/09/20/346.html boost库的Singleton的实现 ...
- java中在linux下利用jstack检测死锁
首先,编写一个死锁程序 package deadlock; public class testJstack { final static Object resource_1 = new Object( ...
- Hibernate的Restrictions用法
Restrictions.eq --> equal,等于. Restrictions.allEq --> 参数为Map对象,使用key/value进行多个等于的比对,相当于多个Restri ...
- 【poj2728】Desert King
[poj2728]Desert King 题意 最优比率生成树. http://blog.csdn.net/ophunter_lcm/article/details/10113817 分析 Dinke ...
- parent,parents和closest
1.parent parent() 获得当前匹配元素集合中每个元素的父元素,使用选择器进行筛选是可选的. <ul id="menu" style="width:10 ...
- Search for a Range [LeetCode]
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- IO流--字符流
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java ...
- Mybatis update In
mysql语句如下: ,,) mybatis的mapper如下: int updateStateByIDs(@Param("ids") String[] ids, @Param(& ...
- Android 编程下的代码混淆
什么是代码混淆 Java 是一种跨平台的.解释型语言,Java 源代码编译成中间”字节码”存储于 class 文件中.由于跨平台的需要,Java 字节码中包括了很多源代码信息,如变量名.方法名,并且通 ...
- 冲销交货单WS_REVERSE_GOODS_ISSUE
LOOP AT ITAB. AT END OF VBELN. PERFORM FRM_LOCK_DELIVERY(ZSDS0002) USING ITAB-VBELN. CALL FUNCTION ' ...