Jump to content

Finding the shortest distance on a grid

TheGosuStandard

Hello, I am looking for a way to find the shortest distance (closest point) from a current location to that of a target location which is found within a vector using structs as x & y coordinates.  Currently I start at a certain point on the grid and travel to the next vector point until I have iterated through the vector, however since the points in the vector can be mixed up and all over the place on the grid it takes up a lot of time. 

 

I'm not sure how well I explained the problem, so if you need further explanation let me know and I'll try my best to.

Desktop: Intel 4770k - 12GB Vengeance Pro 1866Mhz RAM - Asus Maximus VI Formula Mobo - Asus Strix 970 SLI - Cooler Master V850 PSU -  Nzxt Phantom 630 Case  - 1TB WD HDD - Samsung 840 Evo 250GB SSD - Nzxt Kraken X60 - 24" Asus VG248QE 1080p Monitor - Logitech G35 Headset -  G502 Proteus Core - Logitech G710+ Keyboard - Nzxt Hue - Windows 10

Link to comment
Share on other sites

Link to post
Share on other sites

Is this for the advent problem?

If so, the shortest path would be to walk horizontally until your X matches the target X then vertically until your Y matches the target Y. So the total distance would just be horizontal distance + vertical distance.

 

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, Mr_KoKa said:

I think that path finding is what you are looking for, and algorithms like A* (A star) or Dijkstra. A* is Faster Dijkstra is more precise.

Thanks, I'll take a look into this. 

4 minutes ago, fizzlesticks said:

Is this for the advent problem?

If so, the shortest path would be to walk horizontally until your X matches the target X then vertically until your Y matches the target Y. So the total distance would just be horizontal distance + vertical distance.

 

What advent problem?

 

Wouldn't walking horizontally until you match X and then Y not take into account all the points in the vector (x,y). I feel as if I need to compare the current location to all the points in the vector to find the closest one and go on from there continuously comparing to find the shortest. 

Desktop: Intel 4770k - 12GB Vengeance Pro 1866Mhz RAM - Asus Maximus VI Formula Mobo - Asus Strix 970 SLI - Cooler Master V850 PSU -  Nzxt Phantom 630 Case  - 1TB WD HDD - Samsung 840 Evo 250GB SSD - Nzxt Kraken X60 - 24" Asus VG248QE 1080p Monitor - Logitech G35 Headset -  G502 Proteus Core - Logitech G710+ Keyboard - Nzxt Hue - Windows 10

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, TheGosuStandard said:

What advent problem?

2016 Advent of code started today and the first problem is about shortest paths on a grid, just a coincidence I guess, ignore my answer.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, TheGosuStandard said:

Thanks, I'll take a look into this. 

What advent problem?

 

Wouldn't walking horizontally until you match X and then Y not take into account all the points in the vector (x,y). I feel as if I need to compare the current location to all the points in the vector to find the closest one and go on from there continuously comparing to find the shortest. 

The advent he's referring to is the first challenge in the Advent of Code.

˙ǝɯᴉʇ ɹnoʎ ƃuᴉʇsɐʍ ǝɹɐ noʎ 'sᴉɥʇ pɐǝɹ oʇ ƃuᴉʎɹʇ ǝɹɐ noʎ ɟI

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, Mr_KoKa said:

I think that path finding is what you are looking for, and algorithms like A* (A star) or Dijkstra. A* is Faster Dijkstra is more precise.

 

After looking over Dijkstra algorithm I seem to be confused in terms of applying it. Aside from that I know some people that are using euclidean's distance formula for comparison of the initial target to the rest of the vector. 

Desktop: Intel 4770k - 12GB Vengeance Pro 1866Mhz RAM - Asus Maximus VI Formula Mobo - Asus Strix 970 SLI - Cooler Master V850 PSU -  Nzxt Phantom 630 Case  - 1TB WD HDD - Samsung 840 Evo 250GB SSD - Nzxt Kraken X60 - 24" Asus VG248QE 1080p Monitor - Logitech G35 Headset -  G502 Proteus Core - Logitech G710+ Keyboard - Nzxt Hue - Windows 10

Link to comment
Share on other sites

Link to post
Share on other sites

So you want the shortest path from one point to another in a graph? Tons of ways of doing it.

 

Use a greedy approach with Djisktra's algorithm or a more heuristic one with A*. Dynamic programming with Bellman-Ford or Roy-Floyd (although Roy-Floyd computes the shortest distances between each pair of nodes in the graph). Look them up on Google.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×