fork download
  1.  
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <time.h>
  5.  
  6. enum {
  7. W = 36, // width of maze
  8. H = 25 // height of maze
  9. };
  10.  
  11. enum {
  12. North,
  13. East,
  14. South,
  15. West,
  16. NDir
  17. };
  18.  
  19. char visited[H][W];
  20. char horz[H][W - 1]; // horizontal E-W paths in the maze
  21. char vert[H - 1][W]; // veritcal N-S paths in the maze
  22.  
  23. /*
  24.  * Fill dir with directions to unvisited cells, return count
  25.  */
  26. int adjacent(int dir[], int x, int y)
  27. {
  28. int ndir = 0;
  29.  
  30. if (y > 0 && visited[y - 1][x] == 0) dir[ndir++] = North;
  31. if (x < W - 1 && visited[y][x + 1] == 0) dir[ndir++] = East;
  32. if (y < H - 1 && visited[y + 1][x] == 0) dir[ndir++] = South;
  33. if (x > 0 && visited[y][x - 1] == 0) dir[ndir++] = West;
  34.  
  35. return ndir;
  36. }
  37.  
  38. /*
  39.  * Traverse cells depth first and create paths as you go
  40.  */
  41. void dfs(int x, int y)
  42. {
  43. int dir[NDir];
  44. int ndir;
  45.  
  46. visited[y][x] = 1;
  47.  
  48. ndir = adjacent(dir, x, y);
  49.  
  50. while (ndir) {
  51. int pick = rand() % ndir;
  52.  
  53. switch (dir[pick]) {
  54. case North: vert[y - 1][x] = 1; dfs(x, y - 1); break;
  55. case East: horz[y][x] = 1; dfs(x + 1, y); break;
  56. case South: vert[y][x] = 1; dfs(x, y + 1); break;
  57. case West: horz[y][x - 1] = 1; dfs(x - 1, y); break;
  58. }
  59.  
  60. ndir = adjacent(dir, x, y);
  61. }
  62. }
  63.  
  64. /*
  65.  * Print a map of the maze
  66.  */
  67. void map(void)
  68. {
  69. int i, j;
  70.  
  71. for (i = 0; i < W; i++) {
  72. putchar('_');
  73. putchar('_');
  74. }
  75.  
  76. putchar('\n');
  77.  
  78. for (j = 0; j < H; j++) {
  79. putchar('|');
  80.  
  81. for (i = 0; i < W; i++) {
  82. putchar(j < H - 1 && vert[j][i] ? ' ' : '_');
  83. putchar(i < W - 1 && horz[j][i] ? '_' : '|');
  84. }
  85.  
  86. putchar('\n');
  87. }
  88. }
  89.  
  90. int main()
  91. {
  92. srand(time(NULL));
  93.  
  94. dfs(0, 0);
  95. map();
  96.  
  97. return 0;
  98. }
  99.  
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
________________________________________________________________________
| | ___ | ___ | _ | _____ | _ _____ |__ _ | _ ___ |__ _ _____ |__ _____ |
| |___| |__ | | |___| _ __| |____ |__ | |___| | |__ |_| |______ | | | _ |
|__ | __| __|___| _ | |_| __|__ | | __|__ | | | __| | __| _ | | | __| | |
| | |__ | | ______| |_____| _ | | | | _ |__ | | | __| ____| | | |_| __| |
| | | _ | |__ |__ |__ |__ __| | | |___| |___| | |__ |_____| | |__ | | | |
| __| |_|_____| ____|__ __|_____|__ |______ | | _ | | ______|__ |___| | |
|__ | _________ | ______| _______ | ___ | | | | |_| | ___ | _ | ________|
| | | | _ | _ |___| ___ | ___ | __|__ | | |___| _ | | | |___| |__ | ___ |
| | |_| |___|__ | __| __|_| __|__ | __| | ______|___|__ | _ |__ |___| __|
| |_____|__ _ |___| |_______| | __| __| |____ | ___ |_____|___| | _ |__ |
| _______ |_|______ ___ | ___ |__ | | __| _ | | | | | _ | _ | __|_|__ | |
| _____ |________ __|_____| | | __| | | __| | | | |___|___| | | ___ | | |
| | | __| ___ | __| _ |_______| |___| __| | | | | _______ |___|__ |__ | |
|__ |__ | _ | | | | |________ |____ | | _ | |___|______ | _____ __|___| |
| __| __|_| |_| | |__ | _ | | __| __|___| | | _ |____ | |____ |_| ______|
|__ | | _ |__ | | _ |___| | |__ | | _ | __| | |____ | |____ |_____| _ | |
| __| | |_____| | |_____| | __| | | |___| |___| | __|__ ____| ______|__ |
|___| | | ______|____ _ | | _ | | |______ |____ |____ |____ | _ | ______|
| _ | | | |____ ___ |_| | |_| | | | ___ |__ | __| _ | | | __|_| |____ | |
| |___| _____ | |_______|__ | |___|___|__ __| | __| | | |_______| | __| |
| _ | __| _ |__ | _ | _ __| | ___ _____ |____ | | __| | _____ ____| | _ |
|_| | | __| | | | |___| | __|___| | ____|____ | | | _ | |_____| _ | | | |
| __|_|__ | |___| | _ | | | ___ | |__ | _ _ |_| | | | | | ______| |___| |
| | _ __| |____ | | |___|___| |___| | |_| |__ | | |_| | |__ |__ |__ | __|
|___|_________|___|_______________________|_____|_____|___________|_____|