UVa 102 - Eco Bean Packaging Wrong Answer

I am studying UVa 102 - Ecological Bin Packing question:

Eco bin packaging

Background

Bin-packing or placing objects of certain weights in different cells, subject to certain restrictions, is a historically interesting problem. Some problems with packing in a beam are NP-complete, but suitable for dynamic programming solutions or optimal heuristic solutions.

In this problem, you will be solving the problem of packing a basket that deals with glass recycling.

Problem

Recycled glass requires glass to be categorized by color into one of three categories: brown glass, green glass, and clear glass. In this challenge, you will be given three recycling containers, each containing a specific amount of brown, green, and clear bottles. To be recycled, the bottles will need to be moved so that each bin contains only one color of bottles.

The challenge is to keep the number of bottles moved to a minimum. You might assume that the only problem is keeping the amount of movement between boxes to a minimum.

For the purposes of this problem, each bit has infinite capacity, and the only limitation is the movement of the bottles, so that each bin contains bottles of the same color. The total number of bottles will never exceed 2 ^ 31.

entrance

The input consists of a series of lines with each line containing 9 integers. The first three integers on the line represent the number of brown, green and transparent bottles (respectively) in box number 1, and in the second, the number of brown, green and transparent bottles (respectively) in cell number 2, and the last three integers represent the number of brown ones. green and transparent bottles (respectively) in bunker number 3.

For example, the line 10 15 20 30 12 8 15 8 31

indicates that there are 20 clear bottles in drawer 1, 12 green bottles in basket 2, and 15 brown bottles in basket 3.

Integers in the string will be separated by one or more spaces. Your program should process all lines in the input file.

Output

For each line of input, there will be one line of output indicating which colored bottles go to some bin to minimize the number of bottle movements. You should also print the minimum number of bottle movements.

The output should be a string of three uppercase letters "G", "B", "C" (representing the colors green, brown and crisp) representing the color associated with each drawer.

The first character in the string represents the color associated with the first bin, the second character in the string represents the color associated with the second bin, and the third character represents the color associated with the third bin.

An integer indicating the minimum number of bottle movements must follow the line.

If more than one order of brown, green and clear bins produces the minimum number of movements, then the first line in alphabetical order should be printed, representing the minimum configuration.

Input example

1 2 3 4 5 6 7 8 9


5 10 5 20 10 5 10 20 10

Output result

BCG 30


CBG 50

Now I'm stuck ... UVa's online judging system gives me WA, but I can't figure out why.

Below is my code:

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

      

Can someone please guide me?

+3


source to share


1 answer


You missed the line "If more than one order of brown, green and clear bins gives the minimum number of moves, then the first line in alphabetical order should be printed, representing the minimum configuration." Your initialization of posibleIndex [6] [3] should be in the order {{0, 2, 1}, {0, 1, 2}, {2,0,1}, {2,1,0}, {1, 0 , 2}, {1, 2, 0}} because you initialized the bcgText [3] array as {'B', 'G', 'C'} to maintain lexicographic ordering. One more thing, you must initialize the sum variable declared in the getSumOfBottles function from 0. Although you will get even without initializing the sum, it is bad practice.



+1


source







All Articles