- Home /

# How do you change Breadth First Search into Dijkstra's algorithm?

Hello. A while ago, I found a turn-based strategy game tutorial where the pathfinding algorithm known as Breadth First Search is used to determine which tiles the characters can get to each turn. I would like to use Dijkstra's algorithm instead, because I am looking to have some tiles cost more to move through. For example, a forest may have a cost of 2, and mountains may have a cost of 3, or 4, etc. I found a web page that shows how to change BFS into Dijkstra's algorithm: https://www.redblobgames.com/pathfinding/a-star/introduction.html, but it is written in Python, which I don't know how to translate to C# for Unity. Maybe someone here does? Anyhow, here is the BFS part of the code that appears in the turn-based strategy game tutorial: (Does anyone know how to change this to Dijkstra's algorithm?)

```
public void FindSelectableTiles()
{
ComputeAdjacencyLists(jumpHeight, null);
GetCurrentTile();
Queue<Tile> process = new Queue<Tile>();
process.Enqueue(currentTile);
currentTile.visited = true;
//currentTile.parent = ?? leave as null
while (process.Count > 0)
{
Tile t = process.Dequeue();
selectableTiles.Add(t);
t.selectable = true;
if (t.distance < move)
{
foreach (Tile tile in t.adjacencyList)
{
if (!tile.visited)
{
tile.parent = t;
tile.visited = true;
tile.distance = 1 + t.distance;
process.Enqueue(tile);
}
}
}
}
}
```

Could you provide a link to to the turn-based strategy tutorial?

That could also help: http://www.codeding.com/articles/dijkstras-path-finding

Here is the tutorial: http://www.gameprogrammingacademy.com/unity-tutorial-tactics-movement/

Thanks for the link!

Just leaving this here in case it helps, since the link above seems broken...

https://www.redblobgames.com/pathfinding/a-star/introduction.html

Thank you. It looks like the comma got interpreted as part of the URL. Sorry about that.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Generating Triangles Given a Set of Points 1 Answer

Trail renderer algorithm? 0 Answers

Tile to Sphere world algorithm 1 Answer

Path finding algorithm for 2d game 2 Answers

Foreach with where with 2 lists 0 Answers