原题:
非常可乐
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 39190 Accepted Submission(s): 15332
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校园文化活动月”之“校庆杯”大学生程序设计竞赛暨杭州电子科技大学第四届大学生程序设计竞赛
代码:
#include <iostream>
#include <vector>
#include <iterator>
#include <string>
#include <algorithm>
#include <cstring>
#include <limits>
#include <queue>
#include <functional>
using namespace std;
const int maxn = 200000 + 1;
typedef long long ll;
int A,B,C;
bool vis[201][201][201];
struct node
{
int a, b, c,step;
};
// 4 1 3
bool a2b(node& tmp)
{
if (tmp.a == 0 || tmp.b == B)
return false;
if (tmp.a > B - tmp.b)
{
tmp.a -= (B - tmp.b);
tmp.b = B;
}
else
{
tmp.b += tmp.a;
tmp.a = 0;
}
return true;
}
bool a2c(node& tmp)
{
if (tmp.a == 0 || tmp.c == C)
return false;
if (tmp.a > C - tmp.c)
{
tmp.a -= (C - tmp.c);
tmp.c = C;
}
else
{
tmp.c += tmp.a;
tmp.a = 0;
}
return true;
}
bool b2c(node & tmp)
{
if (tmp.b == 0 || tmp.c == C)
return false;
if (tmp.b > C - tmp.c)
{
tmp.b -= (C - tmp.c);
tmp.c = C;
}
else
{
tmp.c += tmp.b;
tmp.b = 0;
}
return true;
}
bool b2a(node& tmp)
{
if (tmp.b == 0 || tmp.a == A)
return false;
if (tmp.b > A - tmp.a)
{
tmp.b -= (A - tmp.a);
tmp.a = A;
}
else
{
tmp.a += tmp.b;
tmp.b = 0;
}
return true;
}
bool c2a(node& tmp)
{
if (tmp.c == 0 || tmp.a == A)
return false;
if (tmp.c > A - tmp.a)
{
tmp.c -= (A - tmp.a);
tmp.a = A;
}
else
{
tmp.a += tmp.c;
tmp.c = 0;
}
return true;
}
bool c2b(node& tmp)
{
if (tmp.c == 0 || tmp.b == B)
return false;
if (tmp.c > B - tmp.b)
{
tmp.c -= (B - tmp.b);
tmp.b = B;
}
else
{
tmp.b += tmp.c;
tmp.c = 0;
}
return true;
}
bool visNode(node& tmp)
{
if (vis[tmp.a][tmp.b][tmp.c])
return true;
return false;
}
int bfs()
{
memset(vis, false, sizeof(vis));
node st{ A,0,0 ,0};
queue<node> Q;
Q.push(st);
vis[A][0][0] = 1;
while (!Q.empty())
{
node head = Q.front();
Q.pop();
if (head.b == head.c && head.b == A/ 2 && head.a == 0
|| head.b == head.a && head.b == A / 2 && head.c == 0
|| head.c == head.a && head.c == A / 2 && head.b == 0)
{
return head.step;
}
node tmp = head;
if (a2c(tmp))
{
if (!visNode(tmp))
{
tmp.step++;
vis[tmp.a][tmp.b][tmp.c] = 1;
Q.push(tmp);
}
}
tmp = head;
if (a2b(tmp))
{
if (!visNode(tmp))
{
tmp.step++;
vis[tmp.a][tmp.b][tmp.c] = 1;
Q.push(tmp);
}
}
tmp = head;
if (b2a(tmp))
{
if (!visNode(tmp))
{
tmp.step++;
vis[tmp.a][tmp.b][tmp.c] = 1;
Q.push(tmp);
}
}
tmp = head;
if (b2c(tmp))
{
if (!visNode(tmp))
{
tmp.step++;
vis[tmp.a][tmp.b][tmp.c] = 1;
Q.push(tmp);
}
}
tmp = head;
if (c2a(tmp))
{
if (!visNode(tmp))
{
tmp.step++;
vis[tmp.a][tmp.b][tmp.c] = 1;
Q.push(tmp);
}
}
tmp = head;
if (c2b(tmp))
{
if (!visNode(tmp))
{
tmp.step++;
vis[tmp.a][tmp.b][tmp.c] = 1;
Q.push(tmp);
}
}
}
return 0;
}
int main()
{
ios::sync_with_stdio(false);
while (cin >> A >> B >> C, A + B + C)
{
int ans = bfs();
if (ans)
{
cout << ans << endl;
}
else
{
cout << "NO" << endl;
}
}
return 0;
}
解答:
裸的广搜,也是经典的倒水问题和隐式图搜索问题。没啥好说的