Advertisement
Guest User

Untitled

a guest
May 28th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.53 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <stdio.h>
  3. #include <iostream>
  4.  
  5. const int MazeHeight = 9;
  6. const int MazeWidth = 9;
  7.  
  8. char Maze[MazeHeight][MazeWidth + 1] =
  9. {
  10.     "# #######",
  11.     "#   #   #",
  12.     "# ### # #",
  13.     "# #   # #",
  14.     "# # # ###",
  15.     "#   # # #",
  16.     "# ### # #",
  17.     "#   #   #",
  18.     "####### #",
  19. };
  20.  
  21. const char Wall = '#';
  22. const char Free = ' ';
  23. const char Filler = '*';
  24.  
  25. class COORD
  26. {
  27. public:
  28.     int X;
  29.     int Y;
  30.     COORD(int x = 0, int y = 0) { X = x, Y = y; }
  31.     COORD(const COORD &coord) { X = coord.X; Y = coord.Y; }
  32. };
  33.  
  34. COORD StartingPoint(1, 0);
  35. COORD EndingPoint(7, 8);
  36.  
  37. void PrintMaze()
  38. {
  39.     for (int Y = 0; Y < MazeHeight; Y++)
  40.     {
  41.         printf("%s\n", Maze[Y]);
  42.     }
  43.     printf("\n");
  44. }
  45.  
  46. bool Solve(int X, int Y)
  47. {
  48.    
  49.     Maze[Y][X] = Filler;
  50.  
  51.     if (X == EndingPoint.X && Y == EndingPoint.Y)
  52.     {
  53.         return true;
  54.     }
  55.  
  56.     if (X > 0 && Maze[Y][X - 1] == Free && Solve(X - 1, Y))
  57.     {
  58.         return true;
  59.     }
  60.     if (X < MazeWidth && Maze[Y][X + 1] == Free && Solve(X + 1, Y))
  61.     {
  62.         return true;
  63.     }
  64.     if (Y > 0 && Maze[Y - 1][X] == Free && Solve(X, Y - 1))
  65.     {
  66.         return true;
  67.     }
  68.     if (Y < MazeHeight && Maze[Y + 1][X] == Free && Solve(X, Y + 1))
  69.     {
  70.         return true;
  71.     }
  72.  
  73.     // Otherwise we need to backtrack and find another solution.
  74.     Maze[Y][X] = Free;
  75.  
  76.     // If you want progressive update, uncomment these lines...
  77.     //PrintDaMaze();
  78.     //Sleep(50);
  79.     return false;
  80. }
  81.  
  82. int _tmain(int argc, _TCHAR* argv[])
  83. {
  84.     if (Solve(StartingPoint.X, StartingPoint.Y))
  85.     {
  86.         PrintMaze();
  87.     }
  88.     else
  89.     {
  90.         printf("Damn\n");
  91.     }
  92.     system("pause");
  93.     return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement