USACO 1.5 Mother's Milk

Posted by softsolvers on Sat, 01 Feb 2020 06:25:57 +0100

The main idea of the topic

There are three milk buckets a, b and c. the capacity of each milk bucket is an integer of 1 to 20. At the beginning, a, b are empty, c are full, and they can pour each other. When a bucket is empty, how about the volume of milk in c bucket?

 

S amp le input & output

sample input 1

8 9 10

sample output 1

1 2 8 9 10

sample input 2

2 5 10

sample output 2

5 6 7 8 9 10

 

Analysis & Reflection

Continue to help recall a question about dfs.

1. Deduce the state, judge the state and trace back.

2. Compared with bfs, authorities are the same, de duplication and a bit of hashing.

3. When you output the answer, you forget 0, which is a very easy point to forget.

 

Code

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

int a, b, c, atop, btop, ctop;
int ans[22], vis[22][22]; 

void dfs(int x, int y) {
	if(vis[x][y]) return;
	vis[x][y] = 1;
	
	if(!x) ans[ctop - b]++;
	
	
	int cha, dao;
	{
		cha = btop - b;
		dao = min(a, cha);
		a -= dao;
		b += dao;
		dfs(a, b);
		a += dao;
		b -= dao;
		
	}
	{
		cha = atop - a;
		dao = min(b, cha);
		b -= dao;
		a += dao;
		dfs(a, b);
		b += dao;
		a -= dao;
		
	}
	{
		cha = ctop - c;
		dao = min(a, cha);
		a -= dao;
		c += dao;
		dfs(a, b);
		a += dao;
		c -= dao;
		
	}
	{
		cha = atop - a;
		dao = min(c, cha);
		c -= dao;
		a += dao;
		dfs(a, b);
		c += dao;
		a -= dao;
		
	}
	{
		cha = btop - b;
		dao = min(c, cha);
		c -= dao;
		b += dao;
		dfs(a, b);
		c += dao;
		b -= dao;
		
	}
	{
		cha = ctop - c;
		dao = min(b, cha);
		b -= dao;
		c += dao;
		dfs(a, b);
		b += dao;
		c -= dao;
		
	}
	return ;
}

int anss[30], tot;
int main() {
	
	freopen("milk3.in", "r", stdin);
	freopen("milk3.out", "w", stdout);
	
	cin >> atop >> btop >> ctop;
	c = ctop;
	
	dfs(a, b);
	
	for(int i = 0; i <= 20; i++) 
		if(ans[i]) anss[++tot] = i;
	for(int i = 1; i < tot; i++) cout << anss[i] << " ";
	cout << anss[tot]  << endl;
		
	return 0;
}

Remarks

Finally, we need to add a transfer line