-
Notifications
You must be signed in to change notification settings - Fork 0
/
randome_maze.cpp
71 lines (65 loc) · 1.39 KB
/
randome_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
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
class maze2d{
vector<vector<int> > grid;
vector<vector<bool> > visited;
int xdir[4] = {1,0,-1,0};
int ydir[4] = {0,1,0,-1};
int dd[4] = {1,2,4,8}; //1: South 2: East 4: North 8:West
int m,n;
public:
void generate(int _x,int _y){
if(_x<0||_x>=m||_y<0||_y>=n){
cout<<"invalid input!"<<endl;
return;
}
pair<int,int> cur;
stack<pair<int,int> > st;
st.push({_x,_y});
while(!st.empty()){
cur = st.top();
visited[cur.first][cur.second] = true;
//cout<<cur.first<<" "<<cur.second<<endl;
vector<int> dir;
for(int i = 0;i <4;i++){
int nx = cur.first+xdir[i];
int ny = cur.second+ydir[i];
if(nx >=0&&nx<m&&ny>=0&&ny<n&&visited[nx][ny]==false){
dir.push_back(i);
}
}
if(!dir.empty()){
int ir = rand()%dir.size();
grid[cur.first][cur.second] |= dd[dir[ir]];
st.push({cur.first+xdir[dir[ir]],cur.second+ydir[dir[ir]]});
}else{
st.pop();
}
}
}
maze2d(int _m,int _n){
grid = vector<vector<int> >(_m,vector<int>(_n,0));
visited = vector<vector<bool> >(_m,vector<bool>(_n,false));
m = _m;
n = _n;
}
void display(){
for(int i = 0;i < m;i++){
for(int j = 0;j < n;j++){
cout<<grid[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
}
};
int main(){
int m,n;
while(cin>>m>>n&&m&&n){
maze2d mz(m,n);
mz.generate(0,0);
mz.display();
}
}