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
就是三个杯子,反正我是分了8个状态,因为有可能到不满,也有可能倒满。然后就是搜索,脑子一抽写了深搜,超时了,发现搜索最沮丧的就是写了dfs后发现时bfs,然而并不想继续写下去了……
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
int n, m, s, leap[105][105][105], pre, time, dp[105][105][105];
struct Node{
int x;
int y;
int z;
};
int min(int a, int b)
{
return a < b ? a : b;
}
queue<Node>p;
int main()
{
int i, j, k, tempx, tempy, tempz, x, y, z;
Node temp, now;
while (scanf("%d%d%d", &s, &n, &m) != EOF)
{
if (!s&&!n&&!m)break;
if (s % 2 != 0)
cout << "NO" << endl;
else
{
for (i = 0; i <= 104;i++)
for (j = 0; j <= 104;j++)
for (k = 0; k <= 104; k++)
leap[i][j][k] = 0;
leap[s][0][0] = 1;
pre = s / 2; time = 999;
temp.x = s; temp.y = temp.z = 0;
dp[s][0][0] = 0;
p.push(temp);
while (p.size())
{
temp = p.front();
x = temp.x; y = temp.y; z = temp.z;
if (dp[x][y][z] <= time){
if (x == y&&x == pre)
time = min(time, dp[x][y][z]);
else if (x == z&&x == pre)
time = min(time, dp[x][y][z]);
else if (y == z&&y == pre)
time = min(time, dp[x][y][z]);
else
{
tempx = x - (n - y); tempy = n; tempz = z;
if (tempx >= 0 && tempx <= s&&!leap[tempx][tempy][tempz])
{
leap[tempx][tempy][tempz] = 1;
dp[tempx][tempy][tempz] = dp[x][y][z] + 1;
now.x = tempx; now.y = tempy; now.z = tempz;
p.push(now);
}
tempx = x - (m - z); tempy = y; tempz = m;
if (tempx >= 0 && tempx <= s&&!leap[tempx][tempy][tempz])
{
leap[tempx][tempy][tempz] = 1;
dp[tempx][tempy][tempz] = dp[x][y][z] + 1;
now.x = tempx; now.y = tempy; now.z = tempz;
p.push(now);
}
tempx = x + y; tempy = 0; tempz = z;
if (tempx >= 0 && tempx <= s&&!leap[tempx][tempy][tempz])
{
leap[tempx][tempy][tempz] = 1;
dp[tempx][tempy][tempz] = dp[x][y][z] + 1;
now.x = tempx; now.y = tempy; now.z = tempz;
p.push(now);
}
if (y + z > m)
{
tempx = x; tempy = y - (m - z); tempz = m;
if (!leap[tempx][tempy][tempz])
{
leap[tempx][tempy][tempz] = 1;
dp[tempx][tempy][tempz] = dp[x][y][z] + 1;
now.x = tempx; now.y = tempy; now.z = tempz;
p.push(now);
}
}
else
{
tempx = x; tempy = 0; tempz = y + z;
if (!leap[tempx][tempy][tempz])
{
leap[tempx][tempy][tempz] = 1;
dp[tempx][tempy][tempz] = dp[x][y][z] + 1;
now.x = tempx; now.y = tempy; now.z = tempz;
p.push(now);
}
}
tempx = x + z; tempy = y; tempz = 0;
if (!leap[tempx][tempy][tempz])
{
leap[tempx][tempy][tempz] = 1;
dp[tempx][tempy][tempz] = dp[x][y][z] + 1;
now.x = tempx; now.y = tempy; now.z = tempz;
p.push(now);
}
if (y + z > n)
{
tempx = x; tempy = n; tempz = z - (n - y);
if (!leap[tempx][tempy][tempz])
{
leap[tempx][tempy][tempz] = 1;
dp[tempx][tempy][tempz] = dp[x][y][z] + 1;
now.x = tempx; now.y = tempy; now.z = tempz;
p.push(now);
}
}
else
{
tempx = x; tempy = z; tempz = 0;
if (!leap[tempx][tempy][tempz])
{
leap[tempx][tempy][tempz] = 1;
dp[tempx][tempy][tempz] = dp[x][y][z] + 1;
now.x = tempx; now.y = tempy; now.z = tempz;
p.push(now);
}
}
}
}
p.pop();
}
if (time == 999)
cout << "NO" << endl;
else
cout << time << endl;
}
}
return 0;
}