light oj1074
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
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户人,打算把他们的房子图上颜色,有red、green、blue三种颜色,每家人涂不同的颜色要花不同的费用。
并且相邻两户人家之间的颜色要不同。求最小的总花费费用。
思路:
这个题与刘汝佳的算法竞赛与入门经典中的数字三角形有点类似,能够參照其方法。利用动态规划的思想。找出
状态转移方程,dp[i][j%3+1] = a[i][j%3+1] + min(dp[i-1][(j-1)%3+1], dp[i-1][(j+1)%3+1],这样答案就出来了。
代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int dp[23][4];
int a[23][4];
int main()
{
int n, t;
scanf("%d",&t);
for(int k = 1; k <= t; k++)
{
scanf("%d",&n);
memset(a, 0, sizeof(a));
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= 3; j++)
scanf("%d",&a[i][j]);
}
for (int i = 1; i <= n; i++)
{
for (int j = 3; j < 6; j++)
{
dp[i][j%3+1] = a[i][j%3+1] + min(dp[i-1][(j-1)%3+1], dp[i-1][(j+1)%3+1]); }
}
printf("Case %d: %d\n", k, min(dp[n][1], min(dp[n][2], dp[n][3])));
}
return 0;
}
light oj1074的更多相关文章
- CSharpGL(13)用GLSL实现点光源(point light)和平行光源(directional light)的漫反射(diffuse reflection)
CSharpGL(13)用GLSL实现点光源(point light)和平行光源(directional light)的漫反射(diffuse reflection) 2016-08-13 由于CSh ...
- 【MVVM Light】Messager的使用
一.前言 在MVVM编程的模式中,有时候我们会遇到一个很尴尬的情况: 若干个xaml.cs都复用一个ViewModel,当ViewModel想传递一个特定的消息给某一个xaml.cs的时候 ...
- bzoj4691: Let There Be Light
如果原点能被一个光源照到,那么这两个点之间一定没有任何球.我们可以通过三分距离来确定某线段和球是否有交点. 注意到m非常小,于是我们可以枚举原点被哪些光源照到.由于\(O(2^{n}*m)\)会超时, ...
- 【MVVM Light】新手初识MVVM,你一看就会
一.前言 作为一个初入软件业的新手,各种设计模式与框架对我是眼花缭乱的.所以当我接触到这些新知识的时候就希望自己能总结几个步骤,以便更好更方便的在日常工作中进行使用. MVVM顾名思义就是Model- ...
- Sensor(LIGHT)
package com.example.sensor01; import java.util.List; import android.hardware.Sensor; import android. ...
- sourcesafe.light 开源项目启动
sourcesafe.light 源于一个2D独立砖块沙盒游戏. 在这个游戏的设计中碰到了一个瓶颈:这个游戏想把玩家变成一个个neo,在矩阵世界中没有什么不可以修改. 这个游戏要跨平台,玩家的修改操作 ...
- C#Light 和 uLua的对比第二弹
上次的对比大家还有印象否,C#Light和ulua对比各有胜负 今天我们加入一个去反射优化,这是uLua没办法实现的优化,我们也就只能不要脸的胜之不武了 以原生执行同一测试时间为X1,数字越小的越快 ...
- C#Light/Evil合体啦
决定将C#Light和C#Evil合并成一个项目,毕竟C#Evil包含C#Light所有的功能,分开两个,基本的表达式方面有什么bug还得两头改 暂时就C#Light/Evil这么叫吧,庆祝合体,画了 ...
- C#最良心脚本语言C#Light/Evil,Xamarin\WP8\Unity热更新最良心方案,再次进化.
C#Light的定位是嵌入式脚本语言,一段C#Light脚本是一个函数 C#Evil定位为书写项目的脚本语言,多脚本文件合作,可以完全用脚本承载项目. C#Light/Evil 使用完全C#一致性语法 ...
随机推荐
- ie9以下的浏览器兼容性问题
.bind不兼容的问题Function.prototype.bind = function () { var fn = this, args = Array.prototype.slice.call( ...
- MySQL实现当前数据表的所有时间都增加或减少指定的时间间隔
DATE_ADD() 函数向日期添加指定的时间间隔. 当前表所有数据都往后增加一天时间: UPDATE ACT_BlockNum SET CreateTime = DATE_ADD(CreateTim ...
- JS高级——静态成员与实例成员
静态成员:构造函数的属性和方法 实例成员:实例化之后对象的属性和方法 // $("#id").css(); // $("#id").text(); // $.t ...
- c++将bool变量以文字形式打印
#include <iostream> // std::cout, std::boolalpha, std::noboolalpha int main () { bool b = true ...
- ARM处理器的寄存器,ARM与Thumb状态,7中运行模式
** ARM处理器的寄存器,ARM与Thumb状态,7中运行模式 分类: 嵌入式 ARM处理器工作模式一共有 7 种 : USR 模式 正常用户模式,程序正常执行模式 FIQ模式(Fast ...
- dotfuscator 如何设置
- form:input 标签使用
<form:input path="suplier" htmlEscape="false" maxlength="50" id=&qu ...
- C# DataTable扩展方法
在日常搬砖中,总结了一些简单的扩展方法. public static bool IsNullOrEmpty(this DataTable dt) { ; } public static bool Is ...
- 【解题报告】 Leapin' Lizards HDU 2732 网络流
[解题报告] Leapin' Lizards HDU 2732 网络流 题外话 在正式讲这个题目之前我想先说几件事 1. 如果大家要做网络流的题目,我在网上看到一个家伙,他那里列出了一堆网络流的题目, ...
- [Luogu] P4198 楼房重建
题目描述 小A的楼房外有一大片施工工地,工地上有N栋待建的楼房.每天,这片工地上的房子拆了又建.建了又拆.他经常无聊地看着窗外发呆,数自己能够看到多少栋房子. 为了简化问题,我们考虑这些事件发生在一个 ...