-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze.cpp
142 lines (122 loc) · 3.28 KB
/
maze.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <iostream>
#include <string>
#include <stack>
#include <vector>
#include <random>
#include <set>
#include <list>
using namespace std;
#define random(x) (rand()%x)
#define MAX 1000
int width, height;
char maze[MAX][MAX];
struct Block{
int x;
int y;
// bool isVisited;
char d;
};
void initmaze(int w, int h){
for(int i = 0; i < w; i++){
for(int j = 0; j < h; j++){
// if(i % 2 == 0 || j % 2 == 0){
maze[j][i] = 0;
// } else {
// maze[j][i] = 1;
// }
}
}
}
void printmaze(int w, int h){
for(int i = 0; i < w; i++){
for(int j = 0; j < h; j++){
// printf("%d",maze[j][i]);
if(maze[j][i] == 0){
cout << '#';
}else{
cout << ' ';
}
}
cout << endl;
}
}
int main(){
//迷宫长宽
cout << "input width:";
cin >> width;
cout << "input height:";
cin >> height;
int w = width * 2 + 1;
int h = height * 2 + 1;
//初始化迷宫 生成一个2*w+1 2*h+1的数组
initmaze(w, h);
//随机选择一个起始点
srand(time(NULL));
int x = 2 * random(width) + 1;
int y = 2 * random(height) + 1;
// cout << x << y;
//把邻墙放入列表
vector<Block> neighbor;
if(x > 1){
Block block = {x-1, y, 'L'};
neighbor.push_back(block);
}
if(x < w - 2){
Block block = {x+1, y, 'R'};
neighbor.push_back(block);
}
if(y > 1){
Block block = {x, y-1 ,'U'};
neighbor.push_back(block);
}
if(y < h - 2){
Block block = {x, y+1, 'D'};
neighbor.push_back(block);
}
//
while(neighbor.size()){
int r = random(neighbor.size());
switch(neighbor[r].d){
case 'U':
y = neighbor[r].y -1;
x = neighbor[r].x;
break;
case 'D':
y = neighbor[r].y + 1;
x = neighbor[r].x;
break;
case 'L':
y = neighbor[r].y;
x = neighbor[r].x - 1;
break;
case 'R':
y = neighbor[r].y;
x = neighbor[r].x + 1;
break;
}
if(maze[y][x] == 0){
//打通墙和通路
maze[neighbor[r].y][neighbor[r].x] = 1;
maze[y][x] = 1;
if(neighbor[r].d != 'D' && y > 1 && maze[y-1][x] == 0 && maze[y-2][x] == 0){
Block block = {x, y-1, 'U'};
neighbor.push_back(block);
}
if(neighbor[r].d != 'U' && y < h-2 && maze[y+1][x] == 0 && maze[y+2][x] == 0){
Block block = {x, y+1, 'D'};
neighbor.push_back(block);
}
if(neighbor[r].d != 'R' && x > 1 && maze[y][x-1] == 0 && maze[y][x-2] == 0){
Block block = {x-1, y, 'L'};
neighbor.push_back(block);
}
if(neighbor[r].d != 'L' && x < w-2 && maze[y][x+1] == 0 && maze[y][x+2] == 0){
Block block = {x+1, y, 'R'};
neighbor.push_back(block);
}
}
neighbor.erase(neighbor.begin()+r);
}
printmaze(w, h);
return 0;
}