kuangbin topic one simple search M - very cola

M - Very Coke

Everyone must think that drinking Coke after exercising is a very pleasant thing, but seeyou doesn't think so. Because every time seeyou buys Coke, Aniu asks to share this bottle of Coke with seeyou, and he must drink as much as seeyou. But seeyou only has two cups in his hand, their capacities are N ml and M ml respectively. The volume of cola is S (S<101) ml (just fill a bottle), and the three of them can pour cola into each other (both are Unscaled, and S==N+M, 101>S>0, N>0, M>0). Smart ACMER, do you say they can be divided equally? If you can, please output the minimum number of times to pour Coke, if not, output "NO".
Input three integers: S is the volume of the cola, N and M are the capacities of the two cups, ending with "0 0 0". Output If it can be divided equally, please output the minimum number of times to be poured, otherwise output "NO". Sample Input
7 4 3
4 1 3
0 0 0
Sample Output
NO
3

This question is that bfs does not record the path, and the starting position is judged to be odd or even. This should be called pruning. . . . , but dx and dy are not the same as before. This time, dx and dy need to judge whether they are full. . .

#include<iostream>
#include<string.h>
#include<queue>
using namespace std ;
typedef long long ll;
typedef unsigned long long ull;
const int _max =150;
int V[3],half;
bool vis[_max][_max][_max];
struct node{
	int w[3];
	int t;
};
void bfs( ){
	queue <node> q;
	node now,next;
	int n,m;
	now.w[1]=now.w[2]=0;
	now.w[0]=V[0];
	now.t=0;
	vis [ now.w[0] ][ now.w[1] ][ now.w[2] ] = 0 ;
	q.push(now);
	while( !q.empty() ){
		now = q.front() ;
		q.pop() ;
		for( int i = 0 ; i < 3 ; i++ ){
			if( now.w[i] > 0 ){
				for( int j = 0 ; j < 3 ; j++){
				next = now;
				if( i == j ) continue;
				if( next.w[i] > V[j] - next.w[j] ){
					next.w[i] -= V[j] -next.w[j] ;
					next.w[j] = V[j] ;
				}
				else{
					next.w[j] += next.w[i] ;
					next.w[i] = 0 ;
				}
				if(vis[next.w[0]][next.w[1]][next.w[2]]){
					vis[next.w[0]][next.w[1]][next.w[2]]=0;
					next.t ++ ;
					if( ( next.w[0] == half && next.w[1] == half ) || ( next.w[0] == half && next.w[2] == half ) || ( next.w[1] == half && next.w[2] == half ) ){
						cout << next.t << endl ;
						return ;
					}
					q.push(next);
				}
			}
		  }
		}
	}
	cout << "NO" << endl ;
}
intmain()
{
	while(cin>>V[0]>>V[1]>>V[2]&&V[0]+V[1]+V[2]!=0){
		memset(vis,1,sizeof(vis));
		half=V[0]/2;
		if( V[0]%2 ) cout << "NO" << endl ;
		else
		bfs();
	}
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324787104&siteId=291194637