1class Solution:2def canMeasureWater(3self, jug1Capacity: int, jug2Capacity: int, targetCapacity: int4) -> bool:5n1, n2, t = jug1Capacity, jug2Capacity, targetCapacity6if n1 == t or n2 == t or n1 + n2 == t:7return True8if n1 + n2 < t:9return False10if n1 < n2:11n1, n2 = n2, n112stack = []13visited = set()14d = n1 - n215if d == t:16return True17while d > n2:18d -= n219if d == t:20return True21stack.append(d)22while stack:23# print(stack)24d = stack.pop()25visited.add(d)26n = n1 + d27if n == t:28return True29n = n1 - d30if n == t:31return True32while n > n2:33n -= n234if n == t:35return True36if n < n2 and n not in visited:37stack.append(n)38n = n2 - d39if n == t:40return True41if n not in visited:42stack.append(n)43return False