HDU 1495 非常可乐
非常可乐
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3654 Accepted Submission(s): 1522
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
思路:BFS,需要考虑的情况比较多,相比之下我的代码比较水,看类长路漫漫啊
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int hash[110][110][110];
int s,n,m;
struct Node
{
int s,n,m;
int step;
};
int BFS()
{
queue <Node> q;
Node top;top.s = s;top.n = 0;top.m = 0;top.step = 0;
q.push(top);
while(!q.empty())
{
Node temp = q.front();q.pop();
if(temp.s == s / 2 && (temp.n == s / 2 || temp.m == s / 2))
return temp.step;
if(temp.n == s / 2 && (temp.s == s / 2 || temp.m == s / 2))
return temp.step;
if(temp.m == s / 2 && (temp.s == s / 2 || temp.n == s / 2))
return temp.step;
//printf("%d %d %d have %d\n",temp.s,temp.n,temp.m,temp.step);
if(temp.s != 0)
{
if(temp.n != n)
{
int x = temp.s;int y = temp.n;int z = temp.m;int step = temp.step;
//printf("%d %d %d have %d\n",temp.s,temp.n,temp.m,temp.step);
if(y + x <= n)
{
y = x + y;
x = 0;
z = z;
step = step + 1;
}
else
{
x = x - (n - y);
y = n;
z = z;
step = step + 1;
}
if(hash[x][y][z] == 0)
{
Node xin;xin.s = x;xin.n = y;xin.m = z;xin.step = step;
q.push(xin);
//printf("%d %d %d have %d\n",x,y,z,step);
hash[x][y][z] = 1;
}
}
if(temp.m != m)
{
int x = temp.s;int y = temp.n;int z = temp.m;int step = temp.step;
if(z + x <= m)
{
z = x + z;
x = 0;
y = y;
step = step + 1;
}
else
{
x = x - (m - z);
z = m;
y = y;
step = step + 1;
}
if(hash[x][y][z] == 0)
{
Node xin;xin.s = x;xin.n = y;xin.m = z;xin.step = step;
q.push(xin);
//printf("%d %d %d have %d\n",x,y,z,step);
hash[x][y][z] = 1;
}
}
}
if(temp.n != 0)
{
if(temp.s != s)
{
int x = temp.s;int y = temp.n;int z = temp.m;int step = temp.step;
if(y + x <= s)
{
x = x + y;
y = 0;
z = z;
step = step + 1;
}
else
{
y = y - (s - x);
x = s;
z = z;
step = step + 1;
}
if(hash[x][y][z] == 0)
{
Node xin;xin.s = x;xin.n = y;xin.m = z;xin.step = step;
q.push(xin);
//printf("%d %d %d have %d\n",x,y,z,step);
hash[x][y][z] = 1;
}
}
if(temp.m != m)
{
int x = temp.s;int y = temp.n;int z = temp.m;int step = temp.step;
if(z + y <= m)
{
z = y + z;
y = 0;
x = x;
step = step + 1;
}
else
{
y = y - (m - z);
z = m;
x = x;
step = step + 1;
}
if(hash[x][y][z] == 0)
{
Node xin;xin.s = x;xin.n = y;xin.m = z;xin.step = step;
q.push(xin);
///printf("%d %d %d have %d\n",x,y,z,step);
hash[x][y][z] = 1;
}
}
}
if(temp.m != 0)
{
if(temp.n != n)
{
int x = temp.s;int y = temp.n;int z = temp.m;int step = temp.step;
if(y + z <= n)
{
y = z + y;
z = 0;
x = x;
step = step + 1;
}
else
{
z = z - (n - y);
y = n;
x = x;
step = step + 1;
}
if(hash[x][y][z] == 0)
{
Node xin;xin.s = x;xin.n = y;xin.m = z;xin.step = step;
q.push(xin);
//printf("%d %d %d have %d\n",x,y,z,step);
hash[x][y][z] = 1;
}
}
if(temp.s != s)
{
int x = temp.s;int y = temp.n;int z = temp.m;int step = temp.step;
if(z + x <= s)
{
x = x + z;
z = 0;
y = y;
step = step + 1;
}
else
{
z = z - (s - x);
x = s;
y = y;
step = step + 1;
}
if(hash[x][y][z] == 0)
{
Node xin;xin.s = x;xin.n = y;xin.m = z;xin.step = step;
q.push(xin);
//printf("%d %d %d have %d\n",x,y,z,step);
hash[x][y][z] = 1;
}
}
}
}
return -1;
}
int main()
{
while(scanf("%d%d%d",&s,&n,&m),s != 0 && n != 0 && m != 0)
{
memset(hash,0,sizeof(hash));
hash[s][n][m] = 1;
int sss = BFS();
if(s % 2 == 1)
sss = -1;
if(sss == -1)
printf("NO\n");
else
printf("%d\n",sss);
}
return 0;
}
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int hash[110][110][110];
int s,n,m;
struct Node
{
int s,n,m;
int step;
};
int BFS()
{
queue <Node> q;
Node top;top.s = s;top.n = 0;top.m = 0;top.step = 0;
q.push(top);
while(!q.empty())
{
Node temp = q.front();q.pop();
if(temp.s == s / 2 && (temp.n == s / 2 || temp.m == s / 2))
return temp.step;
if(temp.n == s / 2 && (temp.s == s / 2 || temp.m == s / 2))
return temp.step;
if(temp.m == s / 2 && (temp.s == s / 2 || temp.n == s / 2))
return temp.step;
//printf("%d %d %d have %d\n",temp.s,temp.n,temp.m,temp.step);
if(temp.s != 0)
{
if(temp.n != n)
{
int x = temp.s;int y = temp.n;int z = temp.m;int step = temp.step;
//printf("%d %d %d have %d\n",temp.s,temp.n,temp.m,temp.step);
if(y + x <= n)
{
y = x + y;
x = 0;
z = z;
step = step + 1;
}
else
{
x = x - (n - y);
y = n;
z = z;
step = step + 1;
}
if(hash[x][y][z] == 0)
{
Node xin;xin.s = x;xin.n = y;xin.m = z;xin.step = step;
q.push(xin);
//printf("%d %d %d have %d\n",x,y,z,step);
hash[x][y][z] = 1;
}
}
if(temp.m != m)
{
int x = temp.s;int y = temp.n;int z = temp.m;int step = temp.step;
if(z + x <= m)
{
z = x + z;
x = 0;
y = y;
step = step + 1;
}
else
{
x = x - (m - z);
z = m;
y = y;
step = step + 1;
}
if(hash[x][y][z] == 0)
{
Node xin;xin.s = x;xin.n = y;xin.m = z;xin.step = step;
q.push(xin);
//printf("%d %d %d have %d\n",x,y,z,step);
hash[x][y][z] = 1;
}
}
}
if(temp.n != 0)
{
if(temp.s != s)
{
int x = temp.s;int y = temp.n;int z = temp.m;int step = temp.step;
if(y + x <= s)
{
x = x + y;
y = 0;
z = z;
step = step + 1;
}
else
{
y = y - (s - x);
x = s;
z = z;
step = step + 1;
}
if(hash[x][y][z] == 0)
{
Node xin;xin.s = x;xin.n = y;xin.m = z;xin.step = step;
q.push(xin);
//printf("%d %d %d have %d\n",x,y,z,step);
hash[x][y][z] = 1;
}
}
if(temp.m != m)
{
int x = temp.s;int y = temp.n;int z = temp.m;int step = temp.step;
if(z + y <= m)
{
z = y + z;
y = 0;
x = x;
step = step + 1;
}
else
{
y = y - (m - z);
z = m;
x = x;
step = step + 1;
}
if(hash[x][y][z] == 0)
{
Node xin;xin.s = x;xin.n = y;xin.m = z;xin.step = step;
q.push(xin);
///printf("%d %d %d have %d\n",x,y,z,step);
hash[x][y][z] = 1;
}
}
}
if(temp.m != 0)
{
if(temp.n != n)
{
int x = temp.s;int y = temp.n;int z = temp.m;int step = temp.step;
if(y + z <= n)
{
y = z + y;
z = 0;
x = x;
step = step + 1;
}
else
{
z = z - (n - y);
y = n;
x = x;
step = step + 1;
}
if(hash[x][y][z] == 0)
{
Node xin;xin.s = x;xin.n = y;xin.m = z;xin.step = step;
q.push(xin);
//printf("%d %d %d have %d\n",x,y,z,step);
hash[x][y][z] = 1;
}
}
if(temp.s != s)
{
int x = temp.s;int y = temp.n;int z = temp.m;int step = temp.step;
if(z + x <= s)
{
x = x + z;
z = 0;
y = y;
step = step + 1;
}
else
{
z = z - (s - x);
x = s;
y = y;
step = step + 1;
}
if(hash[x][y][z] == 0)
{
Node xin;xin.s = x;xin.n = y;xin.m = z;xin.step = step;
q.push(xin);
//printf("%d %d %d have %d\n",x,y,z,step);
hash[x][y][z] = 1;
}
}
}
}
return -1;
}
int main()
{
while(scanf("%d%d%d",&s,&n,&m),s != 0 && n != 0 && m != 0)
{
memset(hash,0,sizeof(hash));
hash[s][n][m] = 1;
int sss = BFS();
if(s % 2 == 1)
sss = -1;
if(sss == -1)
printf("NO\n");
else
printf("%d\n",sss);
}
return 0;
}