数据结构_不相交集合_绘制迷宫
2014-03-08 20:36
295 查看
//绘制迷宫
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Maze { public partial class Form1 : Form { public Form1() { InitializeComponent(); Size form_size=new Size(700,700); this.Size = form_size; this.MinimumSize = form_size; this.MaximumSize = form_size; } //迷宫房间格数 // int m = 20, n = 20; //迷宫房间大小 // int size = 10; //迷宫起点距离窗口左上角距离d*size // int d = 10; //窗口尺寸 // int width = 700; int height = 700; private void Form1_Load(object sender, EventArgs e) { Paint_Maze(); } public void Paint_Maze() { Bitmap image=new Bitmap(width,height); Graphics g = Graphics.FromImage(image); //渲染背景 // g.FillRectangle(new SolidBrush(Color.White), 0, 0, width, height); //绘制迷宫房间 // for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { g.DrawRectangle(new Pen(new SolidBrush(Color.Black),1), new Rectangle((i + d) * size, (j + d) * size, size, size)); } } //绘制入口、出口 // g.DrawLine(new Pen(new SolidBrush(Color.White), 1), new Point(d * size, d * size), new Point((d + 1) * size-1, d * size)); //入口横墙 g.DrawLine(new Pen(new SolidBrush(Color.White), 1), new Point(d * size, d * size), new Point(d * size, (d + 1) * size-1)); //入口竖墙 g.DrawLine(new Pen(new SolidBrush(Color.White), 1), new Point(((int)((m * n - 1) % n) + d) * size+1, ((int)((m * n - 1) / n) + 1 + d) * size), new Point(((int)((m * n - 1) % n) + 1 + d) * size, ((int)((m * n - 1) / n) + 1 + d) * size)); //出口横墙 g.DrawLine(new Pen(new SolidBrush(Color.White), 1), new Point(((int)((m * n - 1) % n) + 1 + d) * size, ((int)((m * n - 1) / n) + d) * size+1), new Point(((int)((m * n - 1) % n) + 1 + d) * size, ((int)((m * n - 1) / n) + 1 + d) * size)); //出口竖墙 //不相交集 // ADT adt = new ADT(); adt.InitSet(m * n); Random random = new Random(); while (adt.Find(0) != adt.Find(m * n - 1)) { //随机产生一个房间号 // int room_a = random.Next(m * n); List<int> list=new List<int>(); //检查房间a相邻四间房子a-n(上)、a+n(下)、a-1(左)、a+1(右)是否存在 // if (room_a - n >= 0) { list.Add(room_a - n); } if (room_a + n < m * n) { list.Add(room_a + n); } if (room_a - 1 >= ((int)(room_a / n)) * n) { list.Add(room_a - 1); } if (room_a + 1 < ((int)(room_a / n) + 1) * n) { list.Add(room_a + 1); } //随机产生一个相邻的房间 // int index = random.Next(list.Count); int room_b = list[index]; //检查房间是否连通 // if (adt.Find(room_a) == adt.Find(room_b)) continue; else { int root_a = adt.Find(room_a); int root_b = adt.Find(room_b); //记录哪个房间序号小,靠近入口 // int room_min = Math.Min(room_a, room_b); //定义即将拆掉的墙的坐标 // int x1, x2, y1, y2; //相差1,拆竖墙;否则拆横墙 // if (Math.Abs(room_a - room_b) == 1) { //拆竖墙 if (room_a == room_min) //room_a在room_b前面 { x1 = ((int)(room_a % n) + 1+d) * size; y1 = ((int)(room_a / n) + d) * size + 1; x2 = x1; y2 = ((int)(room_a / n) + 1 + d) * size - 1; } else { x1 = ((int)(room_b % n) + 1+d) * size; y1 = ((int)(room_b / n) + d) * size + 1; x2 = x1; y2 = ((int)(room_b / n) + 1 + d) * size - 1; } } else //拆横墙 { if (room_a == room_min) //room_a在room_b上面 { x1 = ((int)(room_a % n) + d) * size + 1; y1 = ((int)(room_a / n)+1+d) * size; x2 = ((int)(room_a % n) + 1 + d) * size - 1; y2 = y1; } else { x1 = ((int)(room_b % n)+d) * size+1; y1 = ((int)(room_b / n) + 1+d) * size; x2 = ((int)(room_b % n) +1+ d) * size - 1; y2 = y1; } } //将room_a和room_b连通 // adt.Union(root_a, root_b); g.DrawLine(new Pen(new SolidBrush(Color.White), 1), new Point(x1, y1), new Point(x2, y2)); } } //显示图像 // pictureBox1.Image = image; image.Save("maze.bmp"); } } }
//不相交集合类,合并按秩合并
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Maze { class ADT { //parent list // private static int[] parent; private static int[] rank; //Initialize // public void InitSet(int numElements) { parent = new int[numElements]; rank = new int[numElements]; for (int i = 0; i < numElements; i++) { parent[i] = -1; rank[i] = 1; } } //Find // public int Find(int num) { int temp; temp = num; while (parent[temp] != -1) { temp = parent[temp]; } return temp; } //Union // public void Union(int root1, int root2) { if (rank[root1] < rank[root2]) { parent[root1] = root2; } else { if (rank[root1] == rank[root1]) { rank[root1]++; } parent[root2] = root1; } } } }
写完绘制迷宫程序,该填写破解迷宫程序。
相关文章推荐
- 算法导论 第21章 不相交集合的数据结构
- hdu 1272 小希的迷宫(不相交集合森林)
- 用于不相交集合的数据结构
- 用不相交集数据结构生成迷宫
- 算法导论用于不相交集合的数据结构
- 不相交集合的数据结构
- 算法导论——用于不相交集合的数据结构
- 第21章:用于不相交集合的数据结构
- 算法导论学习笔记(15)——用于不相交集合的数据结构
- 并查集(不相交集合数据结构)
- 算法起步之并查集(不相交集合数据结构)
- 算法导论21(用于不相交集合的数据结构)
- 不相交集合的数据结构-并查集
- [算法导论读书笔记]用于不相交集合的数据结构(并查集)
- 利用不相交集类制作迷宫游戏(数据结构课程设计——迷宫老鼠)
- 用于不相交集合的数据结构——查并集
- 用于不相交集合的数据结构——查并集
- 算法导论 第21章 用于不相交集合的数据结构
- 算法导论代码 第21章 用于不相交集合的数据结构
- 【算法与数据结构】不相交集合——并查集