working solver in Java with backtracking