2013
12-30

# UVa OJ 102 – Ecological Bin Packing (生态学装箱问题)

Time limit: 3.000 seconds

## 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 231.

## 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

BCG 30
CBG 50

## Solution解答

#include <iostream>
using namespace std;
int main(void) {
//3种颜色的全部6种排列表。
//按字母顺序排列，当移动次数相同时，后者颜色必大于第0个的颜色。
char *const pClrs[6] = { "BCG", "BGC", "CBG", "CGB", "GBC", "GCB"};
//循环处理每行输入，最后输出结果
for (int n[3][3], aMove[6], nMin, i; cin >> n[0][0];) {
//数组n为各箱各颜色瓶子数。第1维是箱子编号，第2维是瓶子颜色
cin >> n[0][1] >> n[0][2] >> n[1][0] >> n[1][1];
//输入顺序为BGC，第一个数据的输入放在循环头部，以便退出
cin >> n[1][2] >> n[2][0] >> n[2][1] >> n[2][2];
//按上面的6种排列计算每种方法的移动次数
//第0种移法需要移动第1和第2个箱子中的B，等
aMove[0] = n[1][0] + n[2][0] + n[0][2] + n[2][2] + n[0][1] + n[1][1];
aMove[1] = n[1][0] + n[2][0] + n[0][1] + n[2][1] + n[0][2] + n[1][2];
aMove[2] = n[1][2] + n[2][2] + n[0][0] + n[2][0] + n[0][1] + n[1][1];
//第3种移法需要移动第0和第2个箱子中的G，等
aMove[3] = n[1][2] + n[2][2] + n[0][1] + n[2][1] + n[0][0] + n[1][0];
aMove[4] = n[1][1] + n[2][1] + n[0][0] + n[2][0] + n[0][2] + n[1][2];
aMove[5] = n[1][1] + n[2][1] + n[0][2] + n[2][2] + n[0][0] + n[1][0];
//寻找移动次数最小的编号
for (nMin = i = 0; ++i < 6; nMin = aMove[i] < aMove[nMin] ? i : nMin);
//输出结果最小值
cout << pClrs[nMin] << ' ' << aMove[nMin] << endl;
}
return 0;
}

1. 因为是要把从字符串s的start位到当前位在hash中重置，修改提交后能accept，但是不修改居然也能accept

2. “再把所有不和该节点相邻的节点着相同的颜色”，程序中没有进行不和该节点相邻的其他节点是否相邻进行判断。再说求出来的也不一样是颜色数最少的