light oj 1047 - Neighbor House 动态规划
来源:互联网 发布:怎么在淘宝开养生店 编辑:程序博客网 时间:2024/05/21 02:03
题目链接
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 housei 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.………………
题意:
有n户人,打算把他们的房子图上颜色,有red、green、blue三种颜色,每家人涂不同的颜色要花不同的费用,而且相邻两户人家之间的颜色要不同,求最小的总花费费用。
思路:
动态规划,这个和刘汝佳算法竞赛入门经典P158的数字三角形有些相似,不过是求最小的值,而且有些限制,每次走到点和上次走的点不在同一列。
代码:
#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 oj 1047 - Neighbor House 动态规划
- Light OJ 1047 Neighbor House
- Light OJ 1047 Neighbor House
- Light oj 1047 - Neighbor House (简单dp)
- Light OJ 1047 Neighbor House(简单DP)
- 【light-oj】-1047 - Neighbor House(dp)
- LIGHT OJ 1047 - Neighbor House 【DP数塔】
- Light oj 1217 - Neighbor House (II)(dp)
- Light OJ 1217 Neighbor House (II) (环形DP)
- LightOJ 1047 - Neighbor House
- LightOJ 1047 - Neighbor House
- LightOJ 1047 Neighbor House
- LightOJ 1047 - Neighbor House 【DP】
- lightoj 1047 Neighbor House(Dp)
- LightOJ - 1047 Neighbor House(DP)
- LightOJ 1047 - Neighbor House(DP)
- lightoj 1047 - Neighbor House DP
- 【 lightoj 1047 - Neighbor House + dp】
- 黑马程序员——常用的数据库操作
- javascript创建对象的几种方式
- acm常见问题
- 使用JSCH连接Linux服务器-执行linux命令
- 随机组队赛 Diligent Boys Don’t Love 终于AC了。 我知错了。。。555
- light oj 1047 - Neighbor House 动态规划
- JQuery学习(2)选择器(2)
- GetVersion和GetVersionEx
- c# TextBox用法
- ucgui 移植(裸奔)
- 关于运行错误:java.lang.UnsupportedClassVersionError: Bad version number in .class file
- 黑马程序员_筛选DataTable中的数据,批量导入到数据库中
- Declare Function GetVersion Lib "kernel32" Alias "GetVersion" () As Long
- Java开发中的23种设计模式