UVA, 102 Ecological Bin Packing

题意:给你仨箱子,每个箱子里面有三种瓶子B G C,让你将其分类,使得每个箱子里只有一种瓶子,

   每次只能移动一个瓶子,要求移动步数最小,相同则按字典序输出

思路:排列得到顺序有:BCG BGC CBG CGB GBC GCB 六种,都算出来按大小找就行了

    ps:输入时顺序为:B G C ,为了方便我排序成了 B C G

代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 
 4 using namespace std;
 5 int date[3][3],sum[6];
 6 int mins,p;
 7 string type[6]={"BCG","BGC","CBG","CGB","GBC","GCB"};
 8 bool datecin()//每个箱子瓶子B C G
 9 {
10     if(scanf("%d%d%d%d%d%d%d%d%d",&date[0][0],&date[0][2],&date[0][1],&date[1][0],&date[1][2],&date[1][1],&date[2][0],&date[2][2],&date[2][1])!=EOF)
11         return true;
12     return false;
13 }
14 
15 void datecal()
16 {
17 
18     sum[0]=date[1][0]+date[2][0]+ date[0][1]+date[2][1]+ date[0][2]+date[1][2];//BCG
19     sum[1]=date[1][0]+date[2][0]+ date[0][2]+date[2][2]+ date[0][1]+date[1][1];//BGC
20 
21     sum[2]=date[1][1]+date[2][1]+ date[0][0]+date[2][0]+ date[0][2]+date[1][2];//CBG
22     sum[3]=date[1][1]+date[2][1]+ date[0][2]+date[2][2]+ date[0][0]+date[1][0];//CGB
23 
24     sum[4]=date[1][2]+date[2][2]+ date[0][0]+date[2][0]+ date[0][1]+date[1][1];//GBC
25     sum[5]=date[1][2]+date[2][2]+ date[0][1]+date[2][1]+ date[0][0]+date[1][0];//GCB
26 
27     mins=sum[0],p=0;
28     for(int i=1;i<6;i++) if(mins>sum[i]) { mins=sum[i],p=i;}
29 }
30 
31 void showres()
32 {
33     datecal();
34     cout<<type[p]<<' '<<sum[p]<<endl;
35 }
36 
37 int main()
38 {
39     while(datecin())
40     {
41         showres();
42     }
43     return 0;
44 }

 

posted on 2016-05-03 16:42  八云紫是小loli  阅读(188)  评论(0编辑  收藏  举报

导航