How to solve the treasure exploration problem in POJ2594?