非常可乐
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 10 Accepted Submission(s) : 9
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为。因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和seeyou一样多。但seeyou的手中只有两个杯子,它们的容量分别是N 毫升和M 毫升 可乐的体积为S (S<101)毫升 (正好装满一瓶) ,它们三个之间可以相互倒可乐 (都是没有刻度的,且 S==N+M,101>S>0,N>0,M>0) 。聪明的ACMER你们说他们能平分吗?如果能请输出倒可乐的最少的次数,如果不能输出"NO"。
Input
三个整数 : S 可乐的体积 , N 和 M是两个杯子的容量,以"0 0 0"结束。
Output
如果能平分的话请输出最少要倒的次数,否则输出"NO"。
Sample Input
7 4 3
4 1 3
0 0 0
Sample Output
NO
3
Author
seeyou
Source
“2006校园文化活动月”之“校庆杯”大学生程序设计竞赛暨杭州电子科技大学第四届大学生程序设计竞赛
这里用的方法是dfs,重点是对其进行剪枝。创建一个结构体数组,保存每一个出现过的且到达改状态步数最少的{rests,restm,restn}三元组。重复且步数不是最小则保存,重复但步数比原来的小则更新,不重复则保存下来。同时如果s是奇数或者m+n!=s则直接剪枝。从而大幅降低复杂度。
下面就消化代码吧。
#include <iostream>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
typedef struct apair
{
int x;
int y;
int z;
int step;
}appair;
bool flag;
bool sixflag[6];//0:sm,1:sn,2:mn,3:nm,4:ms,5:ns
int nmin;
int m, n, s;
vector<int> v(3);
apair p[1000];
int search(int x, int y, int z,int num)
{
for (int i = 0; i < num; i++)
{
if (p[i].x==x&&p[i].y==y&&p[i].z==z)
return i;
}
return -1;
}
void changecap(int &a,int &b,int &resta,int &restb)//a->b
{
if (resta == 0) return;
if (resta > b - restb)
{
resta = resta - b + restb;
restb = b;
}
else
{
restb = restb + resta;
resta = 0;
}
}
int truemin;
void dfs(int num,int last,int rests,int restm,int restn,int count)
{
int se = search(rests, restm, restn, count);
if (se != -1)
if (p[se].step > num)
{
p[se].step = num;
}
else return;
else {
p[count] = { rests,restm,restn,num };
count++;
}
if (restm == s / 2 || restn == s / 2||rests==s/2)
{
flag = true;
if (num < nmin)
{
nmin = num;
truemin = num;
}
if (rests != 0 && restm != 0 && restn != 0)
truemin = num + 1;
return;
}
if (num > nmin)
return;
if (last != 4) {//0:sm,1:sn,2:mn,3:nm,4:ms,5:ns
if (rests > m - restm)
dfs(num + 1, 0, rests - m + restm, m, restn, count);
else
dfs(num + 1, 0, 0, rests + restm, restn, count);
}
if (last != 5) {
if (rests > n - restn)
dfs(num + 1, 1, rests - n + restn, restm, n, count);
else
dfs(num + 1, 1, 0, restm, restn + rests, count);
}
if (last != 3) {
if (restm > n - restn)
dfs(num + 1, 2, rests, restm - n + restn, n, count);
else
dfs(num + 1, 2, rests, 0, restm + restn, count);
}
if (last != 2) {
if (restn > m - restm)
dfs(num + 1, 3, rests, m, restn - m + restm, count);
else
dfs(num + 1, 3, rests, restm + restn, 0, count);
}
if (last != 0) {
if (restm > s - rests)
dfs(num + 1, 4, s, restm - s + rests, restn, count);
else
dfs(num + 1, 4, rests + restm, 0, restn, count);
}
if (last != 1) {
if (restn > s - rests)
dfs(num + 1, 5, s, restm, restn - s + rests, count);
else
dfs(num + 1, 5, rests + restn, restm, 0, count);
}
}
int main()
{
while (cin>>s>>m>>n)
{
if (s == 0) break;
if (m > n)
{
int t;
t = m;
m = n;
n = t;
}
flag = false;
memset(sixflag,false,sizeof(sixflag));
nmin = 100;
if (s % 2 == 1||m+n!=s)
flag = false;
else
{
dfs(0,-1,s,0,0,0);//没有操作所以是-1
}
if (flag == true)
cout << truemin << endl;
else
cout << "NO" << endl;
}
}