非常可乐
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14827 Accepted Submission(s): 5950
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
Recommend
LL
你有三个容量分别为S, M, N的无刻度瓶子且保证S == M + N, 初始状态下S装满了可乐,问最少倒多少次能使得S, M, N 中
两个容器各装为S / 2体积的可乐。
直接BFS疯狂赋值黏贴模拟过程就好了,一个容器要倒到另一个容器就两种情况,要不然一个瓶子倒空,要不然另一个瓶子倒满,溢出也好负数也好直接通过一个check剪掉就好了。。就不用一直用if 去 judge了
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<stack>
using namespace std;
int const N = 100010;
int n, m, w;
int arm;
int visa[200], visb[200], visc[200];
int mark;
struct Node
{
int a;
int b;
int c;
int ans;
};
bool judge(Node x)
{
if(visa[x.a] && visb[x.b] && visc[x.c]) return false;
if(x.a > w || x.b > n || x.c > m) return false;
if(x.a < 0 || x.b < 0 || x.c < 0) return false;
return true;
}
int bfs(Node x)
{
queue<Node> q;
Node now;
Node next;
int t;
int ans = 0;
q.push(x);
while(!q.empty()){
now = q.front();
q.pop();
if(now.a == arm && now.b == arm || now.a == arm && now.c == arm || now.b == arm && now.c == arm){
return now.ans;
}
if(judge(now) == false) continue;
visa[now.a] = 1;
visb[now.b] = 1;
visc[now.c] = 1;
t = n - now.b; //1 - 2
next.a = 0; //1Пе
next.b = now.b + now.a;
next.c = now.c;
next.ans = now.ans + 1;
q.push(next);
next.a = now.a - t; //2Тњ
next.b = n;
next.c = now.c;
next.ans = now.ans + 1;
q.push(next);
t = m - now.c; //1 - 3
next.a = 0; //1Пе
next.c = now.c + now.a;
next.b = now.b;
next.ans = now.ans + 1;
q.push(next);
next.a = now.a - t; //2Тњ
next.c = m;
next.b = now.b;
next.ans = now.ans + 1;
q.push(next);
t = w - now.a; //2 - 1
next.b = 0; //2Пе
next.a = now.b + now.a;
next.c = now.c;
next.ans = now.ans + 1;
q.push(next);
next.b = now.b - t; //1Тњ
next.a = w;
next.c = now.c;
next.ans = now.ans + 1;
q.push(next);
t = m - now.c; //2 - 3
next.b = 0; //2Пе
next.c = now.c + now.b;
next.a = now.a;
next.ans = now.ans + 1;
q.push(next);
next.b = now.b - t; //3Тњ
next.c = m;
next.a = now.a;
next.ans = now.ans + 1;
q.push(next);
t = w - now.a; //3 - 1
next.c = 0; //3Пе
next.a = now.c + now.a;
next.b = now.b;
next.ans = now.ans + 1;
q.push(next);
next.c = now.c - t; //1Тњ
next.a = w;
next.b = now.b;
next.ans = now.ans + 1;
q.push(next);
t = n - now.b; //3 - 2
next.c = 0; //3Пе
next.b = now.c + now.b;
next.a = now.a;
next.ans = now.ans + 1;
q.push(next);
next.c = now.c - t; //2Тњ
next.b = n;
next.a = now.a;
next.ans = now.ans + 1;
q.push(next);
ans ++;
}
return 0;
}
int main()
{
int a, b;
Node first;
int ans;
while(scanf("%d%d%d", &w, &n, &m) == 3){
if(w % 2){
printf("NO\n");
continue;
}
if(!n && !m && !w) break;
arm = w / 2;
memset(visa, 0, sizeof(visa));
memset(visb, 0, sizeof(visb));
memset(visc, 0, sizeof(visc));
first.a = w;
first.b = 0;
first.c = 0;
first.ans = 0;
ans = bfs(first);
if(ans) printf("%d\n", ans);
else printf("NO\n");
}
return 0;
}
后来再翻这道题的时候发现有数论的做法,然而我还看不懂。。。代码短的一比。。。。惭愧。。。还有很长的路要走。。。这样的代码才叫做ACM啊
http://m.blog.youkuaiyun.com/article/details?id=52097459