Some mazes have a very large number of solutions. In this case, we may encounter some problems of memory capacity. We will consider mazes whose number of solutions remain reasonable. Furthermore, we will avoid the solutions that pass repeatedly through the same cell in order not to have an infinite number of solutions.

Representation of mazes

We can represent a maze by a two-dimensional array of the integers with the following conventions,

We set the table size to 10x10 in the source code, but this size can be changed or even become variable. The cells are located by their coordinates in a Windows coordinate system; i.e,. by pairs of the form column/line.

mazes

Algorithm

We will build the paths, starting from the entry point of the Maze and going in all the possible directions.

Actually a path extending from one point to another will be represented by a list of X-axis values and a list of Y-axis values of the points that make up the path.

The idea is to replace a path by all the paths obtained from it, by adding an unoccupied box not already visited at the end.

It initializes a list with a path containing a single point of departure.

It traverses the list of paths and for each path,

The program

  1. bool alreadyVisited(List < int > pX, List < int > pY, int x, int y)
  2. {
  3. bool notVisited = true;
  4. int n = pX.Count - 1;
  5. while (n >= 0 && notVisited)
  6. {
  7. if (pX[n] == x)
  8. if (pY[n] == y)
  9. {
  10. notVisited = false;
  11. }
  12. n--;
  13. }
  14. return !notVisited;
  15. }
  1. public void SolveMaze()
  2. {
  3. List < List < int >> pathXList = new List < List < int >> ();
  4. List < List < int >> pathYList = new List < List < int >> ();
  5. List < int > pathX = new List < int >
  6. {
  7. startX
  8. };
  9. List < int > pathY = new List < int >
  10. {
  11. startY
  12. };
  13. pathXList.Add(pathX);
  14. pathYList.Add(pathY);
  15. while (pathXList.Count > 0)
  16. {
  17. int pathNumber = pathXList.Count;
  18. while (pathNumber > 0)
  19. {
  20. pathX = pathXList[0];
  21. pathY = pathYList[0];
  22. int n = pathX.Count - 1;
  23. int x = pathX[n];
  24. int y = pathY[n];
  25. if (x == endX && y == endY)
  26. {
  27. completePathX.Add(pathX);
  28. completePathY.Add(pathY);
  29. pathXList.RemoveAt(0);
  30. pathYList.RemoveAt(0);
  31. } else
  32. {
  33. if (x < width - 1) // Checks if not on right edge
  34. if (maze[x + 1, y] != 1)
  35. if (!alreadyVisited(pathX, pathY, x + 1, y))
  36. {
  37. List < int > pX = new List < int > (pathX);
  38. List < int > pY = new List < int > (pathY);
  39. pX.Add(x + 1);
  40. pY.Add(y);
  41. pathXList.Add(pX);
  42. pathYList.Add(pY);
  43. }
  44. if (x > 0) // Checks if not on left edge
  45. if (maze[x - 1, y] != 1)
  46. if (!alreadyVisited(pathX, pathY, x - 1, y))
  47. {
  48. List < int > pX = new List < int > (pathX);
  49. List < int > pY = new List < int > (pathY);
  50. pX.Add(x - 1);
  51. pY.Add(y);
  52. pathXList.Add(pX);
  53. pathYList.Add(pY);
  54. }
  55. if (y > 0) // Checks if not on top edge
  56. if (maze[x, y - 1] != 1)
  57. if (!alreadyVisited(pathX, pathY, x, y - 1))
  58. {
  59. List < int > pX = new List < int > (pathX);
  60. List < int > pY = new List < int > (pathY);
  61. pX.Add(x);
  62. pY.Add(y - 1);
  63. pathXList.Add(pX);
  64. pathYList.Add(pY);
  65. }
  66. if (y < height - 1) // Checks if not on bottom edge
  67. if (maze[x, y + 1] != 1)
  68. if (!alreadyVisited(pathX, pathY, x, y + 1))
  69. {
  70. List < int > pX = new List < int > (pathX);
  71. List < int > pY = new List < int > (pathY);
  72. pX.Add(x);
  73. pY.Add(y + 1);
  74. pathXList.Add(pX);
  75. pathYList.Add(pY);
  76. }
  77. pathXList.RemoveAt(0);
  78. pathYList.RemoveAt(0);
  79. }
  80. pathNumber--;
  81. }
  82. }
  83. }
output

The program can be improved by adding a method of automatically generating mazes. There is already an article in C# Corner about it.