-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPathFinder.cs
More file actions
44 lines (38 loc) · 1.24 KB
/
PathFinder.cs
File metadata and controls
44 lines (38 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System;
using System.Collections.Generic;
using System.Linq;
public static class PathFinder
{
//distance f-ion should return distance between two adjacent nodes
//estimate should return distance between any node and destination node
public static Path<Node> FindPath<Node>(
Node start,
Node destination,
Func distance,
Func estimate)
where Node : IHasNeighbours
{
//set of already checked nodes
var closed = new HashSet();
//queued nodes in open set
var queue = new PriorityQueue> ();
queue.Enqueue(0, new Path(start));
while (!queue.IsEmpty)
{
var path = queue.Dequeue();
if (closed.Contains(path.LastStep))
continue;
if (path.LastStep.Equals(destination))
return path;
closed.Add(path.LastStep);
foreach (Node n in path.LastStep.Neighbours)
{
double d = distance(path.LastStep, n);
//new step added without modifying current path
var newPath = path.AddStep(n, d);
queue.Enqueue(newPath.TotalCost + estimate(n), newPath);
}
}
return null;
}
}