import java.util.Set;

/*
 * Instances of this class are used to record the current
 * search pattern through the tunnels. We need to remember
 * the starting point, the keys we've found and the number
 * of steps to this stage.
 */

public class State
{
    public State (State current, Character key)
    {
        _coord = current._coord;
        _keys = new HashSet<Character>(current._keys);
        _steps = current._steps +1;
        _id = Util.keycode(_keys);
    }

    public State (Coordinate coord, Set<Character> keys, int steps)
    {
        _coord = coord;
        _keys = keys;
        _steps = steps;
        _id = Util.keycode(keys);
    }

    public Coordinate getPosition ()
    {
        return _coord;
    }

    public int numberOfKeys ()
    {
        return _keys.size();
    }

    public int numberOfSteps ()
    {
        return _steps;
    }

    public boolean hasKey (char door)
    {
        char key = Character.toLowerCase(door);

        return _keys.contains(key);
    }

    public Set<Character> getKeys ()
    {
        return _keys;
    }

    @Override
    public int hashCode ()
    {
        return _coord.hashCode();
    }

    @Override
    public boolean equals (Object obj)
    {
        if (obj == null)
            return false;

        if (this == obj)
            return true;
        
        if (getClass() == obj.getClass())
        {
            State temp = (State) obj;

            if (temp._coord.equals(_coord))
            {
                return temp._keys.containsAll(_keys);
            }
        }

        return false;
    }

    @Override
    public String toString ()
    {
        return "State: "+_coord+", "+_id+", "+_steps;
    }

    private Coordinate _coord;
    private Set<Character> _keys;
    private int _steps;
    private String _id;
}

@@ -10,19 +10,17 @@
 va.util.
-Set
+*
 ;%0A%0A/*%0A *
@@ -284,20 +284,21 @@
 t, C
-haracter key
+oordinate pos
 )%0A  
@@ -313,31 +313,131 @@
     
-_coord = current._coord
+this(current, pos, null);%0A    %7D%0A%0A    public State (State current, Coordinate pos, Character key)%0A    %7B%0A        _coord = pos
 ;%0A  
@@ -481,32 +481,87 @@
 current._keys);%0A
+%0A        if (key != null)%0A            _keys.add(key);%0A%0A
         _steps =

Update State.java