public bool CanMeasureWater2(int x, int y, int z) { if (x + y < z) { return(false); } if (x > y) { int tmp = x; x = y; y = tmp; } var states = new Queue <JugState>(); var visited = new HashSet <JugState>(); // initial state var init = new JugState(0, 0); states.Enqueue(init); visited.Add(init); while (states.Count != 0) { JugState curr = states.Dequeue(); // fill jug1 var temp = new List <JugState>(); temp.Add(new JugState(x, curr.b)); // fill jug 1 temp.Add(new JugState(0, curr.b)); // empty jug1 temp.Add(new JugState(curr.a, y)); // fill jug 2 temp.Add(new JugState(curr.a, 0)); // empty jug2 temp.Add(new JugState(Math.Min(curr.a + curr.b, x), curr.a + curr.b < x ? 0 : curr.b - (x - curr.a))); // pour all water from jug2 to jug1 x temp.Add(new JugState(curr.a + curr.b < y ? 0 : curr.a - (y - curr.b), Math.Min(curr.a + curr.b, y))); // pour all water from jug1 to jug2 y foreach (JugState tmp in temp) { if (visited.Contains(tmp)) { continue; } if (curr.a + curr.b == z) { return(true); } states.Enqueue(tmp); visited.Add(tmp); } } return(false); }
public override bool Equals(object obj) { JugState other = (JugState)obj; return(this.a == other.a && this.b == other.b); }