【HDU-OJ】1495_非常可乐

传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1495

题目

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

思路(同学教的)

用BFS,每倒一次,记录一下倒水的最小次水 ,当第一次达到平均状态时,就是次数最小。
用一个结构体{a,b,c,t}分别表示s,m,n中的可乐体积 t表示第一次出现(a,b,c)这个状态时的最小次数。当a,b,c中任意两个=s/2时,就是平均
从(s,0,0,0)这个状态开始搜索。一共有六种可能的情况:s->m m->s s->n n->s m->n n->m

代码

#include <iostream>
#include <queue>
#include <algorithm>

using namespace std; 
/* http://acm.hdu.edu.cn/showproblem.php?pid=1495 */

typedef struct Stat {
	int a, b, c;	// 分别表示容量为s,n,m三个杯中的体积 
	int t;			// 从初始状态开始的倾倒次数 
}Stat; 


bool mark[101][101][101];	// mark[a][b][c]表示状态 Stat(a, b, c)是否出现过 
int s, n, m;				// 三个杯子容量 

int bfs(int a, int b, int c, queue<Stat>& Q) {
	int need = 0; 
	while (!Q.empty()) {
		Stat statCur = Q.front();
		Q.pop();
		a = statCur.a;
		b = statCur.b;
		c = statCur.c;
		// 一共有六种可能的情况:s->n  n->s  s->m   m->s  n->m   m->n
		
		// s->n倒可乐
		need = min(n-b, a);
		b += need;
		a -= need; 
		
		if (mark[a][b][c] == false) {  // 重复的就不用管了 
			mark[a][b][c] = true;
			
			Stat temp;
			temp.a = a;
			temp.b = b;
			temp.c = c;
			temp.t = statCur.t+1;
			
			
			// 看是否达到平分要求
			if( (a==s/2 && b==s/2) || (a==s/2 && c==s/2) || (b==s/2 && c==s/2))
			{
				return temp.t;
			}
			Q.push(temp);
		}
		
		a = statCur.a;
		b = statCur.b;
		c = statCur.c;
		// n->s倒可乐
		need = min(s-a, b);
		a += need;
		b -= need; 
		
		if (mark[a][b][c] == false) {  // 重复的就不用管了 
			mark[a][b][c] = true;
			
			Stat temp;
			temp.a = a;
			temp.b = b;
			temp.c = c;
			temp.t = statCur.t+1;
			
			
			// 看是否达到平分要求
			if( (a==s/2 && b==s/2) || (a==s/2 && c==s/2) || (b==s/2 && c==s/2))
			{
				return temp.t;
			}
			Q.push(temp);
		} 
		
		a = statCur.a;
		b = statCur.b;
		c = statCur.c;
		// s->m倒可乐
		need = min(m-c, a);
		c += need;
		a -= need; 
		
		if (mark[a][b][c] == false) {  // 重复的就不用管了 
			mark[a][b][c] = true;
			
			Stat temp;
			temp.a = a;
			temp.b = b;
			temp.c = c;
			temp.t = statCur.t+1;
			
			
			// 看是否达到平分要求
			if( (a==s/2 && b==s/2) || (a==s/2 && c==s/2) || (b==s/2 && c==s/2))
			{
				return temp.t;
			}
			Q.push(temp);
		}
		
		a = statCur.a;
		b = statCur.b;
		c = statCur.c;
		// m->s倒可乐
		need = min(s-a, c);
		a += need;
		c -= need; 
		
		if (mark[a][b][c] == false) {  // 重复的就不用管了 
			mark[a][b][c] = true;
			
			Stat temp;
			temp.a = a;
			temp.b = b;
			temp.c = c;
			temp.t = statCur.t+1;
			
			
			// 看是否达到平分要求
			if( (a==s/2 && b==s/2) || (a==s/2 && c==s/2) || (b==s/2 && c==s/2))
			{
				return temp.t;
			}
			Q.push(temp);
		} 
		
		
		
		a = statCur.a;
		b = statCur.b;
		c = statCur.c;
		// n->m倒可乐
		need = min(m-c, b);
		c += need;
		b -= need; 
		
		if (mark[a][b][c] == false) {  // 重复的就不用管了 
			mark[a][b][c] = true;
			
			Stat temp;
			temp.a = a;
			temp.b = b;
			temp.c = c;
			temp.t = statCur.t+1;
			
			
			// 看是否达到平分要求
			if( (a==s/2 && b==s/2) || (a==s/2 && c==s/2) || (b==s/2 && c==s/2))
			{
				return temp.t;
			}
			Q.push(temp);
		}
		
		a = statCur.a;
		b = statCur.b;
		c = statCur.c;
		// m->n倒可乐
		need = min(n-b, c);
		b += need;
		c -= need; 
		
		if (mark[a][b][c] == false) {  // 重复的就不用管了 
			mark[a][b][c] = true;
			
			Stat temp;
			temp.a = a;
			temp.b = b;
			temp.c = c;
			temp.t = statCur.t+1;
			
			
			// 看是否达到平分要求
			if( (a==s/2 && b==s/2) || (a==s/2 && c==s/2) || (b==s/2 && c==s/2))
			{
				return temp.t;
			}
			Q.push(temp);
		} 
		
	}
	return -1;
}

int main(int argc, char** argv) {
	while (scanf("%d %d %d", &s, &n, &m) != EOF) {
		if (s == 0) {
			break;
		}
		// 奇数无法平分 
		if (s%2 == 1) {
			printf("NO\n"); 
			continue;
		}
		for(int i = 0; i <= s; i++)
		{
			for(int j = 0; j <= n; j++)
			{
				for(int k = 0; k <= m; k++)
				{
					mark[i][j][k] = false;
				}
			}
		}
		
		
		Stat statInit;
		statInit.a = s;
		statInit.b = 0;
		statInit.c = 0;
		statInit.t = 0;
		mark[s][0][0] = true;
		queue<Stat> Q;				// 用于bfs的队列 
		Q.push(statInit);
		
		int ans = bfs(s, n, m, Q);
		if (ans == -1) {
			printf("NO\n");
		}
		else {
			printf("%d\n", ans);
		}
	} 
	
	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值