Implement the A* algorithm