2014年4月7日 星期一

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.

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.

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.

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

出處: UVa Online Judge - Ecological Bin Packing


問題敘述

此問題會有很多行輸入,每一行都會有九個整數,前三個整數分別代表在第一個回收桶裡棕色瓶子, 綠色瓶子和透明瓶子的數量,中間三個整數分別代表在第二個回收桶裡棕色瓶子, 綠色瓶子和透明瓶子的數量,最後三個整數分別代表在第三個回收桶裡棕色瓶子, 綠色瓶子和透明瓶子的數量。此問題要求您用最少的次數移動瓶子,來讓每個桶子都只裝一種顏色,並且輸出每個桶子各擺哪些顏色以及最少移動的次數。若不只一種移動方法可以讓每個桶子都只裝一種顏色且移動次數最少,則字母排列順序比較優先的要被輸出。

解題思路

因為瓶子的顏色只有三種,總共也只有六種排列方式,故暴力法是最簡單直接的解法。照字母順序把六種擺放瓶子顏色的方式都算出移動次數,並照順序找出移動次數最小的排列方式就是解答。

c++ 程式碼

#include <iostream>
#include <vector>
#include <map>
using namespace std;

int main() {
    int start;

    while (cin >> start) {
        vector< vector<int> > binBottle(3, vector<int>(3, -1));
        vector<string> comb(6, "");
        map<string, int> getCount;
        string minComb;

        comb[0] = "BCG";
        comb[1] = "BGC";
        comb[2] = "CBG";
        comb[3] = "CGB";
        comb[4] = "GBC";
        comb[5] = "GCB";
        for (int i=0; i<binBottle.size(); i++) {
            for (int j=0; j<binBottle[i].size(); j++) {
                if (!i && !j)
                    binBottle[i][j] = start;
                else
                    cin >> binBottle[i][j];
            }
        }
        getCount[comb[0]] = binBottle[1][0] + binBottle[2][0] + binBottle[0][2] + binBottle[2][2] +
                            binBottle[0][1] + binBottle[1][1];
        getCount[comb[1]] = binBottle[1][0] + binBottle[2][0] + binBottle[0][1] + binBottle[2][1] +
                            binBottle[0][2] + binBottle[1][2];
        getCount[comb[2]] = binBottle[1][2] + binBottle[2][2] + binBottle[0][0] + binBottle[2][0] +
                            binBottle[0][1] + binBottle[1][1];
        getCount[comb[3]] = binBottle[1][2] + binBottle[2][2] + binBottle[0][1] + binBottle[2][1] +
                            binBottle[0][0] + binBottle[1][0];
        getCount[comb[4]] = binBottle[1][1] + binBottle[2][1] + binBottle[0][0] + binBottle[2][0] +
                            binBottle[0][2] + binBottle[1][2];
        getCount[comb[5]] = binBottle[1][1] + binBottle[2][1] + binBottle[0][2] + binBottle[2][2] +
                            binBottle[0][0] + binBottle[1][0];
        minComb = comb[0];
        for (int i=1; i<comb.size(); i++) {
            if (getCount[minComb] > getCount[comb[i]])
                minComb = comb[i];
        }
        cout << minComb << " " << getCount[minComb] << endl;
    }

    return 0;
}

沒有留言:

張貼留言