2010年1月14日 星期四

102 - Ecological Bin Packing

Background

Bin packing, or the placement of objects of certain weights into different bins subject to certain constraints, is an historically interesting problem. Some bin packing problems are NP-complete but are amenable to dynamic programming solutions or to approximately optimal heuristic solutions.
In this problem you will be solving a bin packing problem that deals with recycling glass.

The Problem

Recycling glass requires that the glass be separated by color into one of three categories: brown glass, green glass, and clear glass. In this problem you will be given three recycling bins, each containing a specified number of brown, green and clear bottles. In order to be recycled, the bottles will need to be moved so that each bin contains bottles of only one color.
The problem is to minimize the number of bottles that are moved. You may assume that the only problem is to minimize the number of movements between boxes.
For the purposes of this problem, each bin has infinite capacity and the only constraint is moving the bottles so that each bin contains bottles of a single color. The total number of bottles will never exceed 2^31.

在回收玻璃瓶時我們根據其顏色分為三類:棕色、綠色、和透明三種。在問題當中會 有三個回收桶,裡面含有若干個上述三種顏色的瓶子。為了要能徹底做好回收,我們 必須要將一些瓶子從某個回收桶拿出放到另外一個,使得到最後每個桶子裡都只有一 種顏色的瓶子。
我們現在所要做的就只有一個目的,讓移動瓶子的步數盡可能地弄到最少。
我們可以假設這些回收桶都具有無限的容量,所以我們唯一要關心的就只有 如何操作,讓每個桶子裡都只含有一種顏色的瓶子。瓶子的總數量不會超過 2 的 31 次方。



The Input

The input consists of a series of lines with each line containing 9 integers. The first three integers on a line represent the number of brown, green, and clear bottles (respectively) in bin number 1, the second three represent the number of brown, green and clear bottles (respectively) in bin number 2, and the last three integers represent the number of brown, green, and clear bottles (respectively) in bin number 3. For example, the line 10 15 20 30 12 8 15 8 31
indicates that there are 20 clear bottles in bin 1, 12 green bottles in bin 2, and 15 brown bottles in bin 3.
Integers on a line will be separated by one or more spaces. Your program should process all lines in the input file.


輸入包括了若干行的整數,而且每一行都剛剛好有 9 個整數。頭三個數字代表 第一個回收桶裡分別有多少個棕色、綠色和透明的瓶子。接下來的三個數字則是 第二個回收桶裡分別有多少個棕色、綠色和透明的瓶子。最後三個數字代表 第三個回收桶裡分別有多少個棕色、綠色和透明的瓶子。 例如 10 15 20 30 12 8 15 8 31 就代表第一個回收桶裡有 20 個透明的瓶子, 第二個回收桶有 12 個綠色瓶, 第三個回收桶有 15 個棕色瓶子。
數字彼此間會由一個以上的空白做為分隔。你的程式必須處理輸入中的每一行資料。

The Output

For each line of input there will be one line of output indicating what color bottles go in what bin to minimize the number of bottle movements. You should also print the minimum number of bottle movements.
The output should consist of a string of the three upper case characters 'G', 'B', 'C' (representing the colors green, brown, and clear) representing the color associated with each bin.
The first character of the string represents the color associated with the first bin, the second character of the string represents the color associated with the second bin, and the third character represents the color associated with the third bin.
The integer indicating the minimum number of bottle movements should follow the string.
If more than one order of brown, green, and clear bins yields the minimum number of movements then the alphabetically first string representing a minimal configuration should be printed.


對於輸入的每一行,都必須有相對應的一行輸出,來表示哪種顏色的瓶子 要放入哪一個回收桶,才能使移動瓶子的步驟數達到最少,同時你也必須印出 其步驟數。
輸出的每一行都必須有一個由大寫字母 'G' , 'B' , 'C' 所構成的字串 來代表哪個回收桶裡放得是哪種瓶子(其中 G 代表綠色、 B 代表藍色 、 C 代表透明)
其字串的第一個字母代表第一個桶子放的是哪種瓶子,第二個字母代表第二個桶子 ,依此類推。)
在字串後面必須要接一個整數,代表移動瓶子的最少步驟數。
如果有兩種以上的可能配置其步驟數都是一樣最少, 則選擇按照字典順序排在最前的字串。

Sample Input


1 2 3 4 5 6 7 8 9
5 10 5 20 10 5 10 20 10

Sample Output


BCG 30
CBG 50
 
這題實在想不出怎麼寫,參考別人的c語言寫法才,寫出。
 
# -*- coding: utf-8 -*-
# Python version: 2.5.4

data = raw_input('請輸入九組不規則數字,並以空白\' \'做間隔區分:')
rb = data.split(' ')

while len(rb) != 9:
   data = raw_input('輸入錯誤!請重新輸入九組不規則數字,並以空白\' \'做間隔區分:')
   rb = data.split(' ')

count = 0
b,g,c=[],[],[]

for i in range(0,len(rb)):
   rb[i] = int(rb[i])
  
for j in range(0,7,3):
   b.append(rb[j]),g.append(rb[j+1]),c.append(rb[j+2])
   
b1,g1,c1=0,0,0
box = ['B','G','C']
   
for i in range(0,3):
   b1 += b[i];g1 += g[i];c1 += c[i]

count=b1+g1+c1
print b,g,c
print b1,g1,c1,count

for i in range(0,3):
   for j in range(0,3):
      for k in range(0,3):
         while i != j and j!= k and i != k:
            if b1-b[i]+g1-g[k]+c1-c[j] < count:
               count = b1-b[i]+g1-g[k]+c1-c[j]
               box[i] = 'B';box[k] = 'G';box[j] = 'C'
            break
         
print box[0]+box[1]+box[2],count 
>>> ================================ RESTART ================================
>>> 
請輸入九組不規則數字,並以空白' '做間隔區分:5 10 5 20 10 5 10 20 10
[5, 20, 10] [10, 10, 20] [5, 5, 10]
35 40 20 95
CBG 50
>>> 

沒有留言:

張貼留言