Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 475687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:24:49+00:00 2026-05-13T00:24:49+00:00

I’m working on a project where I need to perform pathfinding to find the

  • 0

I’m working on a project where I need to perform pathfinding to find the route which costs the least. I don’t really care if it’s the shortest route possible. So far it seems A* is out of the question and I honestly do not understand Prim’s algorithm.

Let me explain the kind of maps that I need to find routes on. This is an example map:

+------|-*----
+------|----|-
+--|--------|-
+@-|----------

The “*” is the start location, the “@” is the destination. The “+” signs in a line indicate a direct route which a) costs the same as a single step, and b) halves the cost of the entire route.

This means there are 10 “steps” from the start position to the destination via the “+” route, which ends up with a cost of 5. There are 15 steps to use the left-most “|” route (“|” is a lower cost than “-“, but worse than “+”), which ends up with a cost of 15. Obviously, the route with a cost of 5 is the route to use.

Now I’m having trouble implementing this in C#. I currently have a “step” function which moves and returns if the way was blocked or the cost of the step, and the new position. This works well, but at the moment it is extremely naive in that it’ll go down a “|” if it finds one before a “+” (which means the entire trip costs significantly more, as it hasn’t found the faster route).

I was thinking of marking each location as “visited”, but it’s completely plausible that the lowest-cost route will loop back on itself. There are also many different paths, each of which is unique, and each of which may use different path segments (that may have already been visited by a previous run). Obviously each path needs to be traversed in order to find the cheapest path, but I can’t figure out how to do that without ending up searching the same routes over and over again.

If it makes it simpler, I can limit any movement to only move towards the destination (ie, can’t go back up again after going down).

If anyone could provide some insight, that’d be great!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T00:24:49+00:00Added an answer on May 13, 2026 at 12:24 am

    From what I understand, the ‘-‘ fields in your map are graph nodes. Each ‘-‘ node has at most 8 edges to neighboring ‘-‘ fields. 8 if you allow diagonal movement, otherwise only 4 neighboring ‘-‘ nodes are valid. There is no edge between a ‘-‘ node and a ‘|’ node.

    This is enough to implement a simple depth-first search / breadth-first-search in which you keep a queue of unvisted nodes (LIFO for depth-first, FIFO for breadth-first) and a list of visited nodes (to avoid cycling). Both algorithms will be relatively inefficient, but can be easily improved upon.

    I’m not sure what the meaning of your ‘+’ nodes is. Is moving from one ‘+’ to the next ‘+’ mode a free move? If so, you can model this using edge costs. A move from or to a ‘-‘ node has cost 1, a move from ‘+’ to ‘+’ node has cost 0.

    The breadth-first-search algorithm can be extended to Dijkstra’s algorithm that calculates the shortest path between your source and destination as long as all graph edges are non-negative:

    http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm

    The Dijkstra algorithm can be further improved with the addition of a simple heuristic, making it the A* algorithm. If you have the coordinates of your goal in 2D coordinates, you could use the euclidian distance between a node and the goal as a rough estimate of which node is best to follow. If the ‘+’ fields are something of a tunnel through your map with zero cost to move, the A* algorithm may not help that much because heuristically moving towards your destination will often be wrong if you should have moved towards the tunnel. If there are multiple tunnels or tunnels not leading to your destination, there may not be an heuristic better than the naive Dijkstra algorithm.

    Please note that it is impossible for the lowest-cost route to contain a loop: If the lowest-cost route contained a loop, stripping the loop would still yield a valid route to the goal with lower cost contradicting the assumption that we started from a route with lowest-cost.

    Have a look at Cormen’s Introduction to Algorithms, or the relevant Wikipedia pages:

    http://en.wikipedia.org/wiki/Shortest_path

    http://en.wikipedia.org/wiki/Breadth-first_search

    http://en.wikipedia.org/wiki/Depth-first_search

    http://en.wikipedia.org/wiki/A*_search_algorithm

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 262k
  • Answers 262k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I don't know of an official logo for the C#… May 13, 2026 at 11:44 am
  • Editorial Team
    Editorial Team added an answer The reason Tcp is faster, is that it uses binary… May 13, 2026 at 11:44 am
  • Editorial Team
    Editorial Team added an answer The async_xxx functions are templated on the type of the… May 13, 2026 at 11:44 am

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I've got a string that has curly quotes in it. I'd like to replace
In order to apply a triggered animation to all ToolTip s in my app,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.