Advertisement
Noam_15

Solve_The_Maze_V-Symbol

Jun 21st, 2018
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.23 KB | None | 0 0
  1. // The code of Symbol/Skyance (with few changes)
  2. // https://symbolsprogrammingblog.wordpress.com/2010/06/20/%D7%9E%D7%A6%D7%99%D7%90%D7%AA-%D7%94%D7%93%D7%A8%D7%9A-%D7%94%D7%A7%D7%A6%D7%A8%D7%94-%D7%91%D7%99%D7%95%D7%AA%D7%A8-%D7%91%D7%9E%D7%91%D7%95%D7%9A/
  3.  
  4. /************************************************
  5. *************************************************
  6. *************************************************
  7. Shortest Path Finding Algorithm, 20th June, 2010.
  8. By Symbol © 2010.
  9. *************************************************
  10. *************************************************
  11. ************************************************/
  12.  
  13. #include <stdio.h>
  14. #include <memory.h>
  15. #include <time.h>
  16.  
  17. #define HEIGHT    50
  18. #define WIDTH    9
  19.  
  20. #define WALL    -1
  21. #define PATH    0
  22.  
  23. #define UP        24
  24. #define DOWN    25
  25. #define RIGHT    26
  26. #define LEFT    27
  27.  
  28. int SolveMaze(int Maze[HEIGHT][WIDTH],
  29.               int CurrentRow, int CurrentCol,
  30.               int EndingRow, int EndingCol)
  31. {
  32.     static int Steps = 1;
  33.     static int Path[HEIGHT][WIDTH];
  34.  
  35.     int row;
  36.     int col;
  37.  
  38.     if (CurrentRow < 0 || CurrentRow >= HEIGHT ||
  39.         CurrentCol < 0 || CurrentCol >= WIDTH) //Out of maze range - return.
  40.         return -1;
  41.  
  42.     if (Maze[CurrentRow][CurrentCol] == PATH) //If the square is free, not a wall and we haven't been there yet
  43.     {
  44.         Maze[CurrentRow][CurrentCol] = Steps; //Set the square to be step number: Steps.
  45.  
  46.         if (CurrentRow == EndingRow && CurrentCol == EndingCol) //If we've reached our destination
  47.         {
  48.             if (Path[EndingRow][EndingCol] == PATH || Path[EndingRow][EndingCol] > Steps) //And if this path is shorter than the previous (basically, path length is held at our destination cell)
  49.                 memcpy(Path, Maze, WIDTH * HEIGHT * sizeof(int)); //Copy the maze
  50.         }
  51.         else //if we're not at our destination
  52.         {
  53.             Steps++; //Increase steps by 1.
  54.             for (row = -1; row <= 1; row++) //Vertical direction - up, down or none.
  55.             {
  56.                 for (col = -1; col <= 1; col++) //Horizontal direction - left, right or none.
  57.                 {
  58.                     if ((row != 0) ^ (col != 0)) //If and only if row isn't 0 OR col isn't 0, however not both or none, so one must be true!
  59.                         SolveMaze(Maze, CurrentRow + row, CurrentCol + col, EndingRow, EndingCol); //Check the next square for correct/shortest path. ignoring return value, result will be held at the static array.
  60.                 }
  61.             }
  62.             Steps--; //Decrease steps by 1.
  63.         }
  64.  
  65.         Maze[CurrentRow][CurrentCol] = PATH; //Restore the current square's value from "step number: Steps" to PATH (0) to return and check the next path without the current path intersecting and interfering with the next path check.
  66.     }
  67.  
  68.     if (Steps == 1) //If the Steps is 1, aka steps = 1, meaning we're returning back to user.
  69.     { //This check is used to prevent extra memory copying - can save a lot of comuting time and power.
  70.         memcpy(Maze, Path, WIDTH * HEIGHT * sizeof(int)); //Copy the shortest path from static array, Path.
  71.         memset(Path, 0, WIDTH * HEIGHT * sizeof(int)); //Fill the static array Path with zeros for future use.
  72.         return Maze[EndingRow][EndingCol] - 1; //Starting point (0 steps) contains 1, thus return Ending Point's value - 1.
  73.     }
  74.  
  75.     return -1; //Still not returning to user or path finding failed, return -1 indicating failure. (ignored within current function)
  76. }
  77.  
  78. int main()
  79. {
  80.     int Board[HEIGHT][WIDTH] = { //Initialize the maze
  81.         { WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL },
  82.         { WALL, PATH, PATH, PATH, PATH, PATH, PATH, PATH, WALL },
  83.         { WALL, PATH, WALL, WALL, WALL, PATH, WALL, WALL, WALL },
  84.         { WALL, PATH, WALL, PATH, WALL, PATH, PATH, PATH, WALL },
  85.         { WALL, WALL, WALL, PATH, WALL, PATH, WALL, PATH, WALL },
  86.         { WALL, PATH, WALL, PATH, PATH, PATH, PATH, PATH, WALL },
  87.         { WALL, PATH, WALL, WALL, WALL, WALL, WALL, PATH, WALL },
  88.         { WALL, PATH, PATH, PATH, PATH, PATH, PATH, PATH, WALL },
  89.         { WALL, PATH, WALL, WALL, WALL, WALL, WALL, WALL, WALL },
  90.         { WALL, PATH, PATH, PATH, PATH, PATH, PATH, PATH, WALL },
  91.         { WALL, WALL, WALL, WALL, WALL, WALL, WALL, PATH, WALL },
  92.         { WALL, PATH, PATH, PATH, PATH, PATH, PATH, PATH, WALL },
  93.         { WALL, PATH, WALL, WALL, PATH, WALL, WALL, PATH, WALL },
  94.         { WALL, PATH, PATH, PATH, PATH, PATH, PATH, PATH, WALL },
  95.         { WALL, PATH, WALL, PATH, WALL, PATH, WALL, PATH, WALL },
  96.         { WALL, PATH, WALL, PATH, PATH, PATH, WALL, PATH, WALL },
  97.         { WALL, PATH, WALL, WALL, WALL, WALL, WALL, PATH, WALL },
  98.         { WALL, PATH, PATH, PATH, PATH, PATH, PATH, PATH, WALL },
  99.         { WALL, WALL, WALL, WALL, WALL, WALL, WALL, PATH, WALL },
  100.         { WALL, PATH, PATH, PATH, PATH, PATH, WALL, PATH, WALL },
  101.         { WALL, WALL, PATH, WALL, WALL, WALL, WALL, PATH, WALL },
  102.         { WALL, PATH, PATH, PATH, WALL, PATH, PATH, PATH, WALL },
  103.         { WALL, PATH, WALL, WALL, WALL, PATH, WALL, WALL, WALL },
  104.         { WALL, PATH, PATH, PATH, WALL, PATH, PATH, PATH, WALL },
  105.         { WALL, PATH, WALL, PATH, WALL, PATH, WALL, PATH, WALL },
  106.         { WALL, PATH, WALL, PATH, PATH, PATH, PATH, PATH, WALL },
  107.         { WALL, WALL, WALL, WALL, WALL, PATH, WALL, PATH, WALL },
  108.         { WALL, PATH, PATH, PATH, PATH, PATH, PATH, PATH, WALL },
  109.         { WALL, PATH, WALL, PATH, PATH, WALL, PATH, WALL, WALL },
  110.         { WALL, PATH, PATH, PATH, WALL, WALL, PATH, WALL, WALL },
  111.         { WALL, WALL, WALL, PATH, WALL, WALL, PATH, WALL, WALL },
  112.         { WALL, PATH, PATH, PATH, PATH, WALL, PATH, PATH, WALL },
  113.         { WALL, WALL, WALL, PATH, WALL, WALL, PATH, PATH, WALL },
  114.         { WALL, PATH, PATH, PATH, WALL, WALL, WALL, PATH, WALL },
  115.         { WALL, PATH, WALL, PATH, WALL, PATH, WALL, PATH, WALL },
  116.         { WALL, PATH, WALL, PATH, PATH, PATH, PATH, PATH, WALL },
  117.         { WALL, PATH, PATH, PATH, PATH, WALL, PATH, PATH, WALL },
  118.         { WALL, PATH, WALL, PATH, PATH, PATH, PATH, PATH, WALL },
  119.         { WALL, WALL, WALL, WALL, PATH, WALL, PATH, WALL, WALL },
  120.         { WALL, PATH, PATH, PATH, PATH, WALL, PATH, PATH, WALL },
  121.         { WALL, PATH, WALL, WALL, WALL, WALL, PATH, WALL, WALL },
  122.         { WALL, PATH, PATH, PATH, WALL, PATH, PATH, PATH, WALL },
  123.         { WALL, PATH, WALL, WALL, WALL, WALL, PATH, WALL, WALL },
  124.         { WALL, PATH, PATH, PATH, WALL, PATH, PATH, PATH, WALL },
  125.         { WALL, PATH, WALL, PATH, WALL, PATH, WALL, PATH, WALL },
  126.         { WALL, PATH, WALL, PATH, PATH, PATH, PATH, PATH, WALL },
  127.         { WALL, WALL, WALL, WALL, WALL, WALL, WALL, PATH, WALL },
  128.         { WALL, PATH, PATH, PATH, PATH, PATH, PATH, PATH, WALL },
  129.         { WALL, PATH, PATH, WALL, WALL, WALL, PATH, PATH, WALL },
  130.         { WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL }
  131.     };
  132.  
  133.     //Pass Board as the maze, { 1, 1 } as the starting point and { 48, 1 } as the ending point.
  134.     int Steps;
  135.     int row;
  136.     int col;
  137.     int r;
  138.     int c;
  139.     char arrow; //ASCII representation of the arrow of our direction
  140.     clock_t begin, end;
  141.     double time_spent;
  142.  
  143.     begin = clock();
  144.     Steps = SolveMaze(Board, 1, 1, 48, 1);
  145.     end = clock();
  146.     time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  147.  
  148.     if (Steps != -1) //Path found
  149.     {
  150.         printf("%d steps\n-----------------------\n", Steps);
  151.  
  152.         for (row = 0; row < HEIGHT; row++) //Loop through every row
  153.         {
  154.             for (col = 0; col < WIDTH; col++) //And every column
  155.             {
  156.                 if (Board[row][col] == 1) //If it is the starting point
  157.                 {
  158.                     putchar('\x01'); //Then draw a smily
  159.                 }
  160.                 else if (Board[row][col] != WALL && Board[row][col] != PATH) //Not a wall/empty path
  161.                 {
  162.                     if (Board[row][col] <= Steps) //Still not last step
  163.                     {
  164.                         for (r = -1; r <= 1; r++) //Search for the next path
  165.                         {
  166.                             for (c = -1; c <= 1; c++) //To get the correct direction
  167.                             {
  168.                                 if ((r != 0) ^ (c != 0)) //If and only if direction is one of up, down, left or right.
  169.                                 {
  170.                                     if (Board[row + r][col + c] == Board[row][col] + 1) //The square in that direction is our next step
  171.                                     {
  172.                                         if (r == 0)
  173.                                             arrow = (c == 1 ? RIGHT : LEFT);
  174.                                         else
  175.                                             arrow = (r == 1 ? DOWN : UP);
  176.  
  177.                                         putchar(arrow);
  178.                                         c = r = 2; //break 2 loops
  179.                                     }
  180.                                 }
  181.                             }
  182.                         }
  183.                     }
  184.                     else //if (Board[row][col] == Steps + 1), aka last step
  185.                     {
  186.                         printf("X"); //Our destination!!!
  187.                     }
  188.                 }
  189.                 else //if (Board[row][col] <= PATH)
  190.                 {
  191.                     putchar(Board[row][col] == WALL ? '\xDB' : ' '); //Draw either a wall or an empty path
  192.                 }
  193.             } //for (columns)
  194.  
  195.             putchar('\n');
  196.         } //for (rows)
  197.     } //if (Steps == 0)
  198.     else
  199.     {
  200.         printf("Maze cannot be solved! :(\n");
  201.     }
  202.  
  203.     printf("Time:  %lf Seconds\n", time_spent);
  204.     getchar();
  205.  
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement