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 Sample Output NO 3 |
#include <iostream>
#include <queue>
using namespace std;
int s; // 一罐可乐
int a, b; // 2个杯子
int half;
typedef struct Now {
int s;
int a, b;
int step;
} Now;
queue<Now> q;
bool mark[110][110][110];
int aToB(int& a, int& aMax, int& b, int& bMax) {
if (b == bMax) {
return 0;
}
if (a == 0) {
return 0;
}
// 倒了, 有改动
// b缺口非常大,a全到进去了
if (a <= bMax - b) {
b += a;
a = 0;
} else {
//B缺口小,a倒了一部分,满了
a -= bMax - b;
b = bMax;
}
return 1;
}
void printNow(Now& a) {
cout << "contain: s a b:" << a.s << " " << a.a << " " << a.b << " step :" << a.step << endl;
}
bool judge(Now& n) {
if (n.s == n.a && n.a == half || n.s == n.b && n.b == half || n.a == n.b && n.b == half) {
return 1;
} else {
return 0;
}
}
/*操作:或将目标容器倒满,或将源容器倒空。*/
int bfs(int s, int a, int b) {
while(!q.empty()) {
q.pop();
}
Now start;
start.s = s;
start.a = 0;
start.b = 0;
start.step = 0;
mark[s][0][0] = 1;
Now father;
Now child;
q.push(start);
while(!q.empty()) {
father = q.front();
q.pop();
//printNow(father);
if(judge(father)) {
return father.step;
}
int ns;
int na;
int nb;
int nstep;
//s to a
ns = father.s;
na = father.a;
nb = father.b;
nstep = father.step;
if (aToB(ns, s, na, a) == 1) {
if (mark[ns][na][nb] == 0) {
//cout << "s-a" << endl;
child.s = ns;
child.a = na;
child.b = nb;
child.step = nstep + 1;
q.push(child);
mark[ns][na][nb] = 1;
}
}
//s to b
ns = father.s;
na = father.a;
nb = father.b;
if (aToB(ns, s, nb, b) == 1) {
if (mark[ns][na][nb] == 0) {
//cout << "s-b" << endl;
child.s = ns;
child.a = na;
child.b = nb;
child.step = nstep + 1;
q.push(child);
mark[ns][na][nb] = 1;
}
}
//a to s
ns = father.s;
na = father.a;
nb = father.b;
if (aToB(na, a, ns, s) == 1) {
if (mark[ns][na][nb] == 0) {
//cout << "a-s" << endl;
child.s = ns;
child.a = na;
child.b = nb;
child.step = nstep + 1;
q.push(child);
mark[ns][na][nb] = 1;
}
}
//a to b
ns = father.s;
na = father.a;
nb = father.b;
if (aToB(na, a, nb, b) == 1) {
if (mark[ns][na][nb] == 0) {
//cout << "a-b" << endl;
child.s = ns;
child.a = na;
child.b = nb;
child.step = nstep + 1;
q.push(child);
mark[ns][na][nb] = 1;
}
}
//b to s
ns = father.s;
na = father.a;
nb = father.b;
if (aToB(nb, b, ns, s) == 1) {
if (mark[ns][na][nb] == 0) {
//cout << "b-s" << endl;
child.s = ns;
child.a = na;
child.b = nb;
child.step = nstep + 1;
q.push(child);
mark[ns][na][nb] = 1;
}
}
//b to a
ns = father.s;
na = father.a;
nb = father.b;
if (aToB(nb, b, na, a) == 1) {
if (mark[ns][na][nb] == 0) {
//cout << "b-a" << endl;
child.s = ns;
child.a = na;
child.b = nb;
child.step = nstep + 1;
q.push(child);
mark[ns][na][nb] = 1;
}
}
}
return -1;
}
int main() {
while(cin >> s >> a >> b && s != 0 && a != 0 && b != 0) {
half = s / 2;
for (int i = 0; i <= s; i ++) {
for (int j = 0; j <= a; j++)
for (int k = 0; k <= b; k++) {
mark[i][j][k] = 0;
}
}
if (s % 2 == 1){
cout << "NO" << endl;
continue;
}
int res = bfs(s, a, b);
if (res == -1) {
cout << "NO" << endl;
} else {
cout << res << endl;
}
}
}
注意:千万不要 a == b == c //1 == C
需要 a == b && b == c
#include <iostream>
#include <queue>
#include <cmath>
using namespace std;
int sMax; //可乐的体积
int aMax,bMax; //两个杯子
int half;
typedef struct Now {
int s;
int a;
int b;
int cnt;
} Now;
bool mark[101][101][101];
queue<Now> q;
// OK
void s2a(Now& n) {
int k = aMax - n.a;
if (n.s >= k) {
// 目标弄满,原来的还剩下
n.a = aMax;
n.s -= k;
n.cnt++;
} else {
// 目标放入原来的全部,原来清空
n.a += n.s;
n.s = 0;
n.cnt++;
}
}
// OK
void s2b(Now& n) {
int k = bMax - n.b;
if (n.s >= k) {
// 目标弄满,原来的还剩下
n.b = bMax;
n.s -= k;
n.cnt++;
} else {
// 目标放入原来的全部,原来清空
n.b += n.s;
n.s = 0;
n.cnt++;
}
}
// OK
void a2s(Now& n) {
int k = sMax - n.s;
if (n.a >= k) {
// 目标弄满,原来的还剩下
n.s = sMax;
n.a -= k;
n.cnt++;
} else {
// 目标放入原来的全部,原来清空
n.s += n.a;
n.a = 0;
n.cnt++;
}
}
// OK
void a2b(Now& n) {
int k = bMax - n.b;
if (n.a >= k) {
// 目标弄满,原来的还剩下
n.b = bMax;
n.a -= k;
n.cnt++;
} else {
// 目标放入原来的全部,原来清空
n.b += n.a;
n.a = 0;
n.cnt++;
}
}
// OK
void b2s(Now& n) {
int k = sMax - n.s;
if (n.b >= k) {
// 目标弄满,原来的还剩下
n.s = sMax;
n.b -= k;
n.cnt++;
} else {
// 目标放入原来的全部,原来清空
n.s += n.b;
n.b = 0;
n.cnt++;
}
}
// OK
void b2a(Now& n) {
int k = aMax - n.a;
if (n.b >= k) {
// 目标弄满,原来的还剩下
n.a = aMax;
n.b -= k;
n.cnt++;
} else {
// 目标放入原来的全部,原来清空
n.a += n.b;
n.b = 0;
n.cnt++;
}
}
bool check(Now& n) {
if (n.s == half && n.a == half) {
return 1;
} else if (n.s == half && n.b == half) {
return 1;
} else if (n.a == half && n.b == half) {
return 1;
}
return 0;
}
int bfs() {
while(!q.empty()) {
q.pop();
}
Now chu;
chu.s = sMax;
chu.a = chu.b =0;
chu.cnt = 0;
mark[sMax][0][0] = 1;
Now father;
Now child;
q.push(chu);
while(!q.empty()) {
father = q.front();
q.pop();
// 父亲出来 孩子进入
// cout << "father: " << father.s << " " << father.a << " " << father.b << endl;
if (check(father) == 1) {
return father.cnt;
}
//S TO A
child = father;
s2a(child);
if (mark[child.s][child.a][child.b] == 0) {
mark[child.s][child.a][child.b] = 1;
q.push(child);
}
//s to b
child = father;
s2b(child);
if (mark[child.s][child.a][child.b] == 0) {
mark[child.s][child.a][child.b] = 1;
q.push(child);
}
//a TO s
child = father;
a2s(child);
if (mark[child.s][child.a][child.b] == 0) {
mark[child.s][child.a][child.b] = 1;
q.push(child);
}
//a TO b
child = father;
a2b(child);
if (mark[child.s][child.a][child.b] == 0) {
mark[child.s][child.a][child.b] = 1;
q.push(child);
}
//b TO s
child = father;
b2s(child);
if (mark[child.s][child.a][child.b] == 0) {
mark[child.s][child.a][child.b] = 1;
q.push(child);
}
//b TO a
child = father;
b2a(child);
if (mark[child.s][child.a][child.b] == 0) {
mark[child.s][child.a][child.b] = 1;
q.push(child);
}
}
return -1;
}
int main() {
while (cin >> sMax >> aMax >> bMax) {
if (sMax == 0 && aMax == 0 && bMax == 0) {
return 0;
}
for (int i = 0; i <= 100; i++) {
for (int j = 0; j <= 100; j++) {
for (int k = 0; k <= 100; k++) {
mark[i][j][k] = 0;
}
}
}
half = sMax / 2;
if (sMax % 2 == 1) {
cout << "NO" << endl;
continue;
} else {
int res = bfs();
if (res != -1) {
cout << res << endl;
} else {
cout << "NO" << endl;
}
}
}
}