RainX_69

JUG FILL PROBLEM USING BFS

Dec 16th, 2022
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.36 KB | Source Code | 0 0
  1. class Solution {
  2. public:
  3.     bool canMeasureWater(int J1, int J2, int T) {
  4.         // Check for invalid inputs
  5.         if (J1 + J2 < T) {
  6.             return false;
  7.         }
  8.  
  9.         // Create a queue to hold the current state of the jugs
  10.         queue<pair<int, int>> q;
  11.         // Push the initial state of the jugs (both empty) onto the queue
  12.         q.push({0, 0});
  13.         // Create a set to hold the visited states
  14.         set<pair<int, int>> visited;
  15.         // Mark the initial state as visited
  16.         visited.insert({0, 0});
  17.  
  18.         // While the queue is not empty
  19.         while (!q.empty()) {
  20.             // Get the current state of the jugs
  21.             auto currState = q.front();
  22.             q.pop();
  23.  
  24.             // If either jug has T units of water, return true
  25.             if (currState.first == T || currState.second == T || currState.first+currState.second==T) {
  26.                 return true;
  27.             }
  28.  
  29.             // Fill the first jug
  30.             if (visited.find({J1, currState.second}) == visited.end()) {
  31.                 q.push({J1, currState.second});
  32.                 visited.insert({J1, currState.second});
  33.             }
  34.  
  35.             // Fill the second jug
  36.             if (visited.find({currState.first, J2}) == visited.end()) {
  37.                 q.push({currState.first, J2});
  38.                 visited.insert({currState.first, J2});
  39.             }
  40.  
  41.             // Empty the first jug
  42.             if (visited.find({0, currState.second}) == visited.end()) {
  43.                 q.push({0, currState.second});
  44.                 visited.insert({0, currState.second});
  45.             }
  46.  
  47.             // Empty the second jug
  48.             if (visited.find({currState.first, 0}) == visited.end()) {
  49.                 q.push({currState.first, 0});
  50.                 visited.insert({currState.first, 0});
  51.             }
  52.  
  53.             // Pour water from the first jug into the second jug
  54.             int SpaceInJ2 = J2 - currState.second;
  55.             if (currState.first >= SpaceInJ2) {
  56.                 int remainingInJ1 = currState.first - SpaceInJ2;
  57.                 if (visited.find({remainingInJ1, J2}) == visited.end()) {
  58.                     q.push({remainingInJ1, J2});
  59.                     visited.insert({remainingInJ1, J2});
  60.                 }  
  61.             }
  62.             else if (visited.find({0, currState.first + currState.second}) == visited.end()) { // Not enough water in J2 to fill completely
  63.                     q.push({0, currState.first + currState.second});
  64.                     visited.insert({0, currState.first + currState.second});
  65.             }
  66.  
  67.             // Pour water from the second jug into the first jug
  68.             int SpaceInJ1 = J1 - currState.first;
  69.             if (currState.second >= SpaceInJ1) {
  70.                 int remainingInJ2 = currState.second - SpaceInJ1;
  71.                 if (visited.find({J1, remainingInJ2}) == visited.end()) {
  72.                     q.push({J1, remainingInJ2});
  73.                     visited.insert({J1, remainingInJ2});
  74.                 }  
  75.             }
  76.             else if (visited.find({currState.first + currState.second, 0}) == visited.end()) { // Not enough water in J1 to fill completely
  77.                     q.push({currState.first + currState.second, 0});
  78.                     visited.insert({currState.first + currState.second, 0});
  79.             }
  80.         }
  81.         return false;
  82.     }
  83. };
  84.  
  85.  
Advertisement
Add Comment
Please, Sign In to add comment