Created
April 25, 2016 07:33
-
-
Save jacky860226/bbc1472875404b42533a9e24bc95ae7f to your computer and use it in GitHub Desktop.
Random Kruskal Maze Generation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<bits/stdc++.h> | |
using namespace std; | |
struct P{ | |
P(int q,int w,int e,int r):a(q),b(w),x(e),y(r){} | |
int a,b; | |
int x,y; | |
}; | |
char s[105][105]; | |
int id[105][105],cnt; | |
int st[10005]; | |
vector<P>p; | |
inline int find(int x){ | |
return st[x]==x?x:st[x]=find(st[x]); | |
} | |
inline void print(int n,int m){ | |
for(int i=0;i<n;++i){ | |
for(int j=0;j<m;++j){ | |
if(s[i][j]=='#')printf("#"); | |
else printf(" "); | |
} | |
puts(""); | |
} | |
} | |
int main(){ | |
int n=25,m=25; | |
for(int i=0;i<n;++i){ | |
for(int j=0;j<m;++j){ | |
s[i][j]='#'; | |
} | |
} | |
for(int i=1;i<n;i+=2){ | |
for(int j=1;j<m;j+=2){ | |
id[i][j]=++cnt; | |
s[i][j]=' '; | |
} | |
} | |
for(int i=1;i<n-1;++i){ | |
for(int j=1;j<m-1;++j){ | |
if(s[i][j]=='#'){ | |
if(s[i][j-1]!='#'&&s[i][j+1]!='#'){ | |
p.push_back(P(id[i][j-1],id[i][j+1],i,j)); | |
}else if(s[i-1][j]!='#'&&s[i+1][j]!='#'){ | |
p.push_back(P(id[i-1][j],id[i+1][j],i,j)); | |
} | |
} | |
} | |
} | |
for(int i=1;i<=cnt;++i)st[i]=i; | |
srand(time(0)); | |
random_shuffle(p.begin(),p.end()); | |
for(int i=0;i<(int)p.size();++i){ | |
if(find(p[i].a)!=find(p[i].b)){ | |
st[find(p[i].a)]=find(p[i].b); | |
s[p[i].x][p[i].y]=' '; | |
} | |
} | |
print(n,m); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment