SQYBI.com

Change is a part of life, and takes part in finding us who we are.

UVa 102 -- Ecological Bin Packing

| 0 comments

水题,枚举即可.
第一次提交WA,没有注意这道题并不是Special Judge,如果有多组顺序符合要求,需要输出字典序最靠前的一组.
虽然用了string,不过代码写的还是很垃圾.

/*
  UVa 102; Ecological Bin Packing
  - sqybi's code
*/
//for my winsty

#include
#include
using namespace std;

const int nn = 3;
int t, res, now;
int a[nn][nn];
bool u[nn];
char ch[nn] = {'B', 'G', 'C'};
string ress, nows;

void get(int i)
{
    if (i == 3)
    {
        if (now > res || (now == res && nows < ress))
        {
            res = now;
            ress = nows;
        }
        return;
    }
    for (int j = 0; j != 3; ++ j)
        if (! u[j])
        {
            u[j] = true;
            now = now + a[i][j];
            nows += ch[j];
            get(i+1);
            nows = nows.erase(i, 1);
            now = now - a[i][j];
            u[j] = false;
        }
}

int main()
{
    while (scanf("%d", &a[0][0]) != EOF)
    {
        t = a[0][0];
        for (int i = 0; i != 3; ++ i)
            for (int j = 0; j != 3; ++ j)
                if (i + j)
                {
                    scanf("%d", &a[i][j]);
                    t += a[i][j];
                }
        res = 0;
        now = 0;
        nows = "";
        memset(u, false, sizeof(u));
        get(0);
        cout << ress << ' ' << t - res << endl;
    }
}

You May Like These:

  1. DS大作业的两段代码
  2. 确定性有限状态自动机(DFA)
  3. UVa 100 -- The 3n + 1 problem
  4. TopCoder SRM 413
  5. NOIP 2008 第四题 双栈排序(twostack) 题解

Author: sqybi

理工男,宅男,闵行MIT荣誉出品。

发表评论

Required fields are marked *.

*


请使用@user: comment的格式来回复一个人的评论, 或者直接点击评论后的"回复". 例如:
@sqybi: 你好!
这样sqybi将会收到一封通知邮件.