NOJ-1005:装载问题-西工大算法

NOJ-1005:装载问题-西工大算法_第1张图片

#include 
#include 

int n, c1, c2;
int weight[150];
int flag[150];
int maxWeight, curWeight, weightSum;

void inPut();
void dfs(int i);

int main()
{
    int i;
    while(1)
    {
        scanf("%d%d%d", &c1, &c2, &n);
        if(c1 == c2 && n == c1 && n == 0) break;
        for(i = 1; i <= n; i++)
        {
            scanf("%d", &(weight[i]));
            weightSum += weight[i];
        }
        dfs(1);
        if(c2 >= weightSum - maxWeight)
        {
            printf("Yes\n");
        }
        else
        {
            printf("No\n");
        }
        maxWeight = 0;
        weightSum = 0;
    }
    return 0;
}

void dfs(int i)
{
    if(i == n + 1)
    {
        return;
    }
    {
        flag[i] = 0;
        dfs(i + 1);
    };
    {
        if(c1 >= weight[i] + curWeight)
        {
            flag[i] = 1;
            curWeight += weight[i];
            if(maxWeight < curWeight)
            {
                maxWeight = curWeight;
            }
            dfs(i + 1);
        }
    };
    if(flag[i])
    {
        curWeight -= weight[i];
    }
}

 

你可能感兴趣的:(算法设计与分析)