{算法·随机生成迷宫} 您所在的位置:网站首页 数据结构随机生成迷宫 {算法·随机生成迷宫}

{算法·随机生成迷宫}

#{算法·随机生成迷宫}| 来源: 网络整理| 查看: 265

刚在网上看到这个的算法的分析,然后写了一个C#版的。算法分析地址:http://www.j2megame.org/index.php/content/view/2120/125.html

 

算法原理:从起点开始,随机选择一个方向移动,一直移动到终点,则移动的路径便是迷宫的路径。移动过程中要保证路径不要相交,不要超出边界,生成效果

 

 

public partial class MainForm : Form    {        private Bitmap bitmap;        private const int SIZE=10;        private const int MAXMAZE=50;        private int[,] maze_matrix;        public MainForm()        {            InitializeComponent();            maze_matrix=new int[MAXMAZE+2,MAXMAZE+2];        }                void BtnCreateClick(object sender, EventArgs e)        {                        makeMaze(10);            Debugs(10);            Render(10);        }                        void Maze_panelMouseDown(object sender, MouseEventArgs e)        {                    }                void Maze_panelPaint(object sender, PaintEventArgs e)        {                    }                private void makeMaze(int size){            for (int i = 0; i                     maze_matrix[i,j]=1;                }            }                        for(int z1=0, z2=2*size+2; z1                maze_matrix[0,z1] = 0;                maze_matrix[z2,z1] = 0;            }            //建立出口            maze_matrix[2,1] = 0;            maze_matrix[2*size,2*size+1] = 0;            Random random=new Random(GetRandomSeed());            searchPath(random.Next(size)+1,random.Next(size)+1);        }        public int searchPath(int x,int y ){            int[,] dir= {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};            int zx = x*2;            int zy = y*2;            int  turn;            maze_matrix[zx,zy] = 0;            Random random=new Random(GetRandomSeed());            turn=random.Next(2)==0?1:3;            for (int i = 0,next=random.Next(4); i                     maze_matrix[zx+dir[next,0],zy+dir[next,1]]=0;                    searchPath(x+dir[next,0],y+dir[next,1]);                }            }            return 0;        }                public void Debugs(int size){            Form f=new Form();            f.Size=new Size(400,300);            TextBox textbox=new TextBox();            textbox.Multiline=true;            textbox.Size=new Size(400,300);                        f.Controls.Add(textbox);                                    f.Show();            for(int z2=1; z2                    textbox.Text+=maze_matrix[z2,z1]==0?" ":"1";                }                textbox.Text+="\r\n";            }                    }        int GetRandomSeed()        {            byte[] bytes = new byte[4];            System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();            rng.GetBytes(bytes);            return BitConverter.ToInt32(bytes, 0);        }        public void Render(int size){            this.maze_panel.Refresh();            bitmap=new Bitmap(300,300,PixelFormat.Format32bppArgb);            Graphics g=Graphics.FromImage(bitmap);                                    for(int z2=1; z2                    if (maze_matrix[z2,z1]==1) {                        g.DrawRectangle(new Pen(Color.Red),new Rectangle(z2*SIZE,z1*SIZE,SIZE,SIZE));                    }                }                            }                        Graphics mazeg=this.maze_panel.CreateGraphics();                    mazeg.DrawImage(bitmap,0,0);        }    }

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有