-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph.cs
231 lines (214 loc) · 7.13 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
using System;
using System.Collections.Generic;
namespace bill_ai
{
class Graph
{
int counter;
Vertex rootVertex;
bool[,] board;
Dictionary<Point, Vertex> allVertex;
List<IndexedPointHash> zones;
/// <summary>
/// Articulation Points
/// </summary>
public List<Point> ArticulationPoints;
/// <summary>
/// initialize graph from board, heavy function
/// </summary>
/// <param name="start">
/// starting point, must be a valid point
/// </param>
/// <param name="board">the board</param>
public Graph(Point p, bool[,] b)
{
counter = 1;
rootVertex = new Vertex(p);
board = b;
allVertex = new Dictionary<Point, Vertex>();
allVertex.Add(p, rootVertex);
zones = new List<IndexedPointHash>();
ArticulationPoints = new List<Point>();
}
/*
public void AssignNum(Vertex v)
{
v.Num = counter++;
v.Visited = true;
foreach (Vertex w in v.Neighbours)
{
if (!w.Visited)
{
w.Parent = v;
AssignNum(w);
}
}
}
public void AssignLow(Vertex v)
{
v.Low = v.Num;
foreach (Vertex w in v.Neighbours)
{
if (w.Num > v.Num) // Forward Edge
{
AssignLow(w);
if (w.Low >= v.Num)
ArticulationPoints.Add(v.Self);
v.Low = Math.Min(v.Low, w.Low); // Rule 3
}
else if (v.Parent != w) // Back Edge
{
v.Low = Math.Min(v.Low, w.Num); // Rule 2
}
}
}
*/
private List<Vertex> GetNeighbours(Point p)
{
List<Vertex> result = new List<Vertex>();
foreach (Point p1 in Utility.GetValidMoves(p, board))
{
if (!allVertex.ContainsKey(p1))
allVertex.Add(p1, new Vertex(p1));
result.Add(allVertex[p1]);
}
return result;
}
public void FindArtPoints()
{
Vertex v = rootVertex;
int childCount = 0;
v.Visited = true;
v.Low = v.Num = counter++; // Rule 1
foreach (Vertex w in GetNeighbours(v.Self))
{
if (!w.Visited) // Forward Edge
{
w.Parent = v;
FindArtPoints(w);
v.Low = Math.Min(v.Low, w.Low); // Rule 3
childCount++;
}
else if (v.Parent != w) // Back Edge
{
v.Low = Math.Min(v.Low, w.Num); // Rule 2
}
}
if (childCount > 1)
ArticulationPoints.Add(v.Self);
}
private void FindArtPoints(Vertex v)
{
v.Visited = true;
v.Low = v.Num = counter++; // Rule 1
foreach (Vertex w in GetNeighbours(v.Self))
{
if (!w.Visited) // Forward Edge
{
w.Parent = v;
FindArtPoints(w);
v.Low = Math.Min(v.Low, w.Low); // Rule 3
if (w.Low >= v.Num)
ArticulationPoints.Add(v.Self);
}
else if (v.Parent != w) // Back Edge
{
v.Low = Math.Min(v.Low, w.Num); // Rule 2
}
}
}
public void DivideChambers()
{
bool[,] tempBoard = (bool[,])board.Clone();
List<Point> openSet = new List<Point>();
// fill AP points to seperate maps into zones
foreach (Point p in ArticulationPoints)
{
tempBoard[p.X, p.Y] = true;
foreach (Point p1 in Utility.GetValidMoves(p, tempBoard))
{
if (!ArticulationPoints.Contains(p1))
{
openSet.Add(p1);
}
}
}
foreach (Point p in openSet)
{
IndexedPointHash zone = Utility.GetOpenSpotForSelf(p, tempBoard);
if (zone.Count > 0)
zones.Add(zone);
}
}
public IndexedPointHash GetLargestZone()
{
List<int> indexes = new List<int>();
int index = 0;
for (int i = 0; i < zones.Count; i++)
{
if (zones[index].Count < zones[i].Count)
{
index = i;
indexes.Clear();
indexes.Add(i);
}
else if (zones[index].Count == zones[i].Count)
{
indexes.Add(i);
}
}
IndexedPointHash result = null;
if (zones.Count > 0)
{
if (indexes.Count == 1)
result = zones[index];
else
{
result = new IndexedPointHash();
foreach (int i in indexes)
{
result.Merge(zones[i]);
}
}
foreach (Point p in ArticulationPoints)
{
foreach (Point p1 in Utility.GetValidMoves(p, board))
{
if (result.Contains(p1) && !result.Contains(p))
{
result.Add(p, 1);
}
}
}
}
return result;
}
private void DisplayZones()
{
for (int i = 0; i < Map.Height(); i++)
{
for (int j = 0; j < Map.Width(); j++)
{
if (board[j, i])
System.Diagnostics.Trace.Write("#");
else
{
bool found = false;
foreach (IndexedPointHash ph in zones)
{
if (ph.Contains(j, i))
{
System.Diagnostics.Trace.Write(Char.ConvertFromUtf32('a' + zones.IndexOf(ph)));
found = true;
continue;
}
if (!found)
System.Diagnostics.Trace.Write(" ");
}
}
}
System.Diagnostics.Trace.Write("|" + Environment.NewLine);
}
}
}
}