动态规划1——之工厂生产线问题

#include <iostream>

#define LINELENGTH 6

 

int line_1[] = {7, 9, 3, 4, 8, 4} ;

int line_2[] = {8, 5, 6, 4, 5, 7} ;

int time_1[] = {2, 2, 1, 2, 2, 1} ;

int time_2[] = {4, 2, 3, 1, 3, 4} ;

int best_line_1[6] = {0} ;

int best_line_2[6] = {0} ;

int short_time_line_1[6] = {0} ;

int short_time_line_2[6] = {0} ;

int time_line1_to_end = 3 ;

int time_line2_to_end = 2 ;

int lastStep ;

int lastTime;

 

void PrintLine(int stationNumber,int line )

{

if ( stationNumber > 0 ) {

if ( line == 2) {

PrintLine(stationNumber -1 , best_line_2[stationNumber - 1]) ;

std::cout<<"Station"<<stationNumber<<" Line 2"<<std::endl;

else {

PrintLine(stationNumber -1 , best_line_1[stationNumber - 1]) ;

std::cout<<"Station"<<stationNumber<<" Line 1"<<std::endl;

}

}

}

void FindBest()

{

best_line_1[0] = 1 ;

best_line_2[0] = 2;

short_time_line_1[0] = time_1[0] + line_1[0];

short_time_line_2[0] = time_2[0] + line_2[0];

for(int i = 1; i < 6 ; i++) {

if (short_time_line_1[i-1] > short_time_line_2[i-1] + time_1[i]) {

short_time_line_1[i] = short_time_line_2[i-1] + time_1[i] + line_1[i];

best_line_1[i] = 2;

}

else {

short_time_line_1[i] = short_time_line_1[i-1] +line_1[i] ;

best_line_1[i] = 1;

}

if (short_time_line_2[i-1] > short_time_line_1[i-1] + time_2[i]) {

short_time_line_2[i] = short_time_line_1[i-1] + time_2[i] + line_2[i] ;

best_line_2[i] = 1;

else {

short_time_line_2[i] = short_time_line_2[i-1] +line_2[i] ;

best_line_2[i] = 2;

}

 

} //end of for

if ( short_time_line_1[LINELENGTH -1] + time_line1_to_end > short_time_line_2[LINELENGTH -1] + time_line2_to_end ) {

lastTime = short_time_line_2[LINELENGTH -1] + time_line2_to_end ;

lastStep = 2 ;

else

{

lastTime = short_time_line_1[LINELENGTH -1] + time_line1_to_end;

lastStep = 1 ;

}

std::cout<<"Total time is "<<lastTime<<std::endl;

if ( lastStep > 1){

PrintLine(LINELENGTH, 2) ;

//std::cout<<"Station"<<LINELENGTH<<"Line 2"<<std::endl;

else {

PrintLine(LINELENGTH, 1) ;

//std::cout<<"Station"<<LINELENGTH<<"Line 1"<<std::endl;

}

 

 

}

 

 

int main()

{

FindBest();

getchar();

return 0;

}

阅读更多
文章标签: include
个人分类: 算法导论及相关
上一篇微软等公司数据结构面试题3
下一篇微软等数据结构+算法面试100题004
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭