#include<stdio.h>
#include<time.h>
#include<windows.h>
using namespace std;
#define MAX 1000
char mapp[MAX][MAX];
char vis[MAX][MAX];
/* U R D L */
const int xx[] = { 0, 1, 0, -1, -1, -1, 1, 1 };
const int yy[] = { -1, 0, 1, 0, -1, 1, -1, 1 };
const int x2[] = { 0, 2, 0, -2, -2, -2, 2, 2 };
const int y2[] = { -2, 0, 2, 0, -2, 2, -2, 2 };
int W, H;
char WALL = '#';
char PATH = '* ';
int sx, sy, ex, ey;
inline void gotoxy(int x, int y) {
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), { x, y });
}
void border(char mapp[MAX][MAX], char v) {
for (int i = 0; i <= W; i++)
mapp[0][i] = mapp[H][i] = v;
for (int i = 0; i <= H; i++)
mapp[i][0] = mapp[i][W] = v;
}
void fill(char mapp[MAX][MAX], char v) {
for (int i = 0; i <= H; i++)
for (int j = 0; j <= W; j++)
mapp[i][j] = v;
}
void printMap() {
for (int i = 0; i <= H; i++) {
for (int j = 0; j <= W; j++) {
printf("%c", mapp[i][j]);
}
puts("");
}
}
inline bool checkBorder(int x, int y) {
return x < W && x > 0 && y < H && y > 0;
}
void dfsp(int x, int y) {
int d, dic = 0;
vis[y][x] = 1;
/* go until no new direction */
for (; dic != (1 << 4) - 1; dic |= 1 << d) {
/* choose a random direction */
do d = rand() % 4;
while (dic & (1 << d));
if (checkBorder(x + x2[d], y + y2[d]) && !vis[y + y2[d]][x + x2[d]]) {
/* connect CurrentPosition and NewPosition */
mapp[y + yy[d]][x + xx[d]] = PATH;
/* show progress */
Sleep(20); gotoxy(x + xx[d], y + yy[d]); putchar(PATH);
dfsp(x + x2[d], y + y2[d]);
}
}
}
void dfsMazeCreate(char mapp[MAX][MAX], int w, int h) {
W = w << 1, H = h << 1;
srand(time(NULL));
memset(mapp, 0, sizeof(mapp));
memset(vis, 0, sizeof(vis));
border(mapp, WALL);
fill(mapp, WALL);
for (int i = 0; i <= H; i++)
for (int j = 0; j <= W; j++)
if (i & 1 && j & 1)
mapp[i][j] = PATH;
gotoxy(0, 0); printMap();
dfsp(1, 1);
}
int main(void) {
system("mode con: cols=120 lines=45");
int w, h;
w = 40, h = 10;
dfsMazeCreate(mapp, w, h);
gotoxy(0, 0); printMap();
gotoxy(w * 2 + 2, h * 2); puts("Created by DFS.");
getchar();
return 0;
}
#include<time.h>
#include<windows.h>
using namespace std;
#define MAX 1000
char mapp[MAX][MAX];
char vis[MAX][MAX];
/* U R D L */
const int xx[] = { 0, 1, 0, -1, -1, -1, 1, 1 };
const int yy[] = { -1, 0, 1, 0, -1, 1, -1, 1 };
const int x2[] = { 0, 2, 0, -2, -2, -2, 2, 2 };
const int y2[] = { -2, 0, 2, 0, -2, 2, -2, 2 };
int W, H;
char WALL = '#';
char PATH = '* ';
int sx, sy, ex, ey;
inline void gotoxy(int x, int y) {
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), { x, y });
}
void border(char mapp[MAX][MAX], char v) {
for (int i = 0; i <= W; i++)
mapp[0][i] = mapp[H][i] = v;
for (int i = 0; i <= H; i++)
mapp[i][0] = mapp[i][W] = v;
}
void fill(char mapp[MAX][MAX], char v) {
for (int i = 0; i <= H; i++)
for (int j = 0; j <= W; j++)
mapp[i][j] = v;
}
void printMap() {
for (int i = 0; i <= H; i++) {
for (int j = 0; j <= W; j++) {
printf("%c", mapp[i][j]);
}
puts("");
}
}
inline bool checkBorder(int x, int y) {
return x < W && x > 0 && y < H && y > 0;
}
void dfsp(int x, int y) {
int d, dic = 0;
vis[y][x] = 1;
/* go until no new direction */
for (; dic != (1 << 4) - 1; dic |= 1 << d) {
/* choose a random direction */
do d = rand() % 4;
while (dic & (1 << d));
if (checkBorder(x + x2[d], y + y2[d]) && !vis[y + y2[d]][x + x2[d]]) {
/* connect CurrentPosition and NewPosition */
mapp[y + yy[d]][x + xx[d]] = PATH;
/* show progress */
Sleep(20); gotoxy(x + xx[d], y + yy[d]); putchar(PATH);
dfsp(x + x2[d], y + y2[d]);
}
}
}
void dfsMazeCreate(char mapp[MAX][MAX], int w, int h) {
W = w << 1, H = h << 1;
srand(time(NULL));
memset(mapp, 0, sizeof(mapp));
memset(vis, 0, sizeof(vis));
border(mapp, WALL);
fill(mapp, WALL);
for (int i = 0; i <= H; i++)
for (int j = 0; j <= W; j++)
if (i & 1 && j & 1)
mapp[i][j] = PATH;
gotoxy(0, 0); printMap();
dfsp(1, 1);
}
int main(void) {
system("mode con: cols=120 lines=45");
int w, h;
w = 40, h = 10;
dfsMazeCreate(mapp, w, h);
gotoxy(0, 0); printMap();
gotoxy(w * 2 + 2, h * 2); puts("Created by DFS.");
getchar();
return 0;
}