[转载]不太规则的迷宫生成算法1

之前都说的比较方正,比较矩形的迷宫,现在来考虑一下,斜向迷宫

所谓斜向迷宫,就是所有墙,都是斜了45度,结构和原来的不太一样:

 ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
╲    ╱     ╱  ╲    ╱
╱ ╱  ╲ ╲╱ ╱ ╱  ╲╱  ╲
╲╱ ╱ ╱╲ ╲  ╱╲ ╲ ╲ ╲╱
╱ ╱ ╱ ╱ ╱╲╱  ╲ ╲   ╲
╲ ╲ ╲ ╲ ╲ ╲ ╲ ╲ ╲╱ ╱
╱  ╲   ╲  ╱ ╱ ╱ ╱╲ ╲
╲╱ ╱╲╱ ╱ ╱ ╱╲ ╲   ╲╱
╱  ╲  ╱╲ ╲  ╱ ╱╲ ╲ ╲
╲ ╲ ╲  ╱╲ ╲╱╲   ╲╱ ╱
╱ ╱ ╱ ╱  ╲   ╲╱╲  ╱╲
╲╱ ╱ ╱╲ ╲ ╲╱ ╱╲ ╲╱ ╱
╱  ╲  ╱ ╱ ╱ ╱  ╲   ╲
╲╱ ╱╲╱ ╱╲  ╱╲ ╲ ╲ ╲╱
╱        ╲    ╱    ╲
╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲ 

这里显示的效果不太好,你复制到记事本,用宋体字看就很清楚了。

细心的你,可能会发现,其实能用之前的规则迷宫,生成时做一些变形,

然后输出部分也相应修改一下,就可以做出来了,这个难度不高,

我也觉得不需要解释很多,直接给大家代码吧:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAZE_MAX 100
char map[MAZE_MAX+2][MAZE_MAX+2];
char *s[] = {" ","╲","╱"};
int search(int x,int y)
{
    static int d[4][2]={0,1,1,0,0,-1,-1,0};
    int zx=x*2,zy=y*2,next,turn,i;
    map[zx][zy]=1; turn=rand()%2? 1:3;
    for(i=0,next=rand()%4;i<4;i++,next=(next+turn)%4)
        if(map[zx+2*d[next][0]][zy+2*d[next][1]]==0)
            map[zx+d[next][0]][zy+d[next][1]]=1,
            search(x+d[next][0],y+d[next][1]);
    return 0;
}
void Make_Maze(int x,int y)
{
    int z1;
    for(z1=0;z1<=2*x;z1+=2)map[z1][2*x-z1]=1,map[2*y+z1][2*y+2*x-z1]=1;
    for(z1=0;z1<=2*y;z1+=2)map[z1][2*x+z1]=1,map[2*x+z1][z1]=1;
    map[1][2*x]=1;map[2*x+2*y-1][2*y]=1;
    srand((unsigned)time(NULL));
    search(x,y);
}
int main(void)
{
    int x=10,y=8,z1,z2,tx,ty;
    Make_Maze(x,y);
    for (z2=0; z2<y*2; z2+=1)
    {
        for (z1=0; z1<x*2; z1+=2)
        {
            ty = 2*x+z2-z1;
            tx = z2+z1+1;
            fputs(map[tx][ty]?*s:z2&1?s[1]:s[2],stdout);
            --ty; ++tx;
            fputs(map[tx][ty]?*s:z2&1?s[2]:s[1],stdout);
        }
        putchar(10);
    }
    return 0;
}

posted @ 2010-11-21 09:46  BuildNewApp  阅读(236)  评论(0编辑  收藏  举报