nyoj 21 three water cups (simulated bfs)

Three water glasses

Time limit: 1000 ms | memory limit: 65535 KB
Difficulty: 4
describe Give three glasses of different sizes, and only the largest one is full, the other two are empty. The three water cups pour water into each other, and the water cup has no mark, so it can only be calculated according to the given volume of the water cup. Now you are asked to write a program that outputs the minimum number of times the initial state reaches the target state.
input An integer N (0 < N < 50) in the first row indicates the test data of group N
Next, each group of test data has two lines. The first line gives three integers V1 V2 V3 (V1 > V2 > V3 V1 < 100 V3 > 0), indicating the volume of three water cups.
The second line gives three integers E1 E2 E3 (the volume is less than or equal to the corresponding water cup volume) to represent the final state we need output Output the minimum number of pour times of corresponding test data for each line. If the target state output-1 is not reached sample input
6 3 1
4 1 1
9 3 2
7 1 1
sample output

Idea: there are six states (three water cups pour water into each other) in each cycle. Each time, select the smaller one from the maximum capacity a[i] that can be reached by the water pouring cup and the maximum capacity v[j]-a[j] that can be filled by the water pouring cup, and then check whether the state d[a[0]][a[1]][a[2]] has been accessed. If not, join the queue, and no matter if it has been visited.

AC Code:

using namespace std;
const int maxn = 100 + 2;
int d[maxn][maxn][maxn];
struct node{
	int cnt; //So far, how many times to pour water 
	int a,b,c;  //Current capacity of three cups 
	node(int a,int b,int c,int cnt):a(a),b(b),c(c),cnt(cnt){}
	bool operator < (const node& nd)const {   //The first out queue with the smaller cnt value 
	     return this->cnt > nd.cnt;
} ns,ne;

int v[3];//Cup capacity 

void bfs(){
	d[ns.a][ns.b][ns.c] = 1;  //This state has been accessed 
	priority_queue<node> q;
		node u = q.top(); q.pop();
		if(u.a == ne.a && u.b == ne.b && u.c == ne.c){
			return ;

		for(int i = 0; i < 3; i++){	//A cup for pouring water out 
			for(int j = 0; j < 3; j++){  //A cup into which water is poured 
				int a[3]={u.a,u.b,u.c};	
				if(i != j){
				   int Min = min(a[i],v[j]-a[j]);  //Choose the smaller one from the cup with the capacity of a[i] and the cup with the capacity of v[j]-a[j] 
				   a[i] -= Min;
				   a[j] += Min;					
					d[a[0]][a[1]][a[2]] = 1;
					q.push(node(a[0],a[1],a[2],u.cnt + 1));

int main(){
	int n;
		scanf("%d%d%d",&v[0],&v[1],&v[2]);  //Cup capacity 
		scanf("%d%d%d",&ne.a,&ne.b,&ne.c);  //Target status 
		ns.a = v[0] ,ns.b = 0, ns.c = 0, ns.cnt = 0;
		if((ne.a + ne.b + ne.c) > v[0]){
			printf("-1\n"); continue;
	return 0;

Tags: less

Posted on Sun, 03 May 2020 06:15:42 -0700 by webbyboy