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
| public class PathFind
{
private int MSIZEX = 0;
private int MSIZEY = 0;
private int[,] maze_data;
public PathFind(int mapSizeX, int mapSizeY, int[,] maze_data)
{
MSIZEX = mapSizeX;
MSIZEY = mapSizeY;
this.maze_data = maze_data;
}
public int[,] Solve(int xSource, int ySource, int xDest, int yDest)
{
int[,] tMazePath = new int[MSIZEX, MSIZEY];
int[,] maze = new int[MSIZEX, MSIZEY];
bool destReached = false;
CellPosition cellPos = new CellPosition(xSource, ySource);
ArrayList calcState = new ArrayList();
calcState.Add(cellPos);
int step = 0;
for (int i = 0; i < MSIZEX; i++)
for (int j = 0; j < MSIZEY; j++)
tMazePath[i, j] = -1;
tMazePath[xSource, ySource] = step;
if (maze_data == null) return null;
if (xSource == xDest && ySource == yDest) return maze;
while (destReached == false && calcState.Count > 0)
{
step++;
ArrayList calcNextState = new ArrayList();
for (int i = 0; i < calcState.Count; i++)
{
CellPosition calcCPos = (CellPosition)calcState[i];
// движение по клетке
// N
if (calcCPos.y > 0) // только вверх
if (tMazePath[calcCPos.x, calcCPos.y - 1] == -1)
if (maze_data[calcCPos.x, calcCPos.y] != 0)
{
tMazePath[calcCPos.x, calcCPos.y - 1] = step;
CellPosition calcNextCPos = new CellPosition(calcCPos.x, calcCPos.y - 1);
calcNextState.Add(calcNextCPos);
if (calcNextCPos.x == xDest && calcNextCPos.y == yDest) destReached = true;
}
// W
if (calcCPos.x > 0) // движение влево
if (tMazePath[calcCPos.x - 1, calcCPos.y] == -1)
if (maze_data[calcCPos.x, calcCPos.y] != 0)
{
tMazePath[calcCPos.x - 1, calcCPos.y] = step;
CellPosition calcNextCPos = new CellPosition(calcCPos.x - 1, calcCPos.y);
calcNextState.Add(calcNextCPos);
if (calcNextCPos.x == xDest && calcNextCPos.y == yDest) destReached = true;
}
// S
if (calcCPos.y < MSIZEY - 1) // движение вниз
if (tMazePath[calcCPos.x, calcCPos.y + 1] == -1)
if (maze_data[calcCPos.x, calcCPos.y + 1] != 0)
{
tMazePath[calcCPos.x, calcCPos.y + 1] = step;
CellPosition calcNextCPos = new CellPosition(calcCPos.x, calcCPos.y + 1);
calcNextState.Add(calcNextCPos);
if (calcNextCPos.x == xDest && calcNextCPos.y == yDest) destReached = true;
}
// E
if (calcCPos.x < MSIZEX - 1) // движение вправо
if (tMazePath[calcCPos.x + 1, calcCPos.y] == -1)
if (maze_data[calcCPos.x + 1, calcCPos.y] != 0)
{
tMazePath[calcCPos.x + 1, calcCPos.y] = step;
CellPosition calcNextCPos = new CellPosition(calcCPos.x + 1, calcCPos.y);
calcNextState.Add(calcNextCPos);
if (calcNextCPos.x == xDest && calcNextCPos.y == yDest) destReached = true;
}
}
calcState = calcNextState;
}
if (destReached == false)
return null;
else
{
tMazePath[xDest, yDest] = step;
// массив для пути
ArrayList pPath = new ArrayList();
int tx = xDest;
int ty = yDest;
pPath.Add(new CellPosition(tx, ty));
bool stepExists;
while (tx != xSource || ty != ySource)
{
step = tMazePath[tx, ty];
stepExists = false;
// ищи шаги
// N
if (ty > 0 && stepExists == false)
if (tMazePath[tx, ty - 1] == step - 1 &&
maze_data[tx, ty - 1] != 0)
{
ty -= 1; stepExists = true;
pPath.Add(new CellPosition(tx, ty));
}
// W
if (tx > 0 && stepExists == false)
if (tMazePath[tx - 1, ty] == step - 1 &&
maze_data[tx - 1, ty] != 0)
{
tx -= 1; stepExists = true;
pPath.Add(new CellPosition(tx, ty));
}
// S
if (ty < MSIZEY - 1 && stepExists == false)
if (tMazePath[tx, ty + 1] == step - 1 &&
maze_data[tx, ty + 1] != 0)
{
ty += 1; stepExists = true;
pPath.Add(new CellPosition(tx, ty));
}
// E
if (tx < MSIZEX - 1 && stepExists == false)
if (tMazePath[tx + 1, ty] == step - 1 &&
maze_data[tx + 1, ty] != 0)
{
tx += 1; stepExists = true;
pPath.Add(new CellPosition(tx, ty));
}
if (stepExists == false) return null;
}
if (pPath != null)
{
for (int i = 0; i < pPath.Count; i++)
{
if (i > 0)
{
Console.ForegroundColor = ConsoleColor.Magenta;
CellPosition s = (CellPosition)pPath[i - 1];
CellPosition t = (CellPosition)pPath[i];
if (t.x > s.x)
{
maze[s.x + 1, s.y] = 1;
Console.SetCursorPosition(s.x + 1, s.y);
Console.Write("@");
}
if (t.x < s.x)
{
maze[s.x - 1, s.y] = 1;
Console.SetCursorPosition(s.x - 1, s.y);
Console.Write("@");
}
if (t.y > s.y)
{
maze[s.x, s.y + 1] = 1;
Console.SetCursorPosition(s.x, s.y + 1);
Console.Write("@");
}
if (t.y < s.y)
{
maze[s.x, s.y - 1] = 1;
Console.SetCursorPosition(s.x, s.y - 1);
Console.Write("@");
}
maze[t.x, t.y] = 1;
Console.SetCursorPosition(t.x, t.y);
Console.Write("@");
}
else
{
CellPosition t = (CellPosition)pPath[i];
maze[t.x, t.y] = 1;
Console.SetCursorPosition(t.x, t.y);
Console.Write("@");
}
Thread.Sleep(100);
}
}
Console.ResetColor();
return maze;
}
}
}
public class CellPosition
{
public int x, y;
public CellPosition() { }
public CellPosition(int xp, int yp) { x = xp; y = yp; }
} |