有三个杯子,容量分别为 A,B,C。
初始时,C 杯装满了水,而 A,B 杯都是空的。
现在在保证不会有漏水的情况下进行若干次如下操作:
将一个杯子 x 中的水倒到另一个杯子 y 中,当 x 空了或者 y 满了时就停止(满足其中一个条件才停下)。
请问,在操作全部结束后,C 中的水量有多少种可能性。
输入格式
输入包含多组测试数据。
每组数据占一行,包含三个整数 A,B,C。
输出格式
每组数据输出一个结果,占一行。
数据范围
0≤A,B,C≤4000,
每个输入最多包含 100 组数据。
输入样例:
0 5 5
2 2 4
输出样例:
2
3
代码:
// 暴搜 枚举所有状态 判重
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL B = 10000;
int C[3];
unordered_set<LL> states;
unordered_set<int> cs;
LL get(int c[]) // 哈希
{
return c[2] * B * B + c[1] * B + c[0];
}
void pour(int c[], int i, int j)
{
int t = min(c[i], C[j] - c[j]);
c[i] -= t, c[j] += t;
}
void dfs(int c[])
{
states.insert(get(c));
cs.insert(c[2]);
int a[3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (i != j)
{
memcpy(a, c, sizeof a);
pour(a, i, j);
if (!states.count(get(a)))
dfs(a);
}
}
}
}
int main()
{
while (cin >> C[0] >> C[1] >> C[2])
{
states.clear();
cs.clear();
int c[3] = {0, 0, C[2]};
dfs(c);
cout << cs.size() << endl;
}
return 0;
}