您的位置:首页 > 运维架构

[topCoder-每日一二题]--[6]----动态规划

2006-11-16 21:15 302 查看
一个比较有意思的题目,一条街上有很多房子,现在需要对房子染色(红,绿,蓝) ,染色需要耗费资金,染成rgb耗费不同,并且相邻房子的颜色不同,求最少耗费。给出一个vector<string>houses,每一个元素形如" 1 100 123",表示染成rgb对应的耗费。

The people of RGB Street 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 house i are houses i-1 and i+1. The first and last houses are not neighbors.

You will be given a vector <string> houses, where the ith element corresponds to house i. Each element of houses will be formatted as "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.

分析:还是利用动态规划,对于第i各房子,如果染成R cost(i,R)=min(cost(i+1,G),cost(i+1,B))同理其他,同时还是需要重复计算

程序:

#include <set>
#include <vector>
#include <string>
#include <map>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <stdio.h>
#include <complex>
#include <sstream>
#include <math.h>
#include <ctype.h>
//isalnum(), isalpha(), iscntrl(), isgraph(), isprint(), ispunct(), and isspace(),isdigit(),
#define cp(cot,type) copy(cot.begin(),cot.end(),ostream_iterator<type>(cout," "));cout<<endl
#define repn(i,n) for(int i = 0; i < n; i++)
#define repc(i,a) for(int i = 0; i < a.size(); i++)
using namespace std;
class RGBStreet
{
private:

//求当前index层,邻居为color颜色时最小耗费,有2种颜色选择,所以只要分别计算出选择这两种颜色时的耗费,//返回小的即可 

int count(int color,int index,int ret[][3],int cost[][3],int len)

{
if(index==len)
return 0;
if(color==0)//如果当前在index,为上一层为0颜色,则只需要计算index+1层1,2,然后求得最小值返回
{
if((ret[index][1]!=0)&&(ret[index][2]!=0))
return min(ret[index][1], ret[index][2]);
int a = count(1,index+1,ret,cost,len);
int b = count(2,index+1,ret,cost,len);
ret[index][1]=a+cost[index][1];
ret[index][2]=b+cost[index][2];
return min(ret[index][1], ret[index][2]);
}else if(color==1)
{
if((ret[index][0]!=0)&&(ret[index][2]!=0))
return min(ret[index][0], ret[index][2]);
int a = count(0,index+1,ret,cost,len);
int b = count(2,index+1,ret,cost,len);
ret[index][0]=a+cost[index][0];
ret[index][2]=b+cost[index][2];
return min(ret[index][0], ret[index][2]);
}else
{
if((ret[index][1]!=0)&&(ret[index][0]!=0))
return min(ret[index][1], ret[index][0]);
int a = count(1,index+1,ret,cost,len);
int b = count(0,index+1,ret,cost,len);
ret[index][1]=a+cost[index][1];
ret[index][0]=b+cost[index][0];
return min(ret[index][1], ret[index][0]);
}
}
public:
int estimateCost(vector <string> hs)
{
int cost[20][3];
int ret[20][3];
int r,g,b;
for(int i = 0; i < hs.size();i++)
sscanf(hs[i].c_str(),"%d %d %d",&cost[i][0],&cost[i][1],&cost[i][2]);
for(int i=0;i<20;i++)
for(int j = 0;j<3;j++)
ret[i][j]=0;
cout<<hs.size()-1<<endl;
r=count(0,1,ret,cost,hs.size())+cost[0][0];
g=count(1,1,ret,cost,hs.size())+cost[0][1];
b=count(2,1,ret,cost,hs.size())+cost[0][2];
return min(min(r,g),b);

}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: