0% found this document useful (0 votes)
21 views2 pages

Program - 5 (Water Jug)

asd

Uploaded by

testppapa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views2 pages

Program - 5 (Water Jug)

asd

Uploaded by

testppapa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1 // WATER JUG PROGRAM //

2 from collections import deque


3
4 # Define the capacity of Jug A and Jug B
5 capacity_a = 4 # Capacity of Jug A
6 capacity_b = 3 # Capacity of Jug B
7
8 # Define the goal amount of water to measure out
9 goal_amount = 2 # We want to measure out 2 units of water
10
11 # Define a function to perform BFS to find the solution
12 def bfs_jugs():
13 # Queue to store states and paths
14 queue = deque()
15 # Set to keep track of visited states
16 visited = set()
17
18 # Initial state: both jugs are empty
19 initial_state = (0, 0)
20 queue.append((initial_state, []))
21 visited.add(initial_state)
22
23 while queue:
24 current_state, path = queue.popleft()
25 a, b = current_state
26
27 # Check if we have reached the goal state
28 if a == goal_amount or b == goal_amount:
29 # Print the solution path
30 print("Solution found!")
31 print(f"Initial State: Jug A={capacity_a}, Jug B={capacity_b},
Goal={goal_amount}")
32 print("Solution Path:")
33 print(f"Start from (0, 0)")
34 for i, step in enumerate(path):
35 print(f"Step {i + 1}: {step}")
36 print(f"Step {len(path) + 1}: Reach (x, {goal_amount})")
37 return
38
39 # Generate next states
40 next_states = []
41
42 # Fill Jug A
43 next_states.append((capacity_a, b, f"Fill Jug A ({capacity_a}, {b})"))
44
45 # Fill Jug B
46 next_states.append((a, capacity_b, f"Fill Jug B ({a}, {capacity_b})"))
47
48 # Empty Jug A
49 next_states.append((0, b, f"Empty Jug A (0, {b})"))
50
51 # Empty Jug B
52 next_states.append((a, 0, f"Empty Jug B ({a}, 0)"))
53
54 # Pour water from Jug A to Jug B
55 pour_amount = min(a, capacity_b - b)
56 next_states.append((a - pour_amount, b + pour_amount, f"Pour from Jug A to Jug B
({a}, {b}) -> ({a - pour_amount}, {b + pour_amount})"))
57
58 # Pour water from Jug B to Jug A
59 pour_amount = min(b, capacity_a - a)
60 next_states.append((a + pour_amount, b - pour_amount, f"Pour from Jug B to Jug A
({a}, {b}) -> ({a + pour_amount}, {b - pour_amount})"))
61
62 # Add valid next states to the queue
63 for state in next_states:
64 if state[:2] not in visited:
65 queue.append((state[:2], path + [state[2]]))
66 visited.add(state[:2])
67
68 # If no solution found
69 print(f"No solution found to measure {goal_amount} units of water using jugs with
capacities {capacity_a} and {capacity_b}.")
70
71 # Solve the water jug problem using BFS
72 bfs_jugs()
73
74
75 OUTPUT
76
77 Solution found!
78 Initial State: Jug A=4, Jug B=3, Goal=2
79 Solution Path:
80 Start from (0, 0)
81 Step 1: Fill Jug B (0, 3)
82 Step 2: Pour from Jug B to Jug A (0, 3) -> (3, 0)
83 Step 3: Fill Jug B (3, 3)
84 Step 4: Pour from Jug B to Jug A (3, 3) -> (4, 2)
85 Step 5: Reach (x, 2)
86
87 === Code Execution Successful ===

You might also like