There are a row of houses, each house can be painted with three colors red,
blue and green. The cost of painting each house with a certain color is different.
You have to paint all the houses such that no two adjacent houses have the same color.
You have to paint the houses with minimum cost. How would you do it?
Note: Painting house-1 with red costs different from painting house-2 with red.
The costs are different for each house and each color.

一个dp,f(i,j)表示前i个house都paint了且第i个house paint成color_j的最小cost。

 int paint(int N, int M, int[][] cost) {
int[][] res = new int[N+1][M];
for (int t=0; t<M; t++) {
res[0][t] = 0;
}
for (int i=1; i<N; i++) {
for (int j=0; j<M; j++) {
res[i][j] = Integer.MAX_VALUE;
}
}
for (int i=1; i<=N; i++) {
for (int j=0; j<M; j++) {
for (int k=0; k<M; k++) {
if (k != j) {
res[i][j] = Math.min(res[i][j], res[i-1][k]+cost[i-1][j]); //
}
}
}
}
int result = Integer.MAX_VALUE;
for (int t=0; t<M; t++) {
result = Math.min(result, res[N][t]);
}
return result;
}

F面经:painting house的更多相关文章

  1. Mysql_以案例为基准之查询

    查询数据操作

  2. hdu 3685 10 杭州 现场 F - Rotational Painting 重心 计算几何 难度:1

    F - Rotational Painting Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

  3. ARC 062 F - Painting Graphs with AtCoDeer 割点 割边 不动点 burnside引理

    LINK:Painting Graphs with AtCoDeer 看英文题面果然有点吃不消 一些细节会被忽略掉. 问每条边都要被染色 且一个环上边的颜色可以旋转. 用c种颜色有多少本质不同的方法. ...

  4. ARC062 - F. Painting Graphs with AtCoDeer (Polya+点双联通分量)

    似乎好久都没写博客了....赶快来补一篇 题意 给你一个 \(n\) 个点 , 没有重边和自环的图 . 有 \(m\) 条边 , 每条边可以染 \(1 \to k\) 中的一种颜色 . 对于任意一个简 ...

  5. Codeforces Gym 100342C Problem C. Painting Cottages 转化题意

    Problem C. Painting CottagesTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...

  6. Codeforces Gym 100342C Problem C. Painting Cottages 暴力

    Problem C. Painting CottagesTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1 ...

  7. Painting The Wall 期望DP Codeforces 398_B

    B. Painting The Wall time limit per test 1 second memory limit per test 256 megabytes input standard ...

  8. cf509B Painting Pebbles

    B. Painting Pebbles time limit per test 1 second memory limit per test 256 megabytes input standard ...

  9.  D - 粉碎叛乱F - 其他起义

    D - 粉碎叛乱 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Sta ...

随机推荐

  1. Introduction to Structured Data

    https://developers.google.com/search/docs/guides/intro-structured-data Structured data refers to kin ...

  2. Charles辅助调试接口

    http://blog.sina.com.cn/s/blog_6ae8b50d0102w7tw.html

  3. [LeetCode]题解(python):056-Merge Intervals

    题目来源 https://leetcode.com/problems/merge-intervals/ Given a collection of intervals, merge all overl ...

  4. [LeetCode]题解(python):045-Jump game II

    题目来源 https://leetcode.com/problems/jump-game-ii/ Given an array of non-negative integers, you are in ...

  5. leetcode:Factorial Trailing Zeroes

    Given an integer n, return the number of trailing zeroes in n!. 最初的代码 class Solution { public: int t ...

  6. link标签和script标签跑到body下面,网页顶部有空白

    用UltraEdit的16进制编辑模式查看代码,都是EF BB BF开头的,说明都是带BOM的.我手动的将所有文件转成UTF-8 without BOM.页面终于正常了.link,script标签乖乖 ...

  7. Car---hdu5935(简单题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5935 题意:有一辆车在马路上行驶,速度不变或增加,然后警察在某整数点时刻记录下了这辆车所经过的位置,共 ...

  8. ubuntu12.04 登录黑屏

    新安装的ubuntu12.04LTS,登录之后黑屏,切换到ubuntu2D能够进入UI.解决方法记录于此. 转载: http://blog.csdn.net/albertsh/article/deta ...

  9. 目标检测的图像特征提取之(一)HOG特征

    http://blog.csdn.net/liulina603/article/details/8291093

  10. install 命令用法详解

    install 命令用法详解 http://man.linuxde.net/install install命令的作用是安装或升级软件或备份数据,它的使用权限是所有用户.install命令和cp命令类似 ...