-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph.cs
249 lines (212 loc) · 6.63 KB
/
Graph.cs
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MazeGenerator
{
/// <summary>
/// Граф лабиринта
/// </summary>
class Graph
{
private List<Node> nodes = new List<Node>();
public Node AddNode()
{
var node = new Node();
nodes.Add(node);
return node;
}
public Node AddNodePos(Cell cell)
{
var node = AddNode();
node.pos = cell;
return node;
}
public void Associate(Node a, Node b, int distance)
{
a.AddNeighbour(b, distance);
b.AddNeighbour(a, distance);
}
public void AddNextNodeAndDistanceToIt(Cell cell, int distance)
{
var last = nodes.Last();
var newnode = AddNodePos(cell);
Associate(last, newnode, distance);
}
private int[,] GetDistanceMatrix()
{
var n = nodes.Count;
int[,] matrix = new int[n, n];
for (int i = 0; i < n; i++)
{
for (int j = i; j < n; j++)
{
matrix[i, j] = nodes[i].GetDistance(nodes[j]);
}
}
return matrix;
}
public Tuple<Cell,Cell> GetFarest()
{
var dist = GetDistanceMatrix();
var n = nodes.Count;
//Console.BackgroundColor = ConsoleColor.Black;
/*
Console.Clear();
for (int i = -1; i < n; i++)
{
Console.Write((char)((byte)'A' + i));
for (int j = 0; j < n; j++)
{
if (i == -1)
Console.Write(string.Format(" {0,2}", (char)((byte)'A' + j)));
else
Console.Write(string.Format(" {0,2}", i < j ? dist[i, j] : dist[j, i]));
}
Console.WriteLine();
}
Console.ReadKey();
*/
//int[,] temp = (int[,])dist.Clone();
for (int l = 0; l < Math.Sqrt(n); l++)
{
//Console.Clear();
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (dist[i, j] == -1)
{
int? min = null;
for (int k = 0; k < n; k++)
{
if (k == i || k == j)
continue;
var a1 = Math.Min(i, k);
var a2 = Math.Max(i, k);
var b1 = Math.Min(j, k);
var b2 = Math.Max(j, k);
if (dist[a1, a2] != -1 && dist[b1, b2] != -1)
{
if (!min.HasValue || min.Value > dist[a1, a2] + dist[b1, b2])
min = dist[a1, a2] + dist[b1, b2];
}
}
dist[i, j] = min ?? -1;
}
}
}
//dist = temp;
/*for (int i = -1; i < n; i++)
{
Console.Write((char)((byte)'A' + i));
for (int j = 0; j < n; j++)
{
if (i == -1)
Console.Write(string.Format(" {0,2}", (char)((byte)'A' + j)));
else
Console.Write(string.Format(" {0,2}", i < j ? dist[i, j] : dist[j, i]));
}
Console.WriteLine();
}
Console.ReadKey();*/
}
int x = 0, y = 0;
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (dist[i,j] > dist[x,y])
{
x = i; y = j;
}
}
}
//Console.Clear();
return new Tuple<Cell, Cell>(nodes[x].pos.Value, nodes[y].pos.Value);
}
public Node[] Nodes
{
get
{
return nodes.ToArray();
}
}
public Node First
{
get
{
return nodes.First();
}
}
public Node Last
{
get
{
return nodes.Last();
}
}
public Node PreLast
{
get
{
return nodes[nodes.Count - 2];
}
}
internal bool IsNode(Cell cell)
{
return nodes.Any(n => n.pos.HasValue && n.pos.Value.x == cell.x && n.pos.Value.y == cell.y);
}
public Node this[Cell cell]
{
get
{
if (IsNode(cell))
return nodes.First(n => n.pos.HasValue && n.pos.Value.x == cell.x && n.pos.Value.y == cell.y);
else
return null;
}
}
internal void SetLast(Node node)
{
var i = nodes.IndexOf(node);
var j = nodes.Count - 1;
var temp = nodes[j];
nodes[j] = nodes[i];
nodes[i] = temp;
}
}
/// <summary>
/// Вершина графа лабиринта (тупик или развилка)
/// </summary>
class Node
{
Dictionary<Node, int> Edges = new Dictionary<Node, int>();
public Cell? pos { get; set; } = null;
public void AddNeighbour(Node neighbour, int distance)
{
if (Edges.ContainsKey(neighbour))
return;
//neighbour.AddNeighbour(this, distance, reversed = false);
Edges.Add(neighbour, distance);
}
public int GetDistance(Node node, bool recursive = false)
{
if (recursive)
throw new NotImplementedException();
if (node == this)
return 0;
if (!Edges.ContainsKey(node))
return -1;
else
return Edges[node];
}
public override string ToString()
{
if (pos.HasValue)
return pos.ToString();
return base.ToString();
}
}
}