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

一个比较有意思的题目,一条街上有很多房子,现在需要对房子染色(红,绿,蓝) ,染色需要耗费资金,染成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);
  
 }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值