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的更多相关文章

  1. PaintHouse II

    // // Created by Administrator on 2021/7/27. // #ifndef C__TEST01_PAINTHOUSE_HPP #define C__TEST01_P ...

  2. LintCode刷题笔记-- PaintHouse 1&2

    标签: 动态规划 题目描述: There are a row of n houses, each house can be painted with one of the k colors. The ...

  3. [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 ...

  4. LeetCode Paint House

    原题链接在这里:https://leetcode.com/problems/paint-house/ 题目: There are a row of n houses, each house can b ...

  5. 【Todo】所有Locked的题目的分析解答

    下面这个链接有比较全的leetcode题目包括锁的 http://www.cnblogs.com/grandyang/p/4606334.html https://leetcode.com/probl ...

  6. 256. Paint House

    题目: There are a row of n houses, each house can be painted with one of the three colors: red, blue o ...

  7. 【LeetCode】1165. Single-Row Keyboard 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...

  8. 【LeetCode】256. Paint House 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...

随机推荐

  1. 关于web项目中的资源跳转

    1.跳转包括两种方式: 转发 forward 重定向 redirect 2.两种方式的代码: AServlet类: //向request范围中存储数据 request.setAttribute(&qu ...

  2. web_security学习路线

    一.了解黑客是如何工作的 1.在虚拟机配置Linux系统 2.漏洞测试工具 3.msf控制台 4.远程工具RATS 5.远程访问计算机 6.白帽 二.技术基础 漏斗扫描工具AWVS AWVS简介 安装 ...

  3. CF487E Tourists + 圆方树学习笔记(圆方树+树剖+线段树+multiset)

    QWQ果然我已经什么都学不会的人了. 这个题目要求的是图上所有路径的点权和!QWQ(我只会树上啊!) 这个如果是好啊 这时候就需要 圆方树! 首先在介绍圆方树之前,我们先来一点简单的前置知识 首先,我 ...

  4. bzoj3073Journeys(线段树优化最短路)

    这里还是一道涉及到区间连边的问题. 如果暴力去做,那么就会爆炸 那么这时候就需要线段树来优化了. 因为是双向边 所以需要两颗线段树来分别对应入边和出边 QwQ然后做就好了咯 不过需要注意的是,这个边数 ...

  5. 《手把手教你》系列技巧篇(三十二)-java+ selenium自动化测试-select 下拉框(详解教程)

    1.简介 在实际自动化测试过程中,我们也避免不了会遇到下拉选择的测试,因此宏哥在这里直接分享和介绍一下,希望小伙伴或者童鞋们在以后工作中遇到可以有所帮助. 2.select 下拉框 2.1Select ...

  6. 靶场渗透CH4INRULZ_v1.0.1

    最新文章见我个人博客:点此 靶机环境下载地址:[下载] ova下载下来后直接导入virtualbox即可(https://www.vulnhub.com/entry/ch4inrulz-101,247 ...

  7. 【UE4 C++ 基础知识】<12> 多线程——FRunnable

    概述 UE4里,提供的多线程的方法: 继承 FRunnable 接口创建单个线程 创建 AsyncTask 调用线程池里面空闲的线程 通过 TaskGraph 系统来异步完成一些自定义任务 支持原生的 ...

  8. 从原理—实战分析SQL注入

    前言 SQL注入是web安全中最常见的攻击方式,SQL注入有很多方法,但如果只知道payload或只用用sqlmap,不知道原理,感觉也很难掌握,这次就总结一下我所遇到的SQL注入方法,原理分析+题目 ...

  9. vue3.x自定义组件双向数据绑定v-model

    vue2.x 语法 在 2.x 中,在组件上使用 v-model 相当于绑定 value prop 并触发 input 事件: <ChildComponent v-model="pag ...

  10. Spring Security中配置AccessDeniedHandler没有生效

    现象 在 WebSecurityConfigurerAdapter 配置了如下代码: // 自定义未授权和未登录异常 http.exceptionHandling() .accessDeniedHan ...