I understood the algorithm thanks to this image :
When I was doing my tests I tried with a maze (red circle is the destination), with this method you are sure to have the smallest path to the destination, if you already know it of course
Wow thank you for such a detailed response!