zhangsongcui

Maze

Aug 14th, 2011
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.03 KB | None | 0 0
  1. #include <wchar.h>
  2. #include <locale.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. int fy[500][500];
  7. int d[][2] = {{0, 1},{1, 0},{0, -1},{-1, 0}};
  8. wchar_t yzfy[]= {L' ',L'┃',L'━',L'┛',L'┃',L'┃',L'┓',L'┫',
  9.                  L'━',L'┗',L'━',L'┻',L'┏',L'┣',L'┳',L'╋'};
  10.  
  11. const int w=35, h=20, rw = w+2, rh = h+2;
  12.  
  13. int dfs(int y, int x)
  14. {
  15.     if (fy[y][x])
  16.         return 0;
  17.     else
  18.         fy[y][x] |= 0x10;
  19.     for (int f=rand()%4, i=0, p=rand()&1?3:1, u,v; i<4; ++i,f=(f+p)%4)
  20.         if (dfs(v=y+d[f][0], u=x+d[f][1]))
  21.             fy[y][x] |= 1<<f, fy[v][u] |= (1<<((f+2)%4));
  22.     return 1;
  23. }
  24.  
  25. int main()
  26. {
  27.     setlocale(LC_ALL, "");
  28.     for (int y=0; y<rh; ++y) fy[y][0]|=10, fy[y][rw-1]|=10;
  29.     for (int x=0; x<rw; ++x) fy[0][x]|=5, fy[rh-1][x]|=5;
  30.     srand(time(NULL));
  31.     dfs(w/2,h/2);
  32.     fy[1][1] |= 12;
  33.     fy[rh-2][rw-2] |= 3;
  34.     for (int y=1; y<rh; ++y,putwchar(L'\n'))
  35.         for (int x=1; x<rw; ++x)
  36.             putwchar(yzfy[15^((fy[y-1][x-1]&3)|(fy[y][x]&12))]);
  37.     return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment