過河問題

 N個人要從河的一側到另一次 一個船最多做2個人 這個船的速度受慢的人的影響。

輸入一堆數,爲每個人 的時間,怎麼規劃划船的總時間最少?
 
 
  1. #include <iostream> 
  2. #include <list> 
  3. #include <algorithm> 
  4. using namespace std; 
  5. void input(); 
  6. void sortlist(); 
  7. void show(); 
  8. void solution(int sel); 
  9. list<int> timelistA,timelistB; 
  10. int time; 
  11.  
  12. void main() 
  13.     input(); 
  14.     sortlist(); 
  15.     solution(0); 
  16.     cout<<endl<<"總用時"<<time<<"秒"<<endl<<endl; 
  17.  
  18. void solution(int sel) 
  19.     timelistA.sort(); 
  20.     timelistB.sort(); 
  21.  
  22.     if (timelistA.size()==0) 
  23.     { 
  24.         return
  25.     } 
  26.     if(0==sel)  // 左->右 
  27.     { 
  28.         if (timelistA.size()>1) 
  29.         { 
  30.             list<int >::iterator a=timelistA.begin(); 
  31.             list<int >::iterator b=timelistA.begin();b++; 
  32.          
  33.         timelistB.push_back(*a); 
  34.         cout<<*a; 
  35.         timelistA.erase(a); 
  36.         cout<<" "<<*b<<"過去"<<endl; 
  37.         timelistB.push_back(*b); 
  38.         time+=*b; 
  39.         timelistA.erase(b); 
  40.          
  41.         } 
  42.         else  
  43.         { 
  44.                 cout<<timelistA.front()<<" "<<"過去"<<endl; 
  45.                 timelistB.push_back(timelistA.front()); 
  46.                 time+=timelistA.front(); 
  47.                 timelistA.erase(timelistA.begin()); 
  48.              
  49.         } 
  50.         solution(1); 
  51.     } 
  52.     else if(1==sel) //右->左 回來 
  53.     { 
  54.     cout<<timelistB.front()<<"  回來"<<endl; 
  55.     timelistA.push_back(timelistB.front()); 
  56.     time+=timelistB.front(); 
  57.     timelistB.erase(timelistB.begin()); 
  58.      
  59.     solution(2); 
  60.     } 
  61.     else if (2==sel)//左->右 
  62.     { 
  63.         list<int >::iterator a=timelistA.end(); 
  64.         a--;  
  65.         list<int >::iterator b=a; 
  66.         b--; 
  67.         timelistB.push_back(*a); 
  68.         timelistB.push_back(*b); 
  69.         cout<<*a<<" "
  70.         cout<<*b<<" 過去"<<endl; 
  71.             time+=*a; 
  72.         timelistA.erase(a); 
  73.         timelistA.erase(b); 
  74.         solution(3); 
  75.     } 
  76.     else if (3==sel) 
  77.     { 
  78.  
  79.         cout<<timelistB.front()<<" 過來"<<endl; 
  80.             time+=timelistB.front(); 
  81.             timelistA.push_back(timelistB.front()); 
  82.         timelistB.erase(timelistB.begin()); 
  83.         solution(0); 
  84.     } 
  85.      
  86.  
  87.  
  88. void input() 
  89. {    
  90.     cout<<"input every one :"<<endl; 
  91.  
  92.     while(1) 
  93.     { 
  94.         int a; 
  95.         cin>>a; 
  96.         timelistA.push_back(a); 
  97.  
  98.         if (getchar()=='\n'
  99.         { 
  100.             break
  101.         } 
  102.     } 
  103.     cout<<endl; 
  104.  
  105. void sortlist() 
  106.     timelistA.sort(); 
  107.  
  108. void show() 
  109.     list<int>::iterator a=timelistA.begin(); 
  110.     for(;a!=timelistA.end();++a) 
  111.         cout<<*a<<"\t"
  112. }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章