zoukankan      html  css  js  c++  java
  • 本人目前最短的迷宫生成算法程序源代码

    生成的结果示例:
     ━━━━┳━━┳━━┳━━━━━━┳━┓
    ┃━━━┓┗━┏┛┏┓┃━┳┓━━━┫┃┃
    ┣━━┓┣━┓┃━┫┃┗━┃┗━━┓┃┃┃
    ┃┏━┃┃━┫┣┓┃┣━━┻━━┓┣━┛┃
    ┃┣━━┻┓┃┃┣━┃━┳━┳━┃┃┏━┃
    ┃┃┏┳━┣━━┫┏┻┓┃┃┗┓┏┛┃━┫
    ┃┏┛┃━┫┏━┃┃┃┃┃┣┓┗┛┏┻━┃
    ┃┃┃┗┓┃┃┏┻━┫┏┛┃┃┏━┻┓━┫
    ┃┃┣━┗━┫┃━┓┃┃┏┛┃┃┏━┣━┃
    ┃┗┫┃┏━┻┳┓┗┳┫┃┃┃┃┃┏┻┓┃
    ┣━┃┃┃━┳┛┗━┃┃┃┗┳┛┃┃┃┗┫
    ┃┏┛┃┗┓┗━━┳━┃┗━┛━┻━┻┓┃
    ┃┃┃┗━┻━┳━┃┏┻━━┓┏━━━┛┃
    ┃┗┻┳━┏━┛┏┻┫┏━┓┃┃━━┳━┃
    ┣━━┃┃┃━━┛┃┃┃┃┃┗┻━━┃━┫
    ┗━━━┻┻━━━┻━┻┻━━━━━┻━ 
     
    还能更短一些,比如改成格子形式的输出,那段放后面吧,现在先给出上图的生成代码
    源代码(共25行):
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    int fy[500][500];
    int d[][2] = {{0, 1},{1, 0},{0, -1},{-1, 0}};
    char yzfy[][4]={" ","┃","━","┛","┃","┃","┓","┫",
         "━","┗","━","┻","┏","┣","┳","╋"};
    int w=20, h=15, rw = w+2, rh = h+2;
    int dfs(int y, int x){
        if (fy[y][x]) return 0; else fy[y][x] |= 0x10;
        for (int f=rand()%4, i=0, p=rand()&1?3:1, u,v; i<4; ++i,f=(f+p)%4)
            if (dfs(v=y+d[f][0], u=x+d[f][1]))
                fy[y][x] |= 1<<f, fy[v][u] |= (1<<((f+2)%4));
        return 1;
    }
    int main(){
        for (int y=0; y<rh; ++y) fy[y][0]|=10, fy[y][rw-1]|=10;
        for (int x=0; x<rw; ++x) fy[0][x]|=5, fy[rh-1][x]|=5;
        srand(time(NULL)); dfs(w/2,h/2); fy[1][1] |= 12; fy[rh-2][rw-2] |= 3;
        for (int y=1; y<rh; ++y,puts(""))
            for (int x=1; x<rw; ++x)
                printf("%s", yzfy[15^((fy[y-1][x-1]&3)|(fy[y][x]&12))]);
        return 0;
    }
     
     
    以下为最短版本的输出效果:
    ███████████████████████████████
      █               █     █     █
    █ ███████ █████ █ █ ███ █ ███ █
    █ █     █   █   █ █ █ █   █ █ █
    █ █ ███ ███ █ ███ █ █ █████ █ █
    █ █ █   █   █   █   █ █     █ █
    █ █ █ █████████ █████ █ ███ █ █
    █   █           █   █   █ █   █
    █████████████████ █ █ ███ █████
    █             █   █ █   █ █   █
    ███████ █████ █ ███ ███ █ █ █ █
    █     █     █ █ █ █   █ █ █ █ █
    █ ███ ███████ █ █ ███ █ █ █ █ █
    █ █ █     █   █ █   █ █ █ █ █ █
    █ █ █████ █ █ █ █ █ █ █ █ █ █ █
    █ █         █ █ █ █ █ █ █   █ █
    █ █████████████ ███ █ █ █████ █
    █   █   █   █   █   █ █ █     █
    █ █ █ █ █ █ █ ███ █ █ █ █ ███ █
    █ █   █   █       █ █     █    
    ███████████████████████████████
     
    生成的源代码(有效行数10):
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    int fy[500][500];
    int d[][2] = {{0,1},{1,0},{0,-1},{-1,0}},w=15,h=10,u=w*2+1,v=h*2+1;//1
    int yzfy(int y, int x){//2
        if(x<1||y<1||x>=u-1||y>=v-1||fy[y][x])return 0;else fy[y][x]=1;//3
        for (int f=rand()%4, i=0, p=rand()&1?3:1; i<4; ++i,f=(f+p)%4)//4
            if(yzfy(y+*d[f]*2,x+d[f][1]*2)) fy[y+*d[f]][x+d[f][1]]=1;//5
        return 1;//6
    }
    int main(){
        srand(time(NULL)); yzfy(1,1); fy[1][0] = fy[v-2][u-1] = 1;//7
        for (int y=0; y<v; ++y,puts(""))//8
            for (int x=0; x<u; ++x)//9
                printf("%s", fy[y][x]?" ":"█");//10
        return 0;
    }
     
  • 相关阅读:
    LeetCode(287)Find the Duplicate Number
    LeetCode(290) Word Pattern
    LeetCode(205)Isomorphic Strings
    LeetCode(201) Bitwise AND of Numbers Range
    LeetCode(200) Number of Islands
    LeetCode(220) Contains Duplicate III
    LeetCode(219) Contains Duplicate II
    命令行执行Qt程序
    LeetCode(228) Summary Ranges
    redis 的安装和使用记录
  • 原文地址:https://www.cnblogs.com/zqifa/p/c-maze-4.html
Copyright © 2011-2022 走看看