35
35
using System ;
36
36
using System . Collections . Generic ;
37
37
using System . Collections . Specialized ;
38
+ using System . Diagnostics . Eventing . Reader ;
38
39
using System . Dynamic ;
39
40
using System . IO ;
40
41
using System . Linq ;
41
42
using System . Net ;
43
+ using System . Runtime . CompilerServices ;
42
44
using System . Text ;
43
45
using System . Threading . Tasks ;
44
46
@@ -55,8 +57,8 @@ public class Maze
55
57
private int mazeSize ;
56
58
57
59
// later, save all correct paths, compare sizes and print optimal paths
58
- int shortestListSize = 0 ;
59
- List < List < int > > possiblePaths = new List < List < int > > ( ) ;
60
+ private int shortestListSize = 0 ;
61
+ private List < Pointer [ ] > possiblePaths = new List < Pointer [ ] > ( ) ;
60
62
61
63
public Maze ( ) { }
62
64
@@ -130,6 +132,16 @@ public string GetMatrix(int row, int column)
130
132
return matrix [ row , column ] ;
131
133
}
132
134
135
+ public List < Pointer [ ] > GetPossiblePaths ( )
136
+ {
137
+ return possiblePaths ;
138
+ }
139
+
140
+ // It creates a matrix with this coordinates
141
+ // Y,X
142
+ // 0,0 | 0,1 | 0,2 ...
143
+ // 1,0 | 1,1 | 1,2 ...
144
+ // 2,0 | 2,1 | 2,2 ...
133
145
private void CreateMatrix ( string [ ] lines )
134
146
{
135
147
for ( int rowNum = 1 ; rowNum < mazeSize ; rowNum ++ )
@@ -163,88 +175,54 @@ public void SolveMaze()
163
175
}
164
176
else
165
177
{
166
- List < int > yxPath = new List < int > ( ) ; // saves path as y*10 and x*1
167
- ReachTargetNumber ( currentY , currentX , current , yxPath ) ;
178
+ List < Pointer > yxPath = new List < Pointer > ( ) ; // saves path as y*10 and x*1
179
+
180
+ // It tries to find a path right and down
181
+ ReachTargetNumber ( currentY , currentX + 1 , current , yxPath ) ;
182
+ ReachTargetNumber ( currentY + 1 , currentX , current , yxPath ) ;
168
183
}
184
+
185
+ TranslatePossiblePaths ( ) ;
169
186
}
170
187
}
171
188
172
189
173
- public void ReachTargetNumber ( int currentY , int currentX , int current , List < int > yxPath )
190
+ public void ReachTargetNumber ( int currentY , int currentX , int current , List < Pointer > yxPath )
174
191
{
175
- // Checks if we have reach the bottom right of the maze
176
- if ( currentY == mazeSize - 2 && currentX == mazeSize - 2 )
192
+ // Next if statements check if we are at the edge of the maze or inside
193
+ if ( currentX <= mazeSize - 2 && currentY <= mazeSize - 2 && currentX >= 0 && currentY >= 0 && ! ( currentY == 0 && currentX == 0 ) )
177
194
{
178
- if ( current == desiredEndResultNum )
179
- {
180
- possiblePaths . Add ( yxPath ) ;
181
- return ;
182
- }
183
- // Next if statements check if we are at the edge of the maze or inside
184
- else
185
- {
186
- // gets next right number -->
187
- if ( currentX <= mazeSize - 3 )
188
- {
189
- currentX ++ ;
190
- string integerString = matrix [ currentY , currentX ] ;
191
- char nextOperator = integerString [ 0 ] ;
192
- int nextNum = Convert . ToInt32 ( integerString . Substring ( 1 ) ) ;
193
-
194
- // Performs the operation and updates the position
195
- current = Calculator ( current , nextOperator , nextNum ) ;
195
+ yxPath . Add ( new Pointer ( currentY , currentX ) ) ;
196
+ string integerString = matrix [ currentY , currentX ] ;
197
+ char nextOperator = integerString [ 0 ] ;
198
+ int nextNum = Convert . ToInt32 ( integerString . Substring ( 1 ) ) ;
196
199
197
- // Calls the function to see if we have reach our goal
198
- ReachTargetNumber ( currentY , currentX , current , yxPath ) ;
199
- }
200
+ // Performs the operation and updates the position
201
+ current = Calculator ( current , nextOperator , nextNum ) ;
200
202
201
- // gets next down number
202
- if ( currentY <= mazeSize - 3 )
203
- {
204
- currentY ++ ;
205
- string integerString = matrix [ currentY , currentX ] ;
206
- char nextOperator = integerString [ 0 ] ;
207
- int nextNum = Convert . ToInt32 ( integerString . Substring ( 1 ) ) ;
208
-
209
- // Performs the operation and updates the position
210
- current = Calculator ( current , nextOperator , nextNum ) ;
211
-
212
- // Calls the function to see if we have reach our goal
213
- ReachTargetNumber ( currentY , currentX , current , yxPath ) ;
214
- }
215
-
216
- // gets next left number <--
217
- // Once it has reach the bottom right, it won't keep going back into the maze
218
- if ( currentX >= 0 && currentX <= mazeSize - 3 && currentY <= mazeSize - 3 )
203
+ // Checks if we have reach the bottom right of the maze & have reach our goal
204
+ if ( currentY == mazeSize - 2 && currentX == mazeSize - 2 )
205
+ {
206
+ if ( current == desiredEndResultNum )
219
207
{
220
- currentX -- ;
221
- string integerString = matrix [ currentY , currentX ] ;
222
- char nextOperator = integerString [ 0 ] ;
223
- int nextNum = Convert . ToInt32 ( integerString . Substring ( 1 ) ) ;
224
-
225
- // Performs the operation and updates the position
226
- current = Calculator ( current , nextOperator , nextNum ) ;
227
-
228
- // Calls the function to see if we have reach our goal
229
- ReachTargetNumber ( currentY , currentX , current , yxPath ) ;
208
+ Pointer [ ] addableList = new Pointer [ yxPath . Count ] ;
209
+ addableList = yxPath . ToArray ( ) ;
210
+ possiblePaths . Add ( addableList ) ;
211
+ yxPath . Clear ( ) ;
230
212
}
231
-
232
- // gets next up number
233
- // Once it has reach the bottom right, it won't keep going back into the maze
234
- if ( currentY >= 0 && currentX <= mazeSize - 3 && currentY <= mazeSize - 3 )
213
+ else
235
214
{
236
- currentY -- ;
237
- string integerString = matrix [ currentY , currentX ] ;
238
- char nextOperator = integerString [ 0 ] ;
239
- int nextNum = Convert . ToInt32 ( integerString . Substring ( 1 ) ) ;
240
-
241
- // Performs the operation and updates the position
242
- current = Calculator ( current , nextOperator , nextNum ) ;
243
-
244
- // Calls the function to see if we have reach our goal
245
- ReachTargetNumber ( currentY , currentX , current , yxPath ) ;
215
+ yxPath . Clear ( ) ;
246
216
}
247
217
}
218
+ else
219
+ {
220
+ // it tries to find a path right, down, left, and up.
221
+ ReachTargetNumber ( currentY , currentX + 1 , current , yxPath ) ;
222
+ ReachTargetNumber ( currentY + 1 , currentX , current , yxPath ) ;
223
+ ReachTargetNumber ( currentY , currentX - 1 , current , yxPath ) ;
224
+ ReachTargetNumber ( currentY - 1 , currentX , current , yxPath ) ;
225
+ }
248
226
}
249
227
}
250
228
@@ -269,13 +247,71 @@ public int Calculator(int current, char nextRightOperator, int nextRightNum)
269
247
return current + nextRightNum ;
270
248
}
271
249
}
250
+
251
+ public void TranslatePossiblePaths ( )
252
+ {
253
+ if ( possiblePaths != null )
254
+ {
255
+ // finds the lenght of the shortest possible path
256
+ int shortestPathLenght = possiblePaths [ 0 ] . Length ;
257
+ for ( int i = 1 ; i < possiblePaths . Count ; i ++ )
258
+ {
259
+ if ( possiblePaths [ i ] . Length < shortestPathLenght )
260
+ {
261
+ shortestPathLenght = possiblePaths [ i ] . Length ;
262
+ }
263
+ }
264
+
265
+ // saves all correct paths of the shortest lenght
266
+ List < Pointer [ ] > listOfShortPaths = new List < Pointer [ ] > ( ) ;
267
+ for ( int i = 0 ; i < possiblePaths . Count ; i ++ )
268
+ {
269
+ if ( possiblePaths [ i ] . Length == shortestPathLenght )
270
+ {
271
+ listOfShortPaths . Add ( possiblePaths [ i ] ) ;
272
+ }
273
+ }
274
+
275
+ // saves path lenght
276
+ amountOfNumsInPath = shortestPathLenght ;
277
+
278
+ // translate path pointers
279
+ for ( int pathNumber = 0 ; pathNumber < listOfShortPaths . Count ; pathNumber ++ )
280
+ {
281
+ path += "1" ;
282
+ for ( int arrayIndex = 1 ; arrayIndex < listOfShortPaths [ pathNumber ] . Length ; arrayIndex ++ )
283
+ {
284
+ if ( listOfShortPaths [ pathNumber ] [ arrayIndex ] . getY ( ) == 0 )
285
+ {
286
+ path += " " + listOfShortPaths [ pathNumber ] [ arrayIndex ] . getX ( ) + 1 ;
287
+ }
288
+ else
289
+ {
290
+ for ( int y = 1 ; y <= mazeSize - 2 ; y ++ )
291
+ {
292
+ if ( listOfShortPaths [ pathNumber ] [ arrayIndex ] . getY ( ) == y )
293
+ {
294
+ path += " " + ( mazeSize * y - ( y - 1 ) ) +
295
+ listOfShortPaths [ pathNumber ] [ arrayIndex ] . getX ( ) ;
296
+ }
297
+ }
298
+ }
299
+ }
300
+
301
+ if ( pathNumber != listOfShortPaths . Count - 1 )
302
+ {
303
+ path += "\n " ;
304
+ }
305
+ }
306
+ }
307
+ }
272
308
}
273
309
274
310
class Program
275
311
{
276
312
static void Main ( string [ ] args )
277
313
{
278
- Maze myMaze1 = new Maze ( @"C:\Users\ana_k\Documents\Visual Studio 2015\Projects \Week1_Maze\Week1_Maze\Input0.txt" ) ;
314
+ Maze myMaze1 = new Maze ( @"C:\Users\ana_k\Documents\CodingChallenges\Week 1\Karen \Week1_Maze\Week1_Maze\Input0.txt" ) ;
279
315
280
316
}
281
317
}
0 commit comments