Back and Forth [洛谷] (状态压缩) /*rank*/

原题传送门


题面:

Back and Forth

                               time limit per test : 1 second
                           memory limit per test : 256 megabytes
                                  input : standard input
                                 output : standard output

Problem Description

Farmer John has two milking barns, each of which has a large milk tank as well as a storage closet containing 10 buckets of various sizes. He likes to carry milk back and forth between the two barns as a means of exercise. On Monday, Farmer John measures exactly 1000 gallons of milk in the tank of the first barn, and exactly 1000

gallons of milk in the tank of the second barn.

On Tuesday, he takes a bucket from the first barn, fills it, and carries the milk to the second barn, where he pours it into the storage tank. He leaves the bucket at the second barn.

On Wednesday, he takes a bucket from the second barn (possibly the one he left on Tuesday), fills it, and carries the milk to the first barn, where he pours it into the storage tank. He leaves the bucket at the first barn.

On Thursday, he takes a bucket from the first barn (possibly the one he left on Wednesday), fills it, and carries the milk to the second barn, where he pours it into the tank. He leaves the bucket at the second barn.

On Friday, he takes a bucket from the second barn (possibly the one he left on Tuesday or Thursday), fills it, and carries the milk to the first barn, where he pours it into the tank. He leaves the bucket at the first barn.

Farmer John then measures the milk in the tank of the first barn. How many possible different readings could he see?

Input

The first line of input contains 10 integers, giving the sizes of the buckets initially at the first barn. The second line of input contains 10 more integers, giving the sizes of the buckets initially at the second barn. All bucket sizes are in the range 1…100.

Output

Please print the number of possible readings Farmer John could get from measuring the milk in the tank of the first barn after Friday.

Sample Input

1 1 1 1 1 1 1 1 1 2
5 5 5 5 5 5 5 5 5 5

Sample Output

5

题意描述

John会在周一的时候将两个仓库分别充满1000加仑的牛奶,然后在周二和周四的时候会在一号仓已有的桶中选一个桶,在一号仓装满牛奶送到二号仓.而在周三和周五的时候会在二号仓已有的桶中选一个桶,在二号仓装满牛奶送到一号仓.要得出周五之后一号仓的牛奶数量的可能情况数.

题目分析

可以直接模拟,因为只有20个桶,可以用一个数字的二进制来表示一号仓和二号仓的桶的情况,桶的容量可以用一个数组储存起来,再记录一号仓的结果,直到循环到周五的时候,用一个bool数组记录当前结果存在.最后统计bool数组里面有多少个结果是存在的即可.

具体代码

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e4+5;
bool ans[maxn];
int bucket[25];
int bucket_1, bucket_2;
int doing(int a,int b, int time, int barn_A)
{
    if(time == 5)
    {
        ans[barn_A] = true;
        return 0;
    }
    if(time == 1 || time == 3)
    {
        for(int i = 1; i <= 20; i++)
        {
            if(a&(1<<i))
            {
                doing(a-(1<<i), b+(1<<i), time+1, barn_A-bucket[i]);
            }
        }
    }
    else
    {
        for(int i = 1; i <= 20; i++)
        {
            if(b&(1<<i))
            {
                doing(a+(1<<i), b-(1<<i), time+1, barn_A+bucket[i]);
            }
        }
    }
}

int main()
{
    for(int i = 1; i <= 20; i++)
    {
        scanf("%d", &bucket[i]);
    }

    bucket_1 = 0, bucket_2 = 0;
    for(int i = 1; i <= 10; i++)
    {
        bucket_1 += 1 << i;
    }
    for(int i = 11; i <= 20; i++)
    {
        bucket_2 += 1 << i;
    }

    doing(bucket_1, bucket_2, 1, 1000);
    int answer = 0;
    for(int i = 1; i <= maxn; i++)
    {
        if(ans[i])
        {
            answer++;
        }
    }
    printf("%d\n", answer);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43726593/article/details/87924396