非常可乐题解

思路:感觉就是bfs,但是状态的转换比较复杂 ,而且要注意这里的平分是指两个杯子的水都为n/2。

用结构体存我的三杯水和消耗次数。

可以两重循环,第一重判断要给水的被子,第二重循环被给水的杯子

然后不断给水,注意st的转换,其实好像也就那样,主要是有点绕,思路清晰就好了。

/**
*  ┏┓   ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃       ┃
* ┃   ━   ┃ ++ + + +
*  ████━████+
*  ◥██◤ ◥██◤ +
* ┃   ┻   ┃
* ┃       ┃ + +
* ┗━┓   ┏━┛
*   ┃   ┃ + + + +Code is far away from  
*   ┃   ┃ + bug with the animal protecting
*   ┃    ┗━━━┓ 神兽保佑,代码无bug 
*   ┃  	    ┣┓
*    ┃        ┏┛
*     ┗┓┓┏━┳┓┏┛ + + + +
*    ┃┫┫ ┃┫┫
*    ┗┻┛ ┗┻┛+ + + +
*/

#include<cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include<vector>
#include<queue>
#include<map>
#define sc_int(x) scanf("%d", &x)
#define sc_ll(x) scanf("%lld", &x)
#define pr_ll(x) printf("%lld", x)
#define pr_ll_n(x) printf("%lld\n", x)
#define pr_int_n(x) printf("%d\n", x)
#define ll long long 
using namespace std;

const int N=1000000+100;
int n ,m,h;
ll s[N];

bool st[110][110][110];

struct lk{
	int g[3];
	int cnt;
}f,g,z;


void bfs(int half)
{
	queue<lk> q;
	f.g[0]=s[0],f.g[1]=f.g[2]=0;
	f.cnt=0;
	q.push(f);
	while(q.size())
	{
		g=q.front();
		q.pop();
		int X=g.g[0],Y=g.g[1],Z=g.g[2];
		if((X==Y&&X==half)||(X==Z&&X==half)||(Y==Z&&Y==half) )//判断一下
		{
			cout<<g.cnt<<endl;
			return ;
		}		
		for(int i =0;i<3;i++)//模拟给的杯子
		{
			if(!g.g[i])continue;
			for(int j =0;j<3;j++)//被给的杯子
			{
				if(i==j)continue;
				z=g;//赋值
				if(z.g[i]>s[j]-z.g[j]){
					z.g[i]-=(s[j]-z.g[j]);
					z.g[j]=s[j];
				}
				else{
					z.g[j]+=z.g[i];
					z.g[i]=0;
				}
				if(!st[z.g[0]][z.g[1]][z.g[2]])//是否被标记
				{
					st[z.g[0]][z.g[1]][z.g[2]]=1;
					z.cnt++;

					q.push(z);
				}				
			}
		}
	}
	cout<<"NO\n";
	return ;
}

int main()
{
	int t;
	while(~scanf("%d%d%d",&s[0],&s[1],&s[2] ))
	{
		if(!s[0]&&!s[1]&&!s[2])break;
		memset(st,0,sizeof st);
		st[s[0]][0][0]=1;
		if(s[0]%2!=0)
		{
			cout<<"NO\n";
			continue;
		}
		bfs(s[0]/2);

	}
	
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值