-2

I'm struggling to find the exact answer to my problem. I have a 20 by 20 grid. I am looking for a PHP code example, formula or the correct name of the algorithm needed to find out the following:

  • Shortest route between 2 points that also passes through 1 of a few other pre defined points
  • The route does not have to return to the origin at the end
  • The route is allowed to pass diagonally between cells
  • The origin and destination is set

So say my grid runs from A to T horizontally and 1 to 20 vertically. I am looking to calculate the shortest route from my origin A5 to my destination G12, whilst passing through either C7, D9, E5 or J8 based on whatever is the shortest overall route.

Additionally, it would be handy to know how to extend this so it not only passes through one point but perhaps 2, on its way to the destination (G12).

I have searched 2 days and my mind is now swirling with Dijkstra, TSP, BFS and the rest!

Thankyou!

Jeremy Ben
  • 11
  • 3
  • Is this a variation of a traveling salesman problem where order of hitting the points doesn't matter? Can I take the shortest path between the two points (e.g. the line), or do I have to make 90 degree (or 45 degree) turns? – Melissa Key Apr 05 '18 at 02:39
  • The origin and destination is set. The order of the points in between would not matter, just whatever is quickest. Shortest path is fine, you don't have to stick to 90/45 degree turns. Thanks! – Jeremy Ben Apr 05 '18 at 02:42
  • Hi Jeramy, Welcome to SO. SO is for help out people who try to do some coding and when they get stuck with the work, Haven't you tried anything? – Dinidu Hewage Apr 05 '18 at 02:42
  • Thanks for the welcome. I have been searching for a while to get some kind of base to start with. It just seems nothing out there matches the requirements. OR, more likely. I don't know the name of the algorithm I'm searching for. – Jeremy Ben Apr 05 '18 at 02:45
  • Supposing starting point is `(0,0)` and finishing point is `(20,20)` and there is only one required point `(1,19)`. Is path `(0,0) - (1,19) - (20,20)` a valid path? Or must there be some additional internal points because of the restrictions on allowed moves? – SergGr Apr 05 '18 at 02:48
  • @SergGr Thanks for your comment. I have updated both the title and the question. I had missed out the biggest problem! – Jeremy Ben Apr 05 '18 at 02:59
  • Try `open_wormhole()` and `close_wormhole()` – ArtisticPhoenix Apr 05 '18 at 02:59
  • 1
    @JeremyBen, sorry but I don't see how your edits answer my question. My question is fundamentally about allowed steps: can you jump directly from `(0,0)` to `(1,19)` or do you have to use some middle points in the path (and if so - which ones)? Because if they don't have, then I don't get why the "grid" part is of any importance. Why don't you say you have a few point on a XY-plane? – SergGr Apr 05 '18 at 03:04
  • @SergGr Sorry and yes you could jump from 0,0 to 1,19. I thought "grid" would be important because it minimizes the complexity. Eg, all the C row shares the same X position. All the cells in row 6 share the same Y etc. Each possible position shares the same distances from its neighbour. – Jeremy Ben Apr 05 '18 at 03:10
  • 1
    This question has popped up Stackoverflow multiple times, every hiring season, this makes a comeback. May I ask where do you get this question? – Pham Trung Apr 05 '18 at 03:45
  • Possible duplicate of [Shortest path in a matrix](https://stackoverflow.com/questions/25407059/shortest-path-in-a-matrix) – Pham Trung Apr 05 '18 at 03:48
  • 2
    To demonstrate my point, let me list down a series of similar questions:[this](https://stackoverflow.com/questions/25441051/minimum-distance-between-start-and-end-by-going-through-must-visit-points-in-a-m/25442228#25442228), [this](https://stackoverflow.com/questions/25395797/shortest-path-in-2d-arrays/25396058#25396058), [this](https://stackoverflow.com/questions/25385541/reach-end-while-going-through-all-required-points) and [this](https://stackoverflow.com/questions/25368709/dijkstras-algorithm-with-must-pass-nodes) – Pham Trung Apr 05 '18 at 03:48
  • @PhamTrung, 1) This doesn't look like "_every hiring season_", to me this looks like a task in some competition in August 2014. 2) More to the point, it might be that this question is actually the same as the linked ones but the "maze" part is quite an important restriction and there is nothing that suggests that this question has "maze" in it. Moreover from the answers in comments it looks like you can jump directly from `(0,0)` to `(1,19)` without any middle points which is clearly impossible in any maze. So either this question is really badly stated or it is not a duplicate. – SergGr Apr 05 '18 at 12:49
  • @SergGr I am sorry, but I know the company which use this question (and still is) for screening. And if the OP doesn't want to clarify this, even though he has been here one hour ago, what is the point to argue? If this is badly stated, why bother to answer it? – Pham Trung Apr 05 '18 at 13:28
  • And what is your point of jump directly? it is stated clearly in the question `The route is allowed to pass diagonally from cell to cell`. This is a grid 20 x 20, not a 2D plane. – Pham Trung Apr 05 '18 at 13:35
  • @PhamTrung, we probably read it differently. In my opinion the quoted line "_The route is allowed to pass diagonally from cell to cell_" is exactly what makes it **_not_** the "maze" problem because you are not allowed to do that in mazes. This question is badly written **_if_** we assume that this is really the "maze" question. But so far I see no reasons to assume it. All the evidences provided by the OP in the question and in comments so far are IMHO against the "maze" theory. What did I miss in this topic that suggests otherwise (except for the fact that there is a known similar problem)? – SergGr Apr 05 '18 at 14:17
  • @PhamTrung This is a question related to a website tool I am making for a game. I checked your links but there are several things that make my question different. Diagonal movement is 1. Choosing only 1 mid way point from a pool is another. – Jeremy Ben Apr 05 '18 at 15:48
  • Moving in diagonal direction does not mean Euclidean distance. Do you play chess? Ever see a queen move? – Pham Trung Apr 05 '18 at 18:13
  • Its nothing to do with a maze and certainly not for a company or competition. Its for game map website - to find the best route from A to B whilst picking up a resource on the way. – Jeremy Ben Apr 06 '18 at 00:01

1 Answers1

1

If you can jump directly to the middle points ignoring the whole grid, than basic geometry suggest that the shortest path from point S(tart) to point E(nd) via required point A is just two line segments SA + AE and calculating its total distance is actually quite fast (it just standard Euclidean distance). So unless you have thousands of possible middle points just checking all paths SA+AE, SB+BE, ... and comparing the distances will be quite fast. Even if there are two middle points and the pool has a size of a dozen, there will be only a few hundreds of combinations so brute force will still work pretty fast.

SergGr
  • 23,570
  • 2
  • 30
  • 51
  • Thankyou! This set me on the right path. As someone without a great grasp on mathematics, this was by far the best answer. Brute force does indeed work perfectly well in this instance. – Jeremy Ben Apr 05 '18 at 15:45