[topCoder-每日一二题]--[6]----动态规划
2006-11-16 21:15
302 查看
一个比较有意思的题目,一条街上有很多房子,现在需要对房子染色(红,绿,蓝) ,染色需要耗费资金,染成rgb耗费不同,并且相邻房子的颜色不同,求最少耗费。给出一个vector<string>houses,每一个元素形如" 1 100 123",表示染成rgb对应的耗费。
分析:还是利用动态规划,对于第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);
}
};
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. |
程序:
#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);
}
};
相关文章推荐
- [topCoder-每日一二题]--[4]----动态规划的使用
- [topCoder-每日一二题]--[5]
- TopCoder每日一二题--1
- TopCoder每日一二题--2
- [topCoder-每日一二题]--[3]
- 每日一题(7)——0-1背包问题(动态规划)
- 每日一题(19)——数组分割(动态规划)
- 每日AC-LeetCode-palindrome-partitioning-ii --动态规划
- 每日一题(7)——0-1背包问题(动态规划)
- topcoder-SRM565-div2-第二题-500分--搜索/动态规划
- 【动态规划】TopCoder SRM 573 division2 WolfPackDivTwo
- 每日AC--LeetCode-- wordBreak--动态规划
- Topcoder Arena SRM 144 DIV2 1100 point 图遍历(非递归)+动态规划
- Topcoder TriviaGame 动态规划
- 每日一题(4)——动态规划《Introduction to Algorithms》总结篇
- 【动态规划】 TopCoder SRM 555 CuttingBitString
- 每日一题(1)——滑雪问题(动态规划)
- 每日一题(5)——公正陪审团问题(动态规划)
- 每日一题(18)——买书问题(动态规划)
- 每日一题(18)——买书问题(动态规划)