您的位置:首页 > 其它

[bfs] HDU 1495 非常可乐

2015-07-27 15:11 471 查看
M - 非常可乐

Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u

Submit

Status

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

一看到说是输出最少要倒的次数,就知道是用bfs做了。用结构体存储每次三个瓶子的状态以及记录步数,用一个三元数组vis[][][]来存储已出现过的情况。符合要求就可以对相应的情况输出得到最优解,每次状态转移有6个方向:



我们把倒出可乐的瓶子称为第一个瓶子,倒入可乐的瓶子为第二个瓶子,在往六个方向转移的过程中,每个倒可乐的过程有两种可能性:

1、第一个瓶子能把第二个瓶子倒满,第一个瓶子有剩余。

2、第一个瓶子全部倒空了,第二个瓶子没有倒满。

对两种不同的情况加以处理即可。

当三个瓶子中有两个瓶子可乐数相等。且另一个瓶子是空的,就可以结束搜索得到答案。

虽然bfs的方向很多,但是逻辑是完全相同的,并不难写。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>

using namespace std;
int s,n,m;
int vis[105][105][105];
struct node{
int s,n,m; //s,n,m分别为第一个,第二个,第三个
int step;
};
bool check(int s,int n,int m)
{
if ((s==m && n ==0) || (s==n && m==0) || (n==m && s==0))
return 1;
else return 0;
}

int bfs()
{
queue<node> Q;
node a,next;
a.s = s,a.n=0,a.m=0;
a.step = 0;
vis[s][0][0] = 1;
Q.push(a);
while(!Q.empty())
{
a = Q.front();
Q.pop();
if(check(a.s,a.n,a.m))
return a.step;
if(a.s) //处理s中的可乐
{
//s->n
if(a.s > n-a.n) //当s瓶中的容量大于n中剩余容量的,用s把n倒满
{
next = a;
next.s = next.s - (n-a.n);
next.n = n;
if(!vis[next.s][next.n][next.m])
{
next.step = a.step+1;
Q.push(next);
vis[next.s][next.n][next.m] = 1;
}
}
else //s不能把n倒满,只能全部倒过去
{
next = a;
next.n = next.s + next.n;
next.s = 0;
if (!vis[next.s][next.n][next.m])
{
next.step = a.step+1;
Q.push(next);
vis[next.s][next.n][next.m] = 1;
}
}
//s->m
if(a.s > m - a.m)//当s瓶中的容量大于m中剩余容量的,用s把m倒满
{
next = a;
next.s = next.s - (m-a.m);
next.m = m;
if (!vis[next.s][next.n][next.m])
{
next.step = a.step+1;
Q.push(next);
vis[next.s][next.n][next.m] = 1;
}
}
else //s不能把m倒满,只能全部倒过去
{
next = a;
next.m = next.m + next.s;
next.s = 0;
if (!vis[next.s][next.n][next.m])
{
next.step = a.step+1;
Q.push(next);
vis[next.s][next.n][next.m] = 1;
}
}
}
//处理n中的可乐
if(a.n)
{
if(a.n>s-a.s) //n->s 把s倒满了
{
next = a;
next.n = next.n - (s-a.s);
next.s = s;
if(!vis[next.s][next.n][next.m])
{
next.step = a.step +1;
Q.push(next);
vis[next.s][next.n][next.m] = 1;
}
}
else //s没倒满
{
next = a;
next.s = next.n + next.s;
next.n = 0;
if(!vis[next.s][next.n][next.m])
{
next.step = a.step +1;
Q.push(next);
vis[next.s][next.n][next.m] = 1;
}
}
if(a.n > m - a.m) //n->m 倒满了
{
next = a;
next.n = next.n-(m-a.m);
next.m = m;
if(!vis[next.s][next.n][next.m])
{
next.step = a.step +1;
Q.push(next);
vis[next.s][next.n][next.m] = 1;
}
}
else //没倒满
{
next = a;
next.m = next.m+next.n;
next.n = 0;
if(!vis[next.s][next.n][next.m])
{
next.step = a.step +1;
Q.push(next);
vis[next.s][next.n][next.m] = 1;
}
}
}
if(a.m) //处理s
{
//m->s
if (a.m > s-a.s) //把s倒满了
{
next = a;
next.m = next.m - (s-a.s);
next.s = s;
if(!vis[next.s][next.n][next.m])
{
next.step = a.step +1;
Q.push(next);
vis[next.s][next.n][next.m] = 1;
}
}
else //s没倒满
{
next = a;
next.s = next.m+next.s;
next.m = 0;
if(!vis[next.s][next.n][next.m])
{
next.step = a.step +1;
Q.push(next);
vis[next.s][next.n][next.m] = 1;
}
}
//m->n
if(a.m > n-a.n)
{
next = a;
next.m = next.m-(n-a.n);
next.n = n;
if(!vis[next.s][next.n][next.m])
{
next.step = a.step +1;
Q.push(next);
vis[next.s][next.n][next.m] = 1;
}
}
else
{
next= a;
next.n = next.m +next.n;
next.m = 0;
if(!vis[next.s][next.n][next.m])
{
next.step = a.step +1;
Q.push(next);
vis[next.s][next.n][next.m] = 1;
}
}
}
}
return 0;
}

int main()
{
// freopen("in.txt","r",stdin);
int ans;
while(scanf("%d%d%d",&s,&n,&m)!=EOF)
{
if (s==0 && n==0 && m==0) break;
if(s&1)//奇数肯定不能平分,因为被子是整数体积大小
{
printf("NO\n");
continue;
}
memset(vis,0,sizeof(vis));
ans = bfs();
if(ans)
printf("%d\n",ans);
else
printf("NO\n");
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: