PaintHouse I
ColorCostDP.hpp
//
// Created by Administrator on 2021/7/21.
//
#ifndef C__TEST01_COLORCOSTDP_HPP
#define C__TEST01_COLORCOSTDP_HPP
#include <vector>
class ColorCostDP {
public:
ColorCostDP(vector<vector<int>> costN, int N);
void printData();
int algorithmDP(vector<vector<int>> &cost, int &N);
private:
int N; // array of bulidings
vector<vector<int>> cost;
};
ColorCostDP::ColorCostDP(vector<vector<int>> costN, int N):
cost(costN), N(N)
{
cost.resize(costN.size());
for (int i = 0; i < costN.size(); ++i) {
cost[i].resize(costN.size());
}
}
void ColorCostDP::printData() {
for(int i; i<cost.size();++i){
for (int j = 0; j < cost[i].size(); ++j) {
cout<<cost[i][j]<<" ";
}
}
}
int ColorCostDP::algorithmDP(vector<vector<int>> &cost, int &N){
vector<vector<int>> f;
f.resize(N+1); //f[1] is the first buliding
for(int i = 0; i<f.size(); ++i)
f[i].resize(3); //color of every buliding
for(int i = 0; i<f.size(); ++i){
if(i == 0) {
f[0][0] = f[0][1] = f[0][2] = 0; //Initialization
continue;
}
if(i == 1) {
f[i][0] = cost[i-1][0];
f[i][1] = cost[i-1][1];
f[i][2] = cost[i-1][2]; //Initialization
continue;
}
for(int j = 0; j < 3; ++j){
f[i][j] = INT_MAX;
for(int k = 0; k < 3; ++k){
if(j == k) continue;
if(f[i-1][k] + cost[i-1][j]<f[i][j])
f[i][j] = f[i-1][k] + cost[i-1][j];
}
}
}
int result = INT_MAX;
for(int i = 0; i<3; i++){
if(f[f.size()-1][i] < result)
result = f[f.size()-1][i];
}
return result;
}
#endif //C__TEST01_COLORCOSTDP_HPP
main.cpp
#include <iostream>
using namespace std;
/*
* 有一排N栋房子,每栋房子要漆成3种颜色中的一种:红蓝绿
任何相邻的两栋房子不能漆成同样的颜色
第i栋房子要染成红色、蓝色和绿色的花费分别为cost[i][0] cost[i][1] cost[i][2]
问最少花多少钱
例子:
输入:
- N = 3
- Cost = [[14, 2, 11], [11, 14, 5], [14, 3, 10]]
-输出:10
* */
#include "ColorCostDP.hpp"
int main() {
vector<vector<int>> cost = {
{14, 2, 11},
{11, 14, 5},
{14, 3, 10},
};
int N = 4;
ColorCostDP ccdp(cost, N);
//ccdp.printData();
int result;
result = ccdp.algorithmDP(cost, N);
cout << result << endl;
return 0;
}
PaintHouse I的更多相关文章
- PaintHouse II
// // Created by Administrator on 2021/7/27. // #ifndef C__TEST01_PAINTHOUSE_HPP #define C__TEST01_P ...
- LintCode刷题笔记-- PaintHouse 1&2
标签: 动态规划 题目描述: There are a row of n houses, each house can be painted with one of the k colors. The ...
- [LeetCode] Paint House 粉刷房子
There are a row of n houses, each house can be painted with one of the three colors: red, blue or gr ...
- LeetCode Paint House
原题链接在这里:https://leetcode.com/problems/paint-house/ 题目: There are a row of n houses, each house can b ...
- 【Todo】所有Locked的题目的分析解答
下面这个链接有比较全的leetcode题目包括锁的 http://www.cnblogs.com/grandyang/p/4606334.html https://leetcode.com/probl ...
- 256. Paint House
题目: There are a row of n houses, each house can be painted with one of the three colors: red, blue o ...
- 【LeetCode】1165. Single-Row Keyboard 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...
- 【LeetCode】256. Paint House 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...
随机推荐
- css3新增属性-background背景
css3新增属性 边框属性 背景属性 文字属性 颜色属性 背景属性 属性 说明 background-image 添加背景图片 background-size 指定背景图像的大小 background ...
- 1.2 Simple Code!(翻译)
Simple Code! 简洁编码 Playing football is very simple, but playing simple football is the hardest thing ...
- Linux常用命令查看文件、别名、切换目录、创建目录、查看当前目录
一.创建条件(使用liunx常用命令): 1.查看阿里云的环境是否搭建完成 首先快捷键 win+R 输入cmd 回车,打开命令提示符输入命令 ssh,回车. 2.登录阿里云账户 输入命令格式:ssh ...
- MySQL复习(二)MySQL基本数据类型
MySQL基本数据类型 常用的字段类型大致可以分为数值类型.字符串类型.日期时间类型三大类 1. 数值类型 数值类型可以分为整型.浮点型.定点型三小类. 1.1 整型 (tiny:极小的, small ...
- Scrum Meeting 0503
零.说明 日期:2021-5-3 任务:简要汇报两日内已完成任务,计划后两日完成任务 一.进度情况 组员 负责 两日内已完成的任务 后两日计划完成的任务 qsy PM&前端 完成登录.后端管理 ...
- 【二食堂】Alpha - Scrum Meeting 7
Scrum Meeting 7 例会时间:4.17 11:40 - 12:00 进度情况 组员 昨日进度 今日任务 李健 1. 继续文本区域的开发,先完成目前简陋的添加方式,再区实现勾选功能issue ...
- springboot读取配置文件中的信息
在一个项目中,我们有时候会把一些配置信息写入到一个配置文件中,在java代码中读取配置文件的信息.在此记录下读取属性文件中的内容. 在springboot项目中,springboot的配置文件可以使用 ...
- 脚本:bat实现自动转换windows远程端口
问题描述:通过一个脚本可以实现windows远程端口的转换,这个是拷贝过来学习的一个脚本 @echo off color f0 echo 修改远程桌面3389端口(支持Windows 2003 200 ...
- (四)、Docker 镜像
1.Docker镜像是什么? 镜像是一种轻量级.可执行的独立软件包,用来打包软件运行环境和基于运行环境开发的软件,它包含运行某个软件所需的所有内容,包括代码.运行时.库.环境变量和配置文件. 2.Do ...
- 震惊,hzoi的考试竟然折磨简单,活到爆!
众所周知,hzoi的考试题非常"简单",那么究竟有多简单呢?最近,一位外国小哥开发出了hzoi的考试竟然折磨简单,活到爆!的方法,这究竟是怎么一回事呢?快和小编一起来看看吧- 满分 ...