Similar Problems

Similar Problems not available

Water Jug problem using BFS - Leetcode Solution

LeetCode:  Water Jug problem using BFS Leetcode Solution

Difficulty: Unknown

Topics: graphs  

Problem Statement

Given two jugs with J1 and J2 litres of capacities which are initially empty. Using these jugs you have to measure L litres of water (L < J2).
The (x, y) is the state where x and y are the amount of water in J1 and J2 respectively.

The task is to find the path from initial state (0, 0) to final state (d, 0) or (0, d).

Operations that can performed are as follows:

  1. Fill any of the jugs completely with water, like (x, y)->(0, y).
  2. Empty any of the jugs, (0, 0)->(x, 0).
  3. Pour water from one jug into another till the other jug is completely full or the first jug itself is empty, (x, y) -> (x-d, y+d).

Example Input

J1 = 2, J2 = 5 , L = 4

Expected Output

Path is as Follow:

(0, 0)

(0, 5)

(2, 0)

(2, 5)

(2, 3)

(0, 2)

(2, 2)

(0, 4)

Approach

The Approach is to use memoization and recursion.

This approach do the following, using Recursion, visit all the given operations one by one until condition satisfied and true value is returned. For every return value, it is stored using memoization. Memorisation is done to stop the recursive calling.

Water Jug problem using BFS Solution Code

1