F面经:painting house

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。

 1 int paint(int N, int M, int[][] cost) {
 2     int[][] res = new int[N+1][M];
 3     for (int t=0; t<M; t++) {
 4         res[0][t] = 0;
 5     }
 6     for (int i=1; i<N; i++) {
 7         for (int j=0; j<M; j++) {
 8             res[i][j] = Integer.MAX_VALUE;
 9         }
10     }
11     for (int i=1; i<=N; i++) {
12         for (int j=0; j<M; j++) {
13             for (int k=0; k<M; k++) {
14                 if (k != j) {
15                     res[i][j] = Math.min(res[i][j], res[i-1][k]+cost[i-1][j]); //
16                 }
17             }
18         }
19     }
20     int result = Integer.MAX_VALUE;
21     for (int t=0; t<M; t++) {
22         result = Math.min(result, res[N][t]);
23     }
24     return result;
25 }
时间: 2024-11-05 22:34:56

F面经:painting house的相关文章

过分过分进货价获国家

http://f.dangdang.com/group/24554/3491082/http://f.dangdang.com/group/24554/3491087/http://f.dangdang.com/group/24554/3491094/http://f.dangdang.com/group/24554/3491099/http://f.dangdang.com/group/24554/3491105/http://f.dangdang.com/group/24554/349111

我们找个地方看好戏

http://v.qq.com/page/f/y/4/m041433ssun.html http://v.qq.com/page/f/y/4/m041433ssun.html http://v.qq.com/page/f/y/4/m04143o3lhg.html http://v.qq.com/page/f/y/4/m04144675h3.html http://v.qq.com/page/f/y/4/m04144k1k1j.html http://v.qq.com/page/f/y/4/m04

hdu 3685 10 杭州 现场 F - Rotational Painting 重心

F - Rotational Painting Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 3685 Appoint description:  System Crawler  (2014-11-09) Description Josh Lyman is a gifted painter. One of his great works

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

似乎好久都没写博客了....赶快来补一篇 题意: 给你一个 \(n\) 个点 , 没有重边和自环的图 . 有 \(m\) 条边 , 每条边可以染 \(1 \to k\) 中的一种颜色 . 对于任意一个简单环 , 可以将它的边的颜色进行旋转任意位 . 询问本质不同的染色方案数个数 . 数据范围: \(1\le n \le 50\\ 1 \le m \le 100\\1 \le k \le 100\\\) 题解: 将边分为 3种类型: 不属于任何一个简单环 , 它的贡献为 \(k\) . 属于且仅属

? D - 粉碎叛乱F - 其他起义

D - 粉碎叛乱 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1528 Description Adam and Eve play a card game using a regular deck of 52 cards. The rules are simple. The players sit on opposite sides

Line Painting

Line Painting Time limit: 2.0 secondMemory limit: 64 MB The segment of numerical axis from 0 to 109 is painted into white color. After that some parts of this segment are painted into black, then some into white again and so on. In total there have b

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

Problem C. Painting CottagesTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/attachments Description The new cottage settlement is organized near the capital of Flatland. The construction company that is building the settl

[poj 1691] Painting A Board dfs+拓扑排序

Painting A Board Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3611 Accepted: 1795 Description The CE digital company has built an Automatic Painting Machine (APM) to paint a flat board fully covered by adjacent non-overlapping rectangle

Codeforces Gym 100342C Problem C. Painting Cottages 暴力

Problem C. Painting CottagesTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/attachments Description The new cottage settlement is organized near the capital of Flatland. The construction company that is building the sett