题目:
有两个空杯子容量分别为A,B,还有一罐满满的可乐,体积为S,它们三个之间可以相互倒可乐 (都是没有刻度的,且 S==N+M,101>S>0,A>0,B>0) ,你来判断他们是否能平分,如果能则输出倒可乐的最少的次数,如果不能输出"NO"。 https://vjudge.net/problem/HDU-1495
俺来叨叨两句:
就是三个杯子倒水的问题,因为和一定,知两个杯子即可求另一杯子,同时S为奇数直接“NO”
虽然 8 = 6 + 2 这组数据考虑实际应该是两次,但我们还是默认最后必有一个空杯子吧,所以是三次
(8,0,0)~(2,6,0)~(2, 4,2)
补充一道很类似的题:
https://vjudge.net/problem/POJ-3414
//以下代码写得有些复杂了,不是太好可以对比思路
/-Time 15 ms
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include<iomanip>
#include <stack>
#include <queue>
using namespace std;
const int INF = 0x3f3f3f3f;
struct P {
int x, y;
P(int x = 0, int y = 0): x(x), y(y){}
};
int d[110][110];
int s, a, b;
void bfs() {
memset(d, INF, sizeof(d));
d[0][0] = 0;
queue<P> q;
q.push(P(0, 0));
bool findd = false;
while(!q.empty()) {
P tmp = q.front(); q.pop();
int x = tmp.x; int y = tmp.y;
if(!x && y==s/2 || !y && x==s/2 || x && y && x==s/2) // 任意两个杯子
findd = true;
if(x) { //x - > cola
if(d[0][y] > d[x][y] + 1)
q.push(P(0, y)), d[0][y] = d[x][y] + 1;
}
if(y) { //y - > cola
if(d[x][0] > d[x][y] + 1)
q.push(P(x, 0)), d[x][0] = d[x][y] + 1;
}
if(x && x+y<=b) { //x - > y
if(d[0][x+y] > d[x][y] + 1)
q.push(P(0, x+y)), d[0][x+y] = d[x][y] + 1;
}
if(x && x+y>b) { //x - > y
if(d[x+y-b][b] > d[x][y] + 1)
q.push(P(x+y-b, b)), d[x+y-b][b] = d[x][y] + 1;
}
if(y && x+y<=a) { //y - > x
if(d[x+y][0] > d[x][y] + 1)
q.push(P(x+y, 0)), d[x+y][0] = d[x][y] + 1;
}
if(y && x+y>a) { //y - > x
if(d[a][x+y-a] > d[x][y] + 1)
q.push(P(a, x+y-a)), d[a][x+y-a] = d[x][y] + 1;
}
if(x+y<s && x<a) { //cola -> x
if(d[a][y] > d[x][y] + 1)
q.push(P(a, y)), d[a][y] = d[x][y] + 1;
}
if(x+y<s && y<b) { //cola -> y
if(d[x][b] > d[x][y] + 1)
q.push(P(x, b)), d[x][b] = d[x][y] + 1;
}
}
if(findd){
int ans = min(d[0][s/2], d[s/2][0]);
ans = min(ans, d[s/2][s/2]);
cout << ans << endl;
}
else cout << "NO" << endl;
}
int main() {
while(cin >> s >> a >> b && s) {
if(s & 1) cout << "NO" << endl;
else bfs();
}
return 0;
}