Well, then it may backtrack. How a normal pathfinding system works (explaining both dijkstra's and A*, I would recommend using A*)
So, dijkstras: split the map into grips, then slowly keep trying new neighbours (unvisited) and see what route is cheaper. Doesn't take into consideration if it's closer to the goal or not
A*: split the map up in grids, check how expensive it is to go to all neighbouring grid spaces. Determine the cost as: movement costs (e.g., in this case it can all be 1), then determine the beeline to the goal, and add some heuristics costs based off of this (e.g. 1/stud, depending on how large your tiles are. H-costs should be a half decent estimate of the realistic costs to get to a location). Keep on checking points, saving paths to those points until you've met your goal
A* is faster as the H-cost prefer grids closer to the goal, so it will start pointing to the target faster