-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix.cs
219 lines (186 loc) · 5.64 KB
/
Matrix.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
using System;
using System.Text;
namespace RotatingWalkInMatrix
{
public class Matrix
{
private const int DIRECTION_COUNT = 8;
private int size;
int[,] data;
int step;
int currentNumber;
Cell previousCell;
Cell nextCell;
public Matrix(int size, int[,] matrix = null)
{
if (size <= 0)
{
throw new ArgumentOutOfRangeException(
"size", "Size should be a positive number");
}
this.size = size;
if (matrix == null)
{
this.data = new int[size, size];
}
else
{
bool isMatrixEmpty = matrix.GetLength(0) == 0 ||
matrix.GetLength(1) == 0;
if (isMatrixEmpty)
{
throw new ArgumentException("Matrix is empty", "data");
}
this.data = matrix;
}
this.step = size;
this.currentNumber = 1;
this.previousCell = new Cell(0, 0);
this.nextCell = new Cell(1, 1);
}
public int[,] Data
{
get
{
return this.data;
}
}
public void WalkInMatrix()
{
PerformLongestWalk();
this.previousCell = FindNextFreeCell();
if (this.previousCell != new Cell(0, 0))
{
this.currentNumber++;
this.nextCell = new Cell(1, 1);
PerformLongestWalk();
}
}
private void PerformLongestWalk()
{
while (true)
{
data[previousCell.X, previousCell.Y] = currentNumber;
if (!AreThereFreeNeighbours(previousCell))
{
break;
}
if (AreAddedCellsInRange(previousCell, nextCell))
{
while (AreAddedCellsInRange(previousCell, nextCell))
{
MoveCellInDirection(ref nextCell);
}
}
previousCell += nextCell;
currentNumber++;
}
}
private bool AreAddedCellsInRange(Cell previousCell, Cell nextCell)
{
Cell result = previousCell + nextCell;
bool isCellOutOfRange = result.X >= size || result.X < 0 ||
result.Y >= size || result.Y < 0;
return isCellOutOfRange || data[result.X, result.Y] != 0;
}
Cell FindNextFreeCell()
{
Cell cell = new Cell(0, 0);
for (int i = 0; i < this.data.GetLength(0); i++)
{
for (int j = 0; j < this.data.GetLength(0); j++)
{
if (this.data[i, j] == 0)
{
return new Cell(i, j);
}
}
}
return cell;
}
bool AreThereFreeNeighbours(Cell cell)
{
Cell[] directions = new Cell[]
{
new Cell(1, 1),
new Cell(1, 0),
new Cell(1, -1),
new Cell(0, -1),
new Cell(-1, -1),
new Cell(-1, 0),
new Cell(-1, 1),
new Cell(0, 1)
};
for (int i = 0; i < DIRECTION_COUNT; i++)
{
directions[i] = CalculateCellCoordinates(cell, directions[i]);
bool isCellFree = this.data[cell.X + directions[i].X,
cell.Y + directions[i].Y] == 0;
if (isCellFree)
{
return true;
}
}
return false;
}
static void MoveCellInDirection(ref Cell cell)
{
Cell[] directions = new Cell[]
{
new Cell(1, 1),
new Cell(1, 0),
new Cell(1, -1),
new Cell(0, -1),
new Cell(-1, -1),
new Cell(-1, 0),
new Cell(-1, 1),
new Cell(0, 1)
};
int cellCount = 0;
for (int i = 0; i < DIRECTION_COUNT; i++)
{
if (directions[i].X == cell.X && directions[i].Y == cell.Y)
{
cellCount = i;
break;
}
}
if (cellCount == 7)
{
cell = directions[0];
return;
}
cell = directions[cellCount + 1];
}
private Cell CalculateCellCoordinates(Cell cell, Cell direction)
{
Cell result = cell + direction;
bool isXOutOfRange = result.X >= this.data.GetLength(0) ||
result.X < 0;
if (isXOutOfRange)
{
direction.X = 0;
}
bool isYOutOfRange = result.Y >= this.data.GetLength(0) ||
result.Y < 0;
if (isYOutOfRange)
{
direction.Y = 0;
}
return direction;
}
public override string ToString()
{
StringBuilder result = new StringBuilder();
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
result.Append(string.Format("{0,3}", data[i, j]));
}
result.Append("\n");
}
return result.ToString();
}
}
}