USACO 2016 February Contest, Silver - Milk Pails
Problem Link
Tags: USACO, BFS, graphTime Complexity: O(XY)
I learnt the way to solve this problem using BFS from here.
In this problem, there is M (the order quantity), two pails X and Y, and K(the number of moves available). By using at most K moves, we need to find the minimum difference in M and the achievable milk quantity using pails X and Y, in the ways described in the problem.
Even though this may not seem like a graph problem, guess what? It is.
When pail X has x amount of milk and pail Y has y amount of milk, we call this situation a state. A state can be written as s[x][y]. For example, if pail X has 2 units of milk and pail Y has 5 units of milk, the state is s[2][5].
The first state is s[0][0], since we start out with both pails having 0 units of milk. From here, we can reach s[X][0] or s[0][Y] by filling either pail to the top. Treat the different states as nodes in a graph. Since the two states can be obtained from s[0][0], we can draw an edge between the two nodes (s[X][0] and s[0][Y]) and the original node (s[0][0]).
Likewise, we can build additional states by following the different operations in the problem. There are a total of 6 states can can be obtained from a node, although sometimes achieving all 6 states from a node may not be possible (for example: only 2 states can be obtained from s[0][0]) -
- Fill pail X
- Fill pail Y
- Empty pail X
- Empty pail Y
- Pour X into Y
- Pour Y into X
Comments