implementation of the algorithm A*