8000 В метод GetUnvisitedNeighbors добавлен фильтр для отброса препятствий · greenDev7/DijkstraAlgorithm@00ce933 · GitHub
[go: up one dir, main page]

Skip to content

Commit 00ce933

Browse files
author
Зелёный Андрей Сергеевич
committed
В метод GetUnvisitedNeighbors добавлен фильтр для отброса препятствий
1 parent 7b32cfe commit 00ce933

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

Graph.cs

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ public class Graph
3939
return (x, y);
4040

4141
// Merge test
42-
}
43-
42+
}
43+
4444
/// <summary>
4545
/// Возвращает вес ребра, соединяющего две соседние (смежные) вершины графа
4646
/// </summary>
@@ -49,6 +49,33 @@ public class Graph
4949
/// <returns></returns>
5050
double Weight(Vertex v1, Vertex v2)
5151
{
52+
#region Блок для тестирования
53+
54+
if (IsWeightFromTo(v1, 0, 0, v2, 0, 1))
55+
return 1.0;
56+
if (IsWeightFromTo(v1, 0, 1, v2, 0, 2))
57+
return 3.0;
58+
if (IsWeightFromTo(v1, 0, 0, v2, 1, 0))
59+
return 1.0;
60+
if (IsWeightFromTo(v1, 1, 0, v2, 1, 1))
61+
return 100.0;
62+
if (IsWeightFromTo(v1, 1, 1, v2, 1, 2))
63+
return 10.0;
64+
if (IsWeightFromTo(v1, 0, 2, v2, 1, 2))
65+
return 2.0;
66+
if (IsWeightFromTo(v1, 0, 0, v2, 1, 1))
67+
return 40.0;
68+
if (IsWeightFromTo(v1, 1, 0, v2, 0, 1))
69+
return 50.0;
70+
if (IsWeightFromTo(v1, 0, 1, v2, 1, 2))
71+
return 70.0;
72+
if (IsWeightFromTo(v1, 1, 1, v2, 0, 2))
73+
return 30.0;
74+
if (IsWeightFromTo(v1, 0, 1, v2, 1, 1))
75+
return 50.0;
76+
77+
#endregion
78+
5279
(double, double) x1y1 = GetRealXY(v1);
5380
(double, double) x2y2 = GetRealXY(v2);
5481

@@ -59,7 +86,18 @@ double Weight(Vertex v1, Vertex v2)
5986
double sumOfSquares = Math.Pow(xDiff, 2.0) + Math.Pow(yDiff, 2.0) + gamma * Math.Pow(zDiff, 2.0);
6087

6188
return Math.Sqrt(sumOfSquares);
62-
}
89+
}
90+
91+
private bool IsWeightFromTo(Vertex v1, int x1, int y1, Vertex v2, int x2, int y2)
92+
{
93+
bool case1 = x1 == v1.Coordinate.i && y1 == v1.Coordinate.j &&
94+
x2 == v2.Coordinate.i && y2 == v2.Coordinate.j;
95+
96+
bool case2 = x1 == v2.Coordinate.i && y1 == v2.Coordinate.j &&
97+
x2 == v1.Coordinate.i && y2 == v1.Coordinate.j;
98+
99+
return case1 || case2;
100+
}
63101

64102
/// <summary>
65103
/// Возвращает кратчайший путь между двумя заданными вершинами графа
@@ -126,6 +164,7 @@ private List<Point2D> GetShortestPath(Vertex goal)
126164

127165
return path;
128166
}
167+
129168
/// <summary>
130169
/// Возвращает текущую вершину используя список посещенных вершин
131170
/// </summary>
@@ -162,12 +201,13 @@ private bool HasUnvisitedAndNotGoalNeighbors(Vertex vertex, out List<Vertex> unv
162201
unvisitedAndNotGoalNeighbors = GetUnvisitedNeighbors(vertex).Where(v => !v.IsGoal).ToList();
163202
return unvisitedAndNotGoalNeighbors.Any();
164203
}
204+
165205
/// <summary>
166206
/// Возвращает все смежные вершины для текущей, которые не посещены
167207
/// </summary>
168208
/// <param name="current">текущая вершина</param>
169209
/// <returns></returns>
170-
private List<Vertex> GetUnvisitedNeighbors(Vertex current) => GetAllAdjacentVertices(current).Where(v => !v.IsVisited).ToList();
210+
private List<Vertex> GetUnvisitedNeighbors(Vertex current) => GetAllAdjacentVertices(current).Where(v => !v.IsVisited && !v.IsObstacle).ToList();
171211

172212
#region Методы для поиска соседней вершины в зависимости от направления
173213
private Vertex GetTopVertex(Vertex v) => Vertices[v.Coordinate.i, v.Coordinate.j + 1];
@@ -293,6 +333,7 @@ private List<Vertex> GetAllAdjacentVertices(Vertex vertex)
293333
GetTopLeftVertex(vertex)
294334
};
295335
}
336+
296337
public Graph(double dx, double dy, int N, int M, Func<double, double, double> SurfaceFunc, double gamma = 1.0)
297338
{
298339
this.N = N;

Program.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ static void Main(string[] args)
1212
{
1313
Graph graph = new Graph(1, 1, 2, 3, Surface.Plane);
1414

15+
graph.Vertices[0, 1].IsObstacle = true;
16+
graph.Vertices[1, 2].IsObstacle = true;
17+
1518
double shortestPathLength;
1619

17-
Vertex start = graph.Vertices[1, 2];
18-
Vertex goal = graph.Vertices[1, 1];
20+
Vertex start = graph.Vertices[0, 0];
21+
Vertex goal = graph.Vertices[0, 2];
1922

2023
List<Point2D> path = graph.FindShortestPathAndLength(start, goal, out shortestPathLength);
2124

0 commit comments

Comments
 (0)
{"resolvedServerColorMode":"day"}
0