“插花问题”的动态规划法算法
2008-04-10 03:21
274 查看
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//:============================“插花问题”的动态规划法算法============================
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#define F 100
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#define V 100
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
插花问题描述:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
将f束鲜花插入v个花瓶中,使达到最徍的视觉效果,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
问题相关约定及插花要求:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
鲜花被编号为1--f,花瓶被编号为1--v,花瓶按从小到
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
大顺序排列,一只花瓶只能插一支花,鲜花i插入花瓶j中的
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
视觉效果效果值已知,编号小的鲜花所放入的花瓶编号也小
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
问题求解思路:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
花瓶j(1<=j<=v)中插入鲜花的可能编号为[1..j](编号
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
小的鲜花所放入的花瓶编号也小);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
设数组p[i][j]表示鲜花i插入花瓶j的好看程度,数组
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
q[i][j]表示[1..i]束鲜花插入[1..j]个花瓶所能得到的最大
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
好看程度,初始化q[0][0] = 0;q[0][j]=0(1<=j<=v),则q[f][v]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
是问题的解.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
特别地,j束鲜花插入到前面的j只花瓶中,所得到的好看
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
程度是q[j][j] = p[1][1]+p[2][2]+...+[j][j].现将插花过
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
程按花瓶排列顺序划分成不同阶段,则在第j阶段,第i束鲜花
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
若放入第j号花瓶,最大好看程度是q[i-1][j-1]+p[i][j];第i束鲜
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
花若放入前j-1个花瓶中的某一个,所得的好看程度是q[i][j-1],
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
那么在第j阶段,插入第i束鲜花所能得到的最大好看程度为:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
q[i][j] = MAX(q[i-1][j-1]+p[i][j],q[i][j-1]),要使q[i][j]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
最大,应使q[i-1][j-1]和q[i][j-1]也最大
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#define MAX(A,B) ((A) > (B) ? (A):(B)) //求取两数的最大值宏定义
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#define F 100 //鲜花数最大值常量定义
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#define V 100 //花瓶数最大值常量定义
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//“插花问题”的初始化函数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// int f,v: 鲜花数量,花瓶个数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// int p[][v]: 鲜花i插入花瓶j的好看程度
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void Flower_Initialize(int *f,int *v,int p[][V])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int i,j;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("输入鲜花数量及花瓶个数:");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
scanf("%d%d",f,v);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("顺序输入各鲜花插入各花瓶的好看程度: ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(i=1;i<=*f;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(j=1;j<=*v;j++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p[i][j] = i*j;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//scanf("%d",&p[i][j]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//“插花问题”的动态规划法解决函数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// int p[][v]: 鲜花i插入花瓶j的好看程度
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// int f,v: 鲜花数量,花瓶个数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// int *way: 鲜花插入花瓶的插入方法结果
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int Ikebana(int p[][V],int f,int v,int *way)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int i,j,q[F][V],newv;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
q[0][0] = 0; //初始化[没有一束花插入花瓶时],好看程度自然为0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//设置v个花瓶分别被插入v束鲜花时各号花瓶对应的(初始)最大好看程度
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(j = 1;j <= v;j++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
q[0][j] = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//设置第j束鲜花放入第j号花瓶中的最大好看程度
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
q[j][j] = q[j - 1][j - 1] + p[j][j];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(j = 1;j <= v;j++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(i = 1;i < j;i++) //计算在第j阶段,插入第i束鲜花所能得到的最大好看程度
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
q[i][j] = MAX(q[i - 1][j - 1] + p[i][j],q[i][j - 1]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
newv = v;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(i = f;i > 0;i--)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
while(q[i-1][newv-1]+p[i][newv] < q[i][newv])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
newv--;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//确定鲜花i插在花瓶newv中,并准备考虑前一只花瓶
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
way[i] = newv--;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return(q[f][v]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//测试“插花问题”的动态规划法函数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void Run_Ikebana()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//循环计数器,鲜花数量,花瓶个数,鲜花i插入花瓶j的好看程度,鲜花插入花瓶的插入方法结果
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int i,f,v,p[F][V],way[F];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Flower_Initialize(&f,&v,p);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("最大好看程度点数为%d ",Ikebana(p,f,v,way));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("插有鲜花的花瓶是: ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(i = 1;i <= f;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("%4d",way[i]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//:============================“插花问题”的动态规划法算法============================
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int main(int argc, char* argv[])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Run_SubString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Run_Ikebana();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf(" 应用程序运行结束! ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
相关文章推荐