Special coke HDU - 1495

Posted by benjrox on Tue, 03 Dec 2019 11:34:26 +0100

Special coke HDU - 1495

You must feel that drinking coke after exercise is a very comfortable thing, but see you don't think so. Because every time seeyou bought a coke, a Niu asked to share this coke with seeyou, and he must drink as much as seeyou. However, there are only two cups in seeyou's hand. Their capacity is N ml and M ml respectively. The volume of coke is s (s < 101) ml (just filling a bottle). The three cups can pour coke to each other (all without scale, and S==N+M, 101 > s > 0, N > 0, M > 0) zh. Smart ACMER do you think they can share equally? If you can output the minimum number of times to pour coke, if you can't output "NO".
Input
Three integers: the volume of S cola, N and M are the capacity of two cups, ending with "0.00".
Output
If it can be divided equally, please output the minimum number of times to be inverted, otherwise output "NO".

Sample Input

7 4 3
4 1 3
0 0 0

Sample Output

NO
3
This problem was originally done with dfs, traversing all the results to find the minimum value, but it was wrong. So it's changed to bfs. At the beginning, i was used to judge six situations. It's very troublesome. After writing, there are bug s and Baidu code. It's really wonderful to see others use two for loops to represent each situation. i learned another way.
See code:

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int c[3];
int n,m,ans;
bool vis[105][105][105];
int half;
struct node{
	int b[3];
	int step;
};

void bfs()
{
	node cur,next;
	queue<node>q;
	cur.b[0]=c[0];
	cur.b[1]=0;
	cur.b[2]=0;
	cur.step=0;
	q.push(cur);
	vis[c[0]][0][0]=true;
	while(!q.empty())
	{
		cur=q.front();
		q.pop();
		for(int i=0;i<3;i++)
		{
			if(cur.b[i]>0)
			for(int j=0;j<3;j++)//i cup pour water to j cup 
			{
				next=cur;
				if(i==j)	continue;
				if(next.b[i]>(c[j]-next.b[j]))//If you can fill it up 
				{
					next.b[i]-=c[j]-next.b[j];
					next.b[j]=c[j];
				}
				else//Dissatisfied 
				{
					next.b[j]+=next.b[i];
					next.b[i]=0;
				}
				if(!vis[next.b[0]][next.b[1]][next.b[2]])
				{
					vis[next.b[0]][next.b[1]][next.b[2]]=true;
					next.step=cur.step+1;
					if((next.b[0]==half&&next.b[1]==half)||(next.b[0]==half&&next.b[2]==half)||(next.b[1]==half&&next.b[2]==half))
					{
						cout<<next.step<<endl;
						return;
					}
					q.push(next);
				}
			}
		}
	}
	cout<<"NO"<<endl;
	return;
}

int main()
{
	while(cin>>c[0]>>c[1]>>c[2],c[0]+c[1]+c[2])
	{
		memset(vis,0,sizeof(vis));
		half=c[0]/2;
		if(c[0]%2!=0)
		{
			cout<<"NO"<<endl;
		}
		else 
		bfs();
	}
	return 0;
}