gpt4 book ai didi

c - UVa 102 - Ecology Bin Packing 错误答案

转载 作者:行者123 更新时间:2023-12-02 03:24:17 25 4
gpt4 key购买 nike

我在学习UVa 102 - Ecological Bin Packing问题:

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 在线评判系统给了我 WA,但我不明白为什么。

以下是我的代码:

#include <stdio.h>
#include <stdlib.h>

int getSumOfBottles(int bottles[3][3]){
int i, j, sum;

for(i = 0; i < 3; i++){
for (j = 0; j < 3; j++){
sum += bottles[i][j];
}
}

return sum;
}


void showBGC(int inputNum){
switch (inputNum){
case 0:
printf("B");
break;
case 1:
printf("G");
break;
case 2:
printf("C");
break;
}
}

/* method 2*/
/* just use 6 kinds of posibilities to count*/

int main(int argc, char *argv[]) {

int i, j;

/* char array, with ', B == 0, G == 1, C ==2 */
char bcgText[3] = {'B', 'G', 'C'};

/* since BCG should show up in the begining, move {0, 2, 1} */
int posibleIndex[6][3] = {{0, 2, 1}, {0, 1, 2}, {1, 0, 2}, {1, 2, 0}, {2, 0, 1}, {2, 1, 0}};

int bottles[3][3];

int sumBottles;
int sumMove;

int minMove;
int minMoveIndex = 0;

while(scanf("%d%d%d%d%d%d%d%d%d", &bottles[0][0], &bottles[0][1], &bottles[0][2],
&bottles[1][0], &bottles[1][1], &bottles[1][2],
&bottles[2][0], &bottles[2][1], &bottles[2][2]) != EOF){

sumBottles = getSumOfBottles(bottles);
minMove = sumBottles;

/* get the moves in each cases, and find the minMovements */
for (i = 0; i < 6; i++){
sumMove = sumBottles - (bottles[0][posibleIndex[i][0]] + bottles[1][posibleIndex[i][1]] + bottles[2][posibleIndex[i][2]]);

if (sumMove < minMove){
minMove = sumMove;
minMoveIndex = i;
/* printf("index %d, minMove %d", i, minMove); */
}
}

/* one line output */
printf("%c%c%c %d\n", bcgText[posibleIndex[minMoveIndex][0]],
bcgText[posibleIndex[minMoveIndex][1]],
bcgText[posibleIndex[minMoveIndex][2]], minMove);
}
return 0;
}

有人可以给我一些提示吗?

最佳答案

您错过了“如果超过一个顺序的棕色、绿色和透明容器产生最少移动次数,则应打印代表最小配置的字母顺序第一个字符串。”您的 posibleIndex [6] [3] 初始化应该按照{{0, 2, 1}, {0, 1, 2}, {2,0,1}, {2,1,0}, {1, 0, 2}, {1, 2, 0} } 因为您已将 bcgText[3] 数组初始化为 { 'B', 'G', 'C'} 以保持字典顺序。还有一点,您应该将 getSumOfBottles 函数中声明的 sum 变量初始化为 0。尽管即使不初始化 sum 也会被接受,但这是一种不好的做法。

关于c - UVa 102 - Ecology Bin Packing 错误答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31260839/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com